首页 > 学院 > 开发设计 > 正文

spring MVC中controller层和service层的junit4测试

2019-11-08 00:37:42
字体:
来源:转载
供稿:网友

1.controller层,调用请求路径

import static org.sPRingframework.test.web.servlet.request.MockMvcRequestBuilders.*;import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;import javax.annotation.Resource;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;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.web.context.WebapplicationContext;import sz.model.api.EmailSendApiInput;import com.mars.fw.util.json.JsonUtil;@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration(locations = { "classpath:config/spring-mvc.xml", "classpath:config/spring-resources.xml", "classpath:config/spring-application.xml", "classpath:config/spring-security.xml" })public class EmailSendApiControllerTest { @Resource WebApplicationContext wac; MockMvc mockMvc; @Before public void setup() { this.mockMvc = webAppContextSetup(this.wac).build(); } @Test public void emailSendTest2() throws Exception { //为参数做些测试数据 EmailSendApiInput input = new EmailSendApiInput(); input.setUserId("123"); input.setPassWord("123"); //转成json String json = JsonUtil.toJson(input); mockMvc.perform( (post("/email/send") .contentType(MediaType.APPLICATION_JSON).content(json))) .andExpect(status().isOk()).andDo(print()); }}

2.service层

package sz.service.email;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import sz.model.email.emailsend.EmailSendInput;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:config/spring-resources.xml", "classpath:config/spring-application.xml" })public class EmailSendServiceTest { //要测试的service @Resource SendEmailService emailSendService; @Test public void testExcute1() { //测试数据 EmailSendInput input = new EmailSendInput(); input.setCharset("UTF-8"); input.setAttId("A2016121200000001"); i try { //调用service中的方法 emailSendService.excute(input); }catch(Exception e) { System.out.println(); } }}

附:可能用到的jar

<!-- junit start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- junit end --><dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency>

注意:如果是Maven项目,当执行Maven install时,可能会报错误,造成不能正确生成war包。此时需要在pom.xml中加入如下配置:

<build> <plugins> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <additionalProjectnatures> <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> </additionalProjectnatures> <additionalBuildcommands> <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> </additionalBuildcommands> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>org.test.int1.Main</mainClass> </configuration> </plugin> </plugins> </build>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表