How to use mocking in this case?

shreeram banne

Here is my problem:

This is the method for which i am trying to write test case.

This method trying to create instance of "HttpClient" but this server is not available on my side.

So i am trying to achieve this by using mocking, But i am not successful in that.

So could anyone tell me that how to do that?

Here is the method:

  public boolean callGet(final boolean keepResult) {
            boolean ok = false;
            if (url != null) {
                try {
                    log.appendLine("Url:");
                    log.append(url);
                    final HttpClient client = new HttpClient();
                    setAuthentication(client);
                    final GetMethod method = new GetMethod(url);
                    if (Utils.isFilled(userAgent)) {
                        method.setRequestHeader("User-Agent", userAgent);

                    }
                    status = client.executeMethod(method);
                    ok = (status == HttpStatus.SC_OK);
                    log.appendLine("Status http call:");
                    log.append(status);
                    if (ok) {
                        if (keepResult) {
                            if (maxSizeResult > 0) {
                                result = method
                                        .getResponseBodyAsString(maxSizeResult);

                            } else {
                                result = method.getResponseBodyAsString();

                            }
                        }
                    }
                } catch (final Exception e) {
                }
            }
            return ok;

This is my unit test method:

   @Before
        public void start() {
            urlcaller = new UrlCaller();
            UrlCaller mock1 = Mockito.mock(UrlCaller.class);
            Mockito.when(mock1.callGet(true)).thenReturn(true);

        }

        @Test
        public void testSetUserAgent() throws HttpException, IOException {

        boolean t1 = urlcaller.callGet(true);

        System.out.println(t1);

    }

}

This is the error i am getting :

Problem:
    UrlCaller.java
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:946)
        ...
        at de.bcode.utilsWithLog.UrlCaller.callGet(UrlCaller.java:115)
        at de.bcode.utilsWithLog.TestUrlCaller.testSetUserAgent(TestUrlCaller.java:52)
    Caused by:
        java.io.EOFException: SSL peer shut down incorrectly
            at sun.security.ssl.InputRecord.read(InputRecord.java:482)
            ...
            at de.bcode.utilsWithLog.UrlCaller.callGet(UrlCaller.java:115)
            at de.bcode.utilsWithLog.TestUrlCaller.testSetUserAgent(TestUrlCaller.java:52)
false

Thank you for reading all the question :-) Hope it is clear enough.

prgmtc

There are a couple issues with your current approach:

You typically use mocks/stubs/fakes/whatever, i.e. providing a fake implementation of a collaborator class, when testing another class in isolation. For example, you could be unit testing some controller in isolation and provide mocks for each of its collaborating objects (a fake repository, a fake UrlCaller,...). That way you can test the actual production code of the system under test (= the controller) in isolation, without depending on the correct functioning of its collaborators.

Your test in question creates both a real and a fake object of the UrlCaller class, then it continues to exercise the real urlcaller object. This means that all the actual production code gets triggered and the created mock url caller is happily doing nothing.

What I typically do in these situations is:

  • Figure out the single responsibility of the UrlCaller class
  • If it has any hard to test/external dependencies (like real HTTP traffic), separate these into a new class and provide some way of replacing this collaborating object with a fake (you can do this by using constructor injection or the subclass to test pattern, for example)
  • Provide the production implementation for your collaborators in your production code
  • Provide a fake implementation in your test code to test your system under test without those nasty external dependencies.

For your concrete case: It seems the external dependency that makes this code hard to test is the HttpClient. Try extracting the dependency on this class out of UrlCaller. If this seems like a moot point to you ("but then the UrlCaller class would not be doing anything"), I would drop the urge to unit test the UrlCaller class in isolation and just depend on an integration test that makes the real http call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related