junit - Java Unit test for Exception -


public document query(string uri) throws ioexception, illegalargumentexception {      final httpget httpget = new httpget(uri);     final httpresponse response = httpclient.execute(httpget);     final httpentity entity = response.getentity();     document doc = null;     try     {         documentbuilderfactory factory = documentbuilderfactory.newinstance();         documentbuilder builder = factory.newdocumentbuilder();         doc = builder.parse(entity.getcontent());     }     catch (saxexception e)     {          logger.error(e);         throw new illegalargumentexception("parse error" + e);     }     catch (parserconfigurationexception e)     {          logger.error(e);         throw new illegalargumentexception("parameter factor invalid: " + e);     }     catch (illegalstateexception e)     {          logger.error(e);         throw new illegalargumentexception("null entity contetents" + e);     }      return doc; }    @test(expected = illegalargumentexception.class) public void testquery_parseexceptionthrown() throws exception {     string uri ="some uri";     easymock.expect(httpclient.execute(easymock.isa(httpget.class))).andreturn(mockresponse);     easymock.expect(mockresponse.getentity()).andreturn(mockentity);     easymock.expect(mockentity.getcontent()).andreturn(new bytearrayinputstream(repsonse_example.getbytes()));     documentbuilderfactory factory = documentbuilderfactory.newinstance();     documentbuilder builder = factory.newdocumentbuilder();       easymock.expect(builder.parse(easymock.isa(inputstream.class))).andthrow(             new illegalargumentexception("expected"));     easymock.replay();     class.query(uri);  } 

error:

java.lang.illegalstateexception: calling verify not allowed in record state     @ org.easymock.internal.mockscontrol.verify(mockscontrol.java:181)     @ org.powermock.api.easymock.internal.invocationcontrol.easymockmethodinvocationcontrol.verify(easymockmethodinvocationcontrol.java:120)     @ org.powermock.api.easymock.powermock.verify(powermock.java:1650)     @ org.powermock.api.easymock.powermock.verifyall(powermock.java:1586)     @ com.amazon.ams.test.abstractunittest.verifymocks(abstractunittest.java:78)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:606)     @ org.junit.internal.runners.methodroadie.runafters(methodroadie.java:145)     @ org.junit.internal.runners.methodroadie.runbeforesthentestthenafters(methodroadie.java:99)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.executetest(powermockjunit44runnerdelegateimpl.java:296)     @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.executetestinsuper(powermockjunit47runnerdelegateimpl.java:112)     @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.executetest(powermockjunit47runnerdelegateimpl.java:73)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runbeforesthentestthenafters(powermockjunit44runnerdelegateimpl.java:284)     @ org.junit.internal.runners.methodroadie.runtest(methodroadie.java:84)     @ org.junit.internal.runners.methodroadie.run(methodroadie.java:49)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.invoketestmethod(powermockjunit44runnerdelegateimpl.java:209)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.runmethods(powermockjunit44runnerdelegateimpl.java:148)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$1.run(powermockjunit44runnerdelegateimpl.java:122)     @ org.junit.internal.runners.classroadie.rununprotected(classroadie.java:34)     @ org.junit.internal.runners.classroadie.runprotected(classroadie.java:44)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.run(powermockjunit44runnerdelegateimpl.java:120)     @ org.powermock.modules.junit4.common.internal.impl.junit4testsuitechunkerimpl.run(junit4testsuitechunkerimpl.java:102)     @ org.powermock.modules.junit4.common.internal.impl.abstractcommonpowermockrunner.run(abstractcommonpowermockrunner.java:53)     @ org.powermock.modules.junit4.powermockrunner.run(powermockrunner.java:42)     @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50)     @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197) 

i keep getting errors

java.lang.assertionerror: expected exception: org.xml.sax.saxexception java.lang.illegalstateexception: calling verify not allowed in record state 

there 3 exceptions need write junit test exception. know how use powermock or easymock class write unit test it?

if have mock builder using easymock can throw exceptions instead of return values:

easymock.expect(builder.parse(mycontent)).andthrow( myexception); 

where myexception exception instance want throw (created new myexception(...));

edit: example test code:

@test public void parsethrowsillegalstateexception(){     //... creating mock factory, builder , entity not shown      //create new exception thrown     illegalstateexception expectedexception = new illegalstateexception("expected");      easymock.expect(mockbuilder.parse(mockcontent).andthrow(expectedexception);      easymock.replay(...);      //exercise system under test tries parse entity's content     //... } 

edit 2: posted actual test code think problem might these lines:

easymock.expect(mockentity.getcontent()).andreturn(new bytearrayinputstream(repsonse_example.getbytes()));  ... easymock.expect(builder.parse(new bytearrayinputstream(malformed_xml.getbytes()))).andthrow(new saxexception("expected")); 

i don't think bytearrayinputstream overrides equals() using object.equals(). bytearrayinputstreams won't equal easymock never throw exception

i change builder.parse() expectation to:

easymock.expect(builder.parse(easymock.isa(inputstream.class))).andthrow(new saxexception("expected")); 

which throw when parse called no matter inputstream is.

as side note, error message mentioned "calling verify not allowed in record state" don't see calls verify() or verifyall() anywhere.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -