参考文档:https://blog.csdn.net/qq_35915384/article/details/80227297
1.pom.xml
一般使用idea新建一个SpringBoot web项目时,一般都会自动引入此依赖,如果没有,请手动引入。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1. spring-boot-start-test
SpringBoot提供了spring-boot-start-test启动器,该启动器提供了常见的单元测试库:
在单元测试中替代Get和Post请求测试方式
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ScoreControllerTestNew {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testScore(){
String jsonStr = "{\"data\":{\"debit_account_balance_code\":40,\"credit_consume_count\":1,\"debit_start_age\":1,\"debit_consume_sum_code\":2,\"age\":38},\"modelProductId\":55}";
String content = this.restTemplate.postForObject("/scoreApi/score", jsonStr, String.class );
assertThat(content).isEqualTo("{\"result\":{\"score\":\"300\",\"logit\":21.144779999999997},\"response_code\":\"00\",\"response_msg\":\"success\"}");
}
}
1、测试Controller
在测试Controller时需要进行隔离测试,这个时候需要Mock Service层的服务。
@RunWith(SpringRunner.class)
@WebMvcTest(ScoreController.class)
public class ScoreControllerTestNew {
@Autowired
private MockMvc mockMvc;
@MockBean
private ICalculateService calculateService;
@MockBean
private IModelMonitorService modelMonitorService;
@MockBean
private IScoreConfigService scoreConfigService;
@MockBean
private IModelProductService modelProductService;
@Before
public void setUp(){
}
@Test
public void testScore() throws Exception {
given(this.modelProductService.get(anyLong()))
.willReturn(null);
String jsonStr = "{\"data\":{\"debit_account_balance_code\":40,\"credit_consume_count\":1,\"debit_start_age\":1,\"debit_consume_sum_code\":2,\"age\":38},\"modelProductId\":5}";
RequestBuilder requestBuilder = null;
requestBuilder = post("/scoreApi/score").contentType(MediaType.APPLICATION_JSON).content(jsonStr);
this.mockMvc.perform(requestBuilder).andExpect(status().isOk()).andExpect(MockMvcResultMatchers.content().string("{}"));
}
}
2、测试Service
测试Service和测试Controller类似,同样采用隔离法。
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {
@MockBean
private ModelMonitorMapper modelMonitorMapper;
@Autowired
private IModelMonitorService modelServiceServiceImpl;
@Test
public void testModelServiceServiceImpl(){
given(modelMonitorMapper.insert(anyObject()))
.willReturn(0);
int n = modelServiceServiceImpl.insert(new ModelMonitor());
assertThat(n).isEqualTo(0);
}
}
3、测试Dao
测试的时候为了防止引入脏数据使用注解@Transactional和@Rollback在测试完成后进行回滚。
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ScoreControllerTestNew {
@Autowired
private ModelMonitorMapper modelMonitorMapper;
@Test
@Rollback
public void testDao() throws Exception {
ModelMonitor modelMonitor = new ModelMonitor();
modelMonitor.setModelProductId(Long.parseLong("5"));
modelMonitor.setLogit(21.144779999999997);
modelMonitor.setDerivedVariables("{\"debit_account_balance_code\":1.0,\"credit_consume_count\":1.0,\"debit_start_age\":1.0,\"debit_consume_sum_code\":1.0,\"age\":1.0}");
modelMonitor.setScore("300");
modelMonitor.setSrcData("{\"data\":{\"debit_account_balance_code\":40,\"credit_consume_count\":1,\"debit_start_age\":1,\"debit_consume_sum_code\":2,\"age\":38},\"modelProductId\":5}");
int n = modelMonitorMapper.insert(modelMonitor);
assertThat(n).as("检查数据是否成功插入").isEqualTo(0);
}
}