my junit test case failing. error occurring @ line
mockito.when(blogentryservice.find(1l)).thenreturn(entry);
failure trace
java.lang.nullpointerexception @ com.sample.controller.blogentrycontrollertest.getexistingblogentry(blogentrycontrollertest.java:72) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ 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:74) @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:83) @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:72) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:233) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:87) @ 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:71) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:176) @ 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)
it seems me blogentryservice null
my code
/** * todo - describe purpose , operation of class. * * <table border="1" cellpadding="0" cellspacing="0" width="100%"> * <caption align="center">edit , version history</caption> * <tr><th>version</th><th>date</th><th>author</th><th>description</th></tr> * <tr><td>1.0</td><td>jan 17, 2016</td><td>eov537</td><td>initial creation.</td></tr> * </table> */ package com.sample.controller; import static org.springframework.test.web.servlet.result.mockmvcresulthandlers.print; import static org.springframework.test.web.servlet.result.mockmvcresultmatchers.forwardedurl; import org.hamcrest.matchers; import org.junit.before; import org.junit.test; import org.junit.runner.runwith; import org.mockito.injectmocks; import org.mockito.mock; import org.mockito.mockito; import org.mockito.mockitoannotations; import org.springframework.beans.factory.annotation.autowired; import org.springframework.http.mediatype; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.web.webappconfiguration; import org.springframework.test.web.servlet.mockmvc; import org.springframework.test.web.servlet.request.mockmvcrequestbuilders; import org.springframework.test.web.servlet.result.mockmvcresultmatchers; import org.springframework.test.web.servlet.setup.mockmvcbuilders; import org.springframework.web.context.webapplicationcontext; import com.sample.config.applicationconfig; import com.sample.model.blogentry; import com.sample.service.blogentryservice; /** * @author eov537 - * @since 1.0 */ @runwith(springjunit4classrunner.class) @contextconfiguration(classes = {applicationconfig.class}) @webappconfiguration public class blogentrycontrollertest { private mockmvc mockmvc; @injectmocks private blogentrycontroller blogentryconttroller; @mock private blogentryservice blogentryservice; @autowired private webapplicationcontext appctx; @before public void setup() { mockitoannotations.initmocks(blogentrycontrollertest.class); mockmvc = mockmvcbuilders.webappcontextsetup(appctx).build(); } @test public void getexistingblogentry() throws exception { blogentry entry = new blogentry(); entry.setid(1l); entry.settitle("test title"); mockito.when(blogentryservice.find(1l)).thenreturn(entry); mockmvc.perform(mockmvcrequestbuilders.get("/rest/blog-entries/1")) .andexpect(mockmvcresultmatchers.jsonpath("$.title", matchers.is("test title"))) .andexpect( mockmvcresultmatchers.jsonpath("$.links[*].href", matchers.hasitem(matchers.endswith("/blog-entries/1")))) .andexpect(mockmvcresultmatchers.status().isok()); } public void getnonexistingblogentry() throws exception { mockito.when(blogentryservice.find(1l)).thenreturn(null); mockmvc.perform(mockmvcrequestbuilders.get("/rest/blog-entries/1")).andexpect( mockmvcresultmatchers.status().isnotfound()); } }
blogentrycontroller.java
package com.sample.controller; import org.springframework.http.httpstatus; import org.springframework.http.responseentity; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.bind.annotation.restcontroller; import com.sample.assembler.blogentryresourceasm; import com.sample.model.blogentry; import com.sample.resource.blogentryresource; import com.sample.service.blogentryservice; /** * @author eov537 - * @since 1.0 */ @controller @requestmapping(value = "/rest/blog-enteries") public class blogentrycontroller { public blogentrycontroller() { } public blogentrycontroller(blogentryservice blogentryservice) { this.blogentryservice = blogentryservice; } private blogentryservice blogentryservice; @requestmapping(value = "/{blogentryid}", method = requestmethod.get) public responseentity<blogentryresource> getexsitingblogentry(@pathvariable long blogentryid) { blogentry entry = blogentryservice.find(blogentryid); if (entry != null) { blogentryresource res = new blogentryresourceasm().toresource(entry); return new responseentity<blogentryresource>(res, httpstatus.ok); } else { return new responseentity<blogentryresource>(httpstatus.not_found); } } }
blogentryservice.java
package com.sample.service; import org.springframework.stereotype.component; import com.sample.model.blogentry; /** * @author eov537 - * * @since 1.0 */ public interface blogentryservice { public blogentry find(long id); }
blogentryresource.java
package com.sample.resource; import org.springframework.hateoas.resourcesupport; /** * @author eov537 - * @since 1.0 */ public class blogentryresource extends resourcesupport { private string title; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } }
blogentryresourceasm.java
public class blogentryresourceasm extends resourceassemblersupport<blogentry, blogentryresource> { /** * @param controllerclass * @param resourcetype */ public blogentryresourceasm() { super(blogentrycontroller.class, blogentryresource.class); // todo auto-generated constructor stub } /* * (non-javadoc) * * @see org.springframework.hateoas.resourceassembler#toresource(java.lang.object) */ @override public blogentryresource toresource(blogentry blogentry) { blogentryresource res = new blogentryresource(); res.settitle(blogentry.gettitle()); link link = controllerlinkbuilder.linkto(blogentrycontroller.class).slash(blogentry.getid()).withselfrel(); return res; } }
applicationconfig.java
/** * todo - describe purpose , operation of class. * * <table border="1" cellpadding="0" cellspacing="0" width="100%"> * <caption align="center">edit , version history</caption> * <tr><th>version</th><th>date</th><th>author</th><th>description</th></tr> * <tr><td>1.0</td><td>jan 17, 2016</td><td>eov537</td><td>initial creation.</td></tr> * </table> */ package com.sample.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.viewresolver; import org.springframework.web.servlet.config.annotation.defaultservlethandlerconfigurer; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.config.annotation.resourcehandlerregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; import org.springframework.web.servlet.view.internalresourceviewresolver; /** * @author eov537 - * @since 1.0 */ @configuration @enablewebmvc @componentscan(basepackages = {"com.sample"}) public class applicationconfig extends webmvcconfigureradapter { private static final string view_resolver_prefix = "/web-inf/jsp/"; private static final string view_resolver_suffix = ".jsp"; @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/static/**").addresourcelocations("/static/"); } @override public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) { configurer.enable(); } @bean public viewresolver viewresolver() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); // viewresolver.setviewclass(internalresourceviewresolver.class); // nosonar viewresolver.setprefix(view_resolver_prefix); viewresolver.setsuffix(view_resolver_suffix); return viewresolver; } }
webapplint.java
/** * todo - describe purpose , operation of class. * * <table border="1" cellpadding="0" cellspacing="0" width="100%"> * <caption align="center">edit , version history</caption> * <tr><th>version</th><th>date</th><th>author</th><th>description</th></tr> * <tr><td>1.0</td><td>jan 17, 2016</td><td>eov537</td><td>initial creation.</td></tr> * </table> */ package com.sample.config; import javax.servlet.servletcontext; import javax.servlet.servletexception; import javax.servlet.servletregistration; import org.springframework.web.webapplicationinitializer; import org.springframework.web.context.contextloaderlistener; import org.springframework.web.context.support.annotationconfigwebapplicationcontext; import org.springframework.web.servlet.dispatcherservlet; /** * @author eov537 - * @since 1.0 */ public class webapplint implements webapplicationinitializer { /* * (non-javadoc) * * @see org.springframework.web.webapplicationinitializer#onstartup(javax.servlet.servletcontext) */ @override public void onstartup(servletcontext servletcontext) throws servletexception { annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext(); rootcontext.register(applicationconfig.class); servletregistration.dynamic registration = servletcontext.addservlet("dispatcherservlet", new dispatcherservlet(rootcontext)); registration.addmapping("/"); registration.setloadonstartup(1); servletcontext.addlistener(new contextloaderlistener(rootcontext)); } }
private mockmvc mockmvc; @autowired @injectmocks private blogentrycontroller blogentryconttroller; @autowired private webapplicationcontext appctx; @mock blogentryservice blogentryservice; @before public void setup() { mockitoannotations.initmocks(blogentrycontrollertest.this); mockmvc = mockmvcbuilders.webappcontextsetup(appctx).build(); } @test public void getexistingblogentry() throws exception { blogentry entry = new blogentry(); entry.setid(1l); entry.settitle("test title"); mockito.when(blogentryservice.find(1l)).thenreturn(entry); mockmvc.perform(mockmvcrequestbuilders.get("/rest/blog-enteries/1")) .andexpect(mockmvcresultmatchers.status().isok()); }
Comments
Post a Comment