簡體   English   中英

模擬靜態方法調用時發生UnexpectedInvocation

[英]UnexpectedInvocation while mocking a static method call

我正在嘗試使用JMockit測試靜態方法。 此方法使用實用程序類靜態方法。

運行測試時,JMockit會抱怨並引發以下異常:

Test set: com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest
    --------------------------------------------------------------------
    Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.086 sec  FAILURE!
    testMakeResourceRequestPositive(com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest)  Time elapsed: 0.085 sec   ERROR!
    mockit.internal.UnexpectedInvocation: Parameter "requestToSend" of com.company.pkg.subpkg.mylib.NetworkUtil#sendHttpRequest(org.apache.http.client.methods.HttpUriRequest requestToSend) expected GET https://localhost HTTP/1.1, got GET https://localhost HTTP/1.1
        at com.company.pkg.subpkg.mylib.NetworkUtil.sendHttpRequest(NetworkUtil.java)
        at com.company.pkg.subpkg.mylib.serviceaccess.client.ServiceCommunicator.makeResourceRequest(ServiceCommunicator.java:57)
        at com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest.testMakeResourceRequestPositive(ServiceCommunicationTest.java:79)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:606)
        .    .    .
        .    .    .

這是導致引發異常的測試-



    @RunWith(JUnit4.class)
    public class ServiceCommunicationTest {

        @Mocked
        private NetworkUtil _mockedNetworkUtil;

        @Test
        public void testMakeResourceRequestPositive() throws IOException {

            ResourcesDTO resDto = null;
            final String jsonResponse = JsonResponsesTestUtil.SUCCESSFUL_RESOURCES_RESPONSE;

            final String serviceUrl = "https://localhost";
            final HttpUriRequest requestToSend = new HttpGet(serviceUrl);

            new Expectations() {
                {
                    NetworkUtil.validateStringInput(serviceUrl, "rootUri");

                    NetworkUtil.sendHttpRequest(requestToSend);
                    returns(jsonResponse);
                }
            };

            // Make the actual call …
            try {
                resDto = ServiceCommunicator.makeResourceRequest(serviceUrl);
            } catch (IOException e) {
                Assert.fail("Failed to parse/obtain the Resources DTO: " + e.getMessage());
            }

            Assert.assertNotNull(resDto);
            Assert.assertNotNull(resDto.getSelfUri());
        }
    }

令人困惑的是,期望的字符串和實際的字符串(在異常中報告)相同-

expected GET https://localhost HTTP/1.1
     got GET https://localhost HTTP/1.1

被測試的方法在這里-

public static ResourcesDTO makeResourceRequest(String rootUri) throws IOException {

    NetworkUtil.validateStringInput(rootUri, "rootUri");

    // Preparing HTTP Request ...
    HttpUriRequest requestToSend = new HttpGet(rootUri);

    // Headers that needs to be set ...
    requestToSend.addHeader(HttpHeaders.ACCEPT,       MimeConstants.CONTENT_TYPE_STRING);

    // Send Request to Authorization Service ...
    String response = NetworkUtil.sendHttpRequest(requestToSend);

    // Got the response, construct a DTOs out of it ...
    StringReader reader = new StringReader(response);

    // Convert the JSON response to appropriate DTO ...
    ObjectMapper mapper = new ObjectMapper();
    ResourcesDTO resourcesDto = mapper.readValue(reader, ResourcesDTO.class);

    return resourcesDto;
}

上面方法引用的實用程序類是-



    public final class NetworkUtil {

        // Prevent Instance Creation …
        private NetworkUtil() {
        }

        public  static  ResourcesDTO  makeResourceRequest(String  rootUri)  throws  IOException {

            NetworkUtil.validateStringInput(rootUri, "rootUri");

            // Preparing HTTP Request ...
            HttpUriRequest requestToSend = new HttpGet(rootUri);

            // Headers that needs to be set ...
            requestToSend.addHeader(HttpHeaders.ACCEPT,   MimeConstants.CONTENT_TYPE_STRING);

            // Send Request to the Server ...
            String response = NetworkUtil.sendHttpRequest(requestToSend);

            // Got the response, construct a DTOs out of it ...
            StringReader reader = new StringReader(response);

            // Convert the JSON response to appropriate DTO ...
            ObjectMapper mapper = new ObjectMapper();
            ResourcesDTO resourcesDto = mapper.readValue(reader, ResourcesDTO.class);

            return resourcesDto;
        }
    }

有人可以看到這里發生的事情嗎?

最后,我能夠確定此意外調用異常的原因。

NetworkUtil.sendHttpRequest((HttpUriRequest) any);
returns(jsonResponse);

如上所示,我在Expectations塊中更改了返回之前的行。 現在我沒有傳遞HttpUriRequest類型的對象,而是告訴JMockit可接受的任何 HttpUriRequest類型的對象。

我相信,JMockit對模擬API調用的參數執行equals()。 這里的問題可能是HttpUriRequest沒有實現自己的equals()方法,而是從Object繼承而來。 這意味着它實際上是在做一個object1 == object2。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM