unit testing - How to use mocking in this case? -
here problem:
this method trying write test case.
this method trying create instance of "httpclient" server not available on side.
so trying achieve using mocking, not successful in that.
so tell me how that?
here 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 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 error getting :
problem: urlcaller.java javax.net.ssl.sslhandshakeexception: remote host closed connection during handshake @ sun.security.ssl.sslsocketimpl.readrecord(sslsocketimpl.java:946) ... @ de.bcode.utilswithlog.urlcaller.callget(urlcaller.java:115) @ de.bcode.utilswithlog.testurlcaller.testsetuseragent(testurlcaller.java:52) caused by: java.io.eofexception: ssl peer shut down incorrectly @ sun.security.ssl.inputrecord.read(inputrecord.java:482) ... @ de.bcode.utilswithlog.urlcaller.callget(urlcaller.java:115) @ de.bcode.utilswithlog.testurlcaller.testsetuseragent(testurlcaller.java:52) false thank reading question :-) hope clear enough.
there couple issues current approach:
you typically use mocks/stubs/fakes/whatever, i.e. providing fake implementation of collaborator class, when testing another class in isolation. example, unit testing controller in isolation , provide mocks each of collaborating objects (a fake repository, fake urlcaller,...). way can test actual production code of system under test (= controller) in isolation, without depending on correct functioning of collaborators.
your test in question creates both real , fake object of urlcaller class, continues exercise real urlcaller object. means actual production code gets triggered , created mock url caller happily doing nothing.
what typically in these situations is:
- figure out single responsibility of urlcaller class
- if has hard test/external dependencies (like real http traffic), separate these new class , provide way of replacing collaborating object fake (you can using constructor injection or subclass test pattern, example)
- provide production implementation collaborators in production code
- provide fake implementation in test code test system under test without nasty external dependencies.
for concrete case: seems external dependency makes code hard test httpclient. try extracting dependency on class out of urlcaller. if seems moot point ("but urlcaller class not doing anything"), drop urge unit test urlcaller class in isolation , depend on integration test makes real http call.
Comments
Post a Comment