一、springboot 2.1.6 + junit5.5.2,mock 是 springboot自带
二、pom.xml
<properties>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
</properties>
<!-- junit 5 测试 begin -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- junit 5 测试 end -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
三、相关实体
@Getter
@Setter
public class ParamModel {
private String key;
private String description;
}
public interface AddrJpa extends JpaRepository<Addr,Integer> {}
public interface PerJpa extends JpaRepository<Per,Integer> {}
四、测试例子:
@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Slf4j
public class TestMock {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private AddrJpa addrJpa;
@Autowired
private PerJpa perJpa;
@Test
@Order(3)
public void test() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/hello")
.contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
log.info("Order(3)");
log.info(mvcResult.getResponse().getContentAsString());
}
@Test
@Order(2)
public void testGetName() throws Exception {
ParamModel model = new ParamModel();
model.setKey("name");
model.setDescription("zhangsan - lisi");
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/getName")
.contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(model)))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
log.info("Order(2)");
log.info(result.getResponse().getContentAsString());
}
@Test
@Order(1)
public void testFindById(){
Optional<Per> optional = perJpa.findById(2);
Per per = optional.orElseThrow(RuntimeException::new);
log.info("order(1)");
log.info(per.getId() + ":" + per.getName() + ":" + per.getAge());
}
@Test
public void testInsertAddr(){
Per per = new Per();
per.setName("zhanglll");
per.setAge(12);
perJpa.save(per);
Addr addr = new Addr();
addr.setAddrDetails("beijing");
addr.setPer(per);
addrJpa.save(addr);
log.info("testInsertAddr,No Order.....");
}
}
@SpringBootTest@AutoConfigureMockMvc@TestMethodOrder(MethodOrderer.OrderAnnotation.class)@Slf4jpublic class TestMock {
@Autowired private MockMvc mockMvc;
@Autowired private ObjectMapper objectMapper;
@Autowired private AddrJpa addrJpa;
@Autowired private PerJpa perJpa;
@Test @Order(3) public void test() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/hello") .contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultHandlers.print()) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn(); log.info("Order(3)"); log.info(mvcResult.getResponse().getContentAsString());
}
@Test @Order(2) public void testGetName() throws Exception { ParamModel model = new ParamModel(); model.setKey("name"); model.setDescription("zhangsan - lisi"); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/getName") .contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(model))) .andDo(MockMvcResultHandlers.print()) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn(); log.info("Order(2)"); log.info(result.getResponse().getContentAsString()); }
@Test @Order(1) public void testFindById(){ Optional<Per> optional = perJpa.findById(2); Per per = optional.orElseThrow(RuntimeException::new); log.info("order(1)"); log.info(per.getId() + ":" + per.getName() + ":" + per.getAge()); }
@Test public void testInsertAddr(){ Per per = new Per(); per.setName("zhanglll"); per.setAge(12); perJpa.save(per); Addr addr = new Addr(); addr.setAddrDetails("beijing"); addr.setPer(per); addrJpa.save(addr); log.info("testInsertAddr,No Order....."); }}