1.pom.xml
<!-- 单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2.目录结构
3.service层测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestUserService {
private Logger log = LoggerFactory.getLogger(TestUserService.class);
@Resource
private UserService userService;
@Before
public void before(){
log.info("test start");
}
@Test
public void test(){
System.out.println(userService.queryUserById(1));
}
@After
public void after(){
log.info("test end");
}
}
4.controller层测试
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestUserController {
private Logger log = LoggerFactory.getLogger(TestUserController.class);
@Autowired
private MockMvc mockMvc;
@Before
public void before(){
log.info("test start");
}
@Test
public void test() throws Exception{
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/1")).
andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
log.info("响应状态" + mvcResult.getResponse().getStatus());
log.info("响应内容" + mvcResult.getResponse().getContentAsString());
}
@After
public void after(){
log.info("test end");
}
}
标签:info,log,void,单元测试,public,test,class
From: https://www.cnblogs.com/lwx11111/p/16833270.html