Reputation: 526
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.
Upvotes: 2
Views: 343
Reputation: 750
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:
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.
Upvotes: 2
Reputation: 64628
Your test doesn't make much sense. You create a mock and call the mock in your test.
Usually you do it this way:
I think you should separate the parts that you want to mock from the code you want to test and inject the mocked part by an interface.
Upvotes: 0