1)pom.xml引入java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2)基類編寫:web
package com.demo.quickStart; import com.ss.quickStart.QuickStartApplication; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringRunner.class) @SpringBootTest(classes = QuickStartApplication.class) @Transactional @Rollback public class BaseJunitTests { }
package com.demo.quickStart.dao; import com.demo.quickStart.BaseJunitTests; import com.ss.quickStart.dao.UserMapper; import com.ss.quickStart.domain.User; import com.ss.quickStart.domain.dto.UserOrdersDTO; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; public class UserMapperTest extends BaseJunitTests { @Autowired private UserMapper userMapper; @Test public void testQryUserOrders3(){ UserOrdersDTO userOrdersDTO = userMapper.qryUserOrders3(1L); Assert.assertTrue(userOrdersDTO != null && userOrdersDTO.getOrderList().size() > 0); } @Test public void testAdd(){ User user = new User(); user.setName("小王"); user.setSex(1); int count = userMapper.insert(user); Assert.assertTrue(count == 1); } }
測試結果以下圖:spring
package com.szhtxx.etcloud.ovat.controller; import com.alibaba.fastjson.JSON; import com.szhtxx.etcloud.ovat.api.dto.OvatBillRuleDTO; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class OvatBillRuleControllerTest { @Autowired private MockMvc mvc; @Test public void testAdd() throws Exception{ OvatBillRuleDTO ruleDTO = new OvatBillRuleDTO(); ruleDTO.setRuleSchemaCode("rule_001"); ruleDTO.setRuleSchemaName("wsy拆合規則001"); ruleDTO.setDetailSplitRule("0"); ruleDTO.setDetailMergeRule("0"); ruleDTO.setListRule("0"); ruleDTO.setListLimitSpecial(9999); ruleDTO.setListLimitGeneral(9999); ruleDTO.setListLimitElectron(9999); ruleDTO.setNegativeDetailRule("1"); ruleDTO.setDeviationRule("0"); mvc.perform(post("/ovatBillRule/add") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(JSON.toJSONString(ruleDTO))) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)); } @Test public void testQryAllRule() throws Exception{ mvc.perform(get("/ovatBillRule/qryAllRule") .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)); } @Test public void testQryRuleInfo() throws Exception{ mvc.perform(get("/ovatBillRule/qryRuleInfo") .contentType(MediaType.APPLICATION_JSON_UTF8) .param("ruleCode","rule_001")) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)); } @Test public void testDelRule() throws Exception{ mvc.perform(get("/ovatBillRule/delRule") .contentType(MediaType.APPLICATION_JSON_UTF8) .param("ruleSchemaId","5aa5f4d63415ae521cecdf8d")) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)); } }