SpringBoot

SpringBoot单元测试

一、对Service层的测试

在对应方法上右击 goto-->Test-->Create New Test...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RunWith(SpringRunner.class)
@SpringBootTest
public class GirlServiceTest {

@Autowired
private GirlService girlService;


@Test
public void getGirls() throws Exception {
Result<Girl> girls = girlService.getGirls();
Assert.assertNotNull(girls.getData());
}

}

二、对controller层的测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class GirlControllerTest {

@Autowired
private MockMvc mvc;

@Test
public void getGirlList() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/girls"));
//.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("abc"));
}

}
注解@SpringBootTest的底层其实是用到了单元测试。

当然还可以在打包的时候运行测试(自动):mvn clean package
打包时跳过单元测试:mvn clean package -Dmaven.test.skip=true
文章目录
  1. 1. 一、对Service层的测试
  2. 2. 二、对controller层的测试