Spring MVC 应用程序 Junit 测试用例失败
Posted
技术标签:
【中文标题】Spring MVC 应用程序 Junit 测试用例失败【英文标题】:Spring MVC application Junit test case failing 【发布时间】:2016-04-30 21:45:08 【问题描述】:我的 JUnit 测试用例失败了。行发生错误
Mockito.when(blogEntryService.find(1L)).thenReturn(entry);
故障跟踪是
java.lang.NullPointerException 在 com.sample.controller.BlogEntryControllerTest.getExistingBlogEntry(BlogEntryControllerTest.java:72) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)在 java.lang.reflect.Method.invoke(未知来源)在 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)
在我看来 blogEntryService 是空的
我的代码是
/**
* TODO - Describe purpose and operation of class.
*
* <table border="1" cellpadding="0" cellspacing="0" >
* <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);
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 and operation of class.
*
* <table border="1" cellpadding="0" cellspacing="0" >
* <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;
WebApplint.java
/**
* TODO - Describe purpose and operation of class.
*
* <table border="1" cellpadding="0" cellspacing="0" >
* <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));
【问题讨论】:
【参考方案1】: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());
【讨论】:
我要使用注解@Mock private BlogEntryService blogEntryService;如果您看到我已经上过的课程,那是不是这种方法不正确? 使用您的解决方案 BlogEntryService blogEntryService = Mockito.mock(BlogEntryService.class); Mockito 给了我代理实现,我不需要添加实现和初始化,但我不明白为什么@mock 不起作用。 我确实初始化了,看看我的代码 MockitoAnnotations.initMocks(BlogEntryControllerTest.class); 使用您的解决方案,我可以前进 1 步,但在测试执行后仍然失败 2016-01-24 11:21:21 DEBUG TestDispatcherServlet:984 - 无法完成请求 java.lang。 com.sample.controller.BlogEntryController.getExsitingBlogEntry(BlogEntryController.java:49) 处的 NullPointerException 我没有服务实现,这就是我使用模拟的原因。这是我的存储库的链接github.com/satyajitn/rest-service 我正在做测试驱动开发,所以在服务实现之前编写测试用例【参考方案2】:在 BlogEntryController() 中,blogEntryService 永远不会用值初始化。在您的 @Before (setup()) 方法中,初始化 blogEntryService:
blogEntryService = new BlogEntryService();
【讨论】:
blogEntryService 是接口,无法解析为变量。请看代码【参考方案3】:Rohit 的上述回答帮助并解决了最初的模拟问题,这是我尝试过的替代方案并且它有效,但它只是替代方案而不是解决方案。
我评论了下面一行
//@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();
改为 StandaloneSetup 而不是 webAppContextSetup 然后
更新了 BlogEntryController 并删除了默认构造函数
// public BlogEntryController()
//
现在只有 1 个构造函数
公共 BlogEntryController(BlogEntryService blogEntryService)
this.blogEntryService = blogEntryService;
现在,当我执行测试时,模拟的 blogEntryService 正在正确注入。
当我使用 webAppContextSetup 时,我必须保留默认构造函数 博客条目控制器。因为它有默认构造函数,所以当我运行测试时 默认构造函数被执行并且 blogEntryService 保持为空并且测试失败。
即使我能够做到这一点,这也不是解决方案,它只是一种替代方案。
如果有人知道如何通过 webAppContextSetup 解决此问题,请发布解决方案。
【讨论】:
以上是关于Spring MVC 应用程序 Junit 测试用例失败的主要内容,如果未能解决你的问题,请参考以下文章
如何JUnit测试@PreAuthorize注释及其由spring MVC控制器指定的spring EL?