我的 JUnit 测试用例失败了。线路出错
Mockito.when(blogEntryService.find(1L)).thenReturn(entry);
故障跟踪为
java.lang.NullPointerException at com.sample.controller.BlogEntryControllerTest.getExistingBlogEntry(BlogEntryControllerTest.java:72) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at 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.statement.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.statement.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) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在 org.springframework.test.context.junit4.statement.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at 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)
在我看来,博客入口服务是空
的我的代码是
/**
* TODO - Describe purpose and operation of class.
*
* <table border="1" cellpadding="0" cellspacing="0" width="100%">
* <caption align="center">Edit and 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);
}
博客条目资源.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;
}
}
应用程序配置.java
/**
* TODO - Describe purpose and operation of class.
*
* <table border="1" cellpadding="0" cellspacing="0" width="100%">
* <caption align="center">Edit and 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;
}
}
网络应用.java
/**
* TODO - Describe purpose and operation of class.
*
* <table border="1" cellpadding="0" cellspacing="0" width="100%">
* <caption align="center">Edit and 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());
}
从不使用值初始化。 在@Before (setup()) 方法中,初始化 blogEntryService:
blogEntryService = new BlogEntryService();
上面的罗希特的答案帮助并解决了以下原始嘲笑问题,这是我尝试过的替代方案,它有效,但它只是替代方案而不是解决方案。
我评论了以下行
//@RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration(classes = {ApplicationConfig.class})
来自 BlogEntryControllerTest.java
我将 setup() 方法从 BlogEntryControllerTest 更改为
@Before public void setup() { // blogEntryService = Mockito.mock(BlogEntryService.class); MockitoAnnotations.initMocks(this); mockMvc = MockMvcBuilders.standaloneSetup(blogentryconttroller).build(); //mockMvc = MockMvcBuilders.webAppContextSetup(appCtx).build(); }
而不是webAppContextSetup,而是更改为StandaloneSetup然后
更新了博客条目控制器并删除了默认构造函数
//public BlogEntryController() {
//}
现在只有 1 个构造函数
public BlogEntryController(BlogEntryService blogEntryService) {
this.blogEntryService = blogEntryService; }
现在,当我执行测试模拟博客入口服务时,正在正确注入。
当我使用 webAppContextSetup 时,我必须保留默认构造函数博客入口控制器。由于它具有默认构造函数,因此当我运行测试时默认构造函数被执行,而blogEntryService保持为空,测试失败。
即使我能够让它工作,这不是解决方案,它只是一种替代方案。
如果有人知道如何通过webAppContextSetup解决此问题,请发布解决方案。