1 SpringMVC测试
1.1 引言
为了测试WEB
案例通常不想启动项目,我们需要配置一些Servlet
相关的模拟对象,比如:MockMVC、MockHttpSession、MockHttpServletRequest、MockHttpServletResponse
等
在Spring
里面使用@WebAppConfiguration
来指定加载的ApplicationContext
是一个WebApplicationContext
这个注解作用在类上,用来说明加载的Application Context
是一个WebApplicationContext
,它的属性指定的是web
资源位置,默认为src/main/webapp
1.2 代码示例
1.2.1 pom依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
1.2.2 service示例
package cn.jzh.service;
import org.springframework.stereotype.Service;
@Service
public class JspService {
public String saySomething(){
return "hello";
}
}
1.2.3 测试controller
package cn.jzh.controller;
import cn.jzh.service.JspService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@Autowired
private JspService servcie;
@RequestMapping("/normal")
public String testPage(Model model){
model.addAttribute("msg",servcie.saySomething());
return "page";
}
//此处是在此处指定响应的是json类型
@RequestMapping(value = "/rest",produces = "text/plain;charset=UTF-8")
public @ResponseBody String testRest(){
return servcie.saySomething();
}
}
1.2.4 测试相关示例
package cn.test;
import cn.jzh.config.MyMvcConfig;
import cn.jzh.service.JspService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
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.MockMvcBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyMvcConfig.class})
//注解WebAppConfiguration作用在类上,用来说明加载的Application Context是一个WebApplicationContext
// 它的属性指定的是web资源位置,默认为src/main/webapp
@WebAppConfiguration
public class TestController {
private MockMvc mockMvc;//主要用来模拟mvc对象
@Autowired
private JspService service;
@Autowired
private WebApplicationContext context;
@Before
public void testBefore(){
this.mockMvc= MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void normalController() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get("/normal")) //模拟向normal发送正常请求
.andExpect(status().isOk()) //预期控制返回状态为200
.andExpect(view().name("page")) //预期返回页面的名字为page
.andExpect(forwardedUrl("/WEB-INF/views/page.jsp")) //预期页面跳转路径
.andExpect(model().attribute("msg",service.saySomething()));//预期返回页面内容
}
@Test
public void restController() throws Exception{
mockMvc.perform(get("/rest")) //模拟向normal发送正常请求
.andExpect(status().isOk()) //预期控制返回状态为200
.andExpect(content().contentType("text/plain;charset=UTF-8")) //预期返回页面的编码格式
.andExpect(content().string(service.saySomething())); //预期返回页面内容
}
}
1.2.5 测试报错ExceptionInInitializerError
java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:31)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:104)
... 15 more
解决方法:
查询资料是Junit
的版本问题,而我所使用的spring
的版本是5.2.9.RELEASE
,而Junit
的版本是4.11
,而错误日志上说明我们要使用SpringJunit
的4.12
或者是更高的版本,所以在更换Junit
的版本为4.12
后,就没有出现这个错误了
网友评论