springboot 单元测试-各个模块举例
controller单测
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(DemoFacade.class)
@RunWith(MockitoJUnitRunner.class)
public class DemoFacadeTest {
@InjectMocks
private DemoFacade demoFacade;
@Autowired
private MockMvc mockMvc;
@Mock
private DemoQryAPI demoQryAPI; // DemoFacade 里的注入的一个类
@Before
public void init() {
// 开启mock
MockitoAnnotations.initMocks(this);
// 测试方法执行前,初始化MockMvc
mockMvc = MockMvcBuilders.standaloneSetup(demoFacade).build();
}
@Autowired
private ObjectMapper objectMapper;
@SneakyThrows
@Test
public void selectAll() {
PaginationResult<List<DemoQryResp>> obj2 = new PaginationResult();
Mockito.when(demoQryAPI.selectAll(Mockito.any())).thenReturn(obj2);
obj2.setCode("000000");
String contentParam = "{}"; //json
mockMvc .perform(MockMvcRequestBuilders.post("/xx/select")
.contentType(MediaType.APPLICATION_JSON)
.content(contentParam)
)
.andExpect(status().isOk()) ;
}
}
mapper单测
测试自定义的sql对不对
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import static org.junit.Assert.*;
//需要加载的配置
@SpringBootTest(classes = {DataSourceAutoConfiguration.class
, MybatisPlusAutoConfiguration.class,
DataSource.class, SqlSessionFactory.class})
@MapperScan(basePackageClasses = DemoMapper.class)
@RunWith(SpringRunner.class)
@ActiveProfiles("mysql") // application-mysql.yml
public class DemoMapperTest {
@Autowired
private DemoMapper demoMapper;
@Test
public void flushCheckingDetail() {
demoMapper.flushCheckingDetail();
}
}
普通类的单测
@RunWith(MockitoJUnitRunner.class)
public class DemoTest {
@InjectMocks
Demo demo;
/**
* 测试
*/
@Test
public void cal() {
List<Integer> list=new ArrayList<>();
list.add(2);
list.add(2);
Integer sum = demo.cal(list);
Assertions.assertThat(sum).isEqualTo(4);
}
}
注意:单测主要是测这个方法行不行,集中在这个最小单元,测试快熟,有利于代码的迭代升级,减少bug