unit testing - Getting java.lang.NullPointerException for mvc.perform() -
i new unit testing in spring boot. trying test service , have used mockito it. when mvc.perform() null value. have used when() specify return. not sure why fails. please provide valuable inputs. here fe snippets:
@test public void testall() throws exception { /*mockito.when(sensordao.findall()).thenreturn((iterable<sensor>) s2); assertequals(200,expect().statuscode(200));*/ collection<sensor>list = getentityliststubdata(); when(sensorserviceimpl.findall()).thenreturn(list); when(sensorservice.findall()).thenreturn(list); when(sensordao.findall()).thenreturn(list); string uri="/sensor"; //system.out.print("gfnjhkjjhhg"); system.out.println("helllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllloooooooooooooooooooooooooooooooooooooooooooo"); system.out.println(mockmvcrequestbuilders.get(uri).accept(mediatype.application_json)); mvcresult result = mvc.perform(mockmvcrequestbuilders.get(uri).accept(mediatype.application_json)).andreturn(); string content = result.getresponse().getcontentasstring(); int status = result.getresponse().getstatus(); }
here output:
2017-08-18 19:11:45.030 info 21424 --- [ main] o.s.t.c.transaction.transactioncontext : rolled transaction test context [defaulttestcontext@35083305 testclass = sensorservicestandaloneapplicationtests, testinstance = com.bny.sr.sensorservicestandalone.sensorservicestandaloneapplicationtests@482cd91f, testmethod = testall@sensorservicestandaloneapplicationtests, testexception = java.lang.nullpointerexception, mergedcontextconfiguration = [webmergedcontextconfiguration@8e0379d testclass = sensorservicestandaloneapplicationtests, locations = '{}', classes = '{class com.bny.sr.sensorservicestandalone.sensorservicestandaloneapplication}', contextinitializerclasses = '[]', activeprofiles = '{}', propertysourcelocations = '{}', propertysourceproperties = '{eureka.client.enabled:false, org.springframework.boot.test.context.springboottestcontextbootstrapper=true, server.port=0}', contextcustomizers = set[org.springframework.boot.test.context.springboottestcontextcustomizer@167fdd33, org.springframework.boot.test.context.filter.excludefiltercontextcustomizer@53f65459, org.springframework.boot.test.json.duplicatejsonobjectcontextcustomizerfactory$duplicatejsonobjectcontextcustomizer@74650e52, org.springframework.boot.test.mock.mockito.mockitocontextcustomizer@0, org.springframework.boot.test.autoconfigure.properties.propertymappingcontextcustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.webdrivercontextcustomizerfactory$customizer@80503], resourcebasepath = 'src/main/webapp', contextloader = 'org.springframework.boot.test.context.springbootcontextloader', parent = [null]]]. java.lang.nullpointerexception @ com.bny.sr.sensorservicestandalone.sensorservicestandaloneapplicationtests.testall(sensorservicestandaloneapplicationtests.java:163) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26) @ org.springframework.test.context.junit4.statements.runbeforetestmethodcallbacks.evaluate(runbeforetestmethodcallbacks.java:75) @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:86) @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:84) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:325) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:252) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:94) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:191) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:68) @ com.intellij.rt.execution.junit.ideatestrunner$repeater.startrunnerwithargs(ideatestrunner.java:47) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:242) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:70) 2017-08-18 19:11:45.046 info 21424 --- [ thread-5] ationconfigembeddedwebapplicationcontext : closing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@5386659f: startup date [fri aug 18 19:11:34 ist 2017]; root of context hierarchy 2017-08-18 19:11:45.054 info 21424 --- [ thread-5] j.localcontainerentitymanagerfactorybean : closing jpa entitymanagerfactory persistence unit 'default' process finished exit code -1
here debug result:
since spring-boot 1.4.0
can use @webmvctest
auto-configure mockmvc
. also, use @mockbean
mock dependency in controller.
@runwith(springrunner.class) @webmvctest(mycontroller.class) public class mycontrollertest { @autowired private mockmvc mvc; @mockbean private sensordao sensordao; }
Comments
Post a Comment