国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 锡林郭勒盟| 五寨县| 顺义区| 合阳县| 钦州市| 娄烦县| 和龙市| 靖江市| 江西省| 赞皇县| 西华县| 安徽省| 荆门市| 手游| 连云港市| 溆浦县| 福泉市| 利津县| 英山县| 镇康县| 昌江| 楚雄市| 古浪县| 二连浩特市| 清水县| 三原县| 乐山市| 常山县| 阳谷县| 独山县| 佛冈县| 广元市| 商都县| 宝兴县| 宾川县| 紫阳县| 库车县| 台中县| 上栗县| 延吉市| 正宁县|