在springboot项目中编写unittest

编程

不同的公司开发系统的时都会要求开发人员编写unit test,但是不同的公司对编写unit test的要求不尽相同。这里记录一下遇到过的编写unit test的一些写法。

1、创建项目,引入unit test相关的jar包

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

项目中会自动生成一个*ApplicationTests.java类

@SpringBootTest(classes = SpringbootUnitTestDemoApplication.class,

webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

public class SpringbootUnitTestDemoApplicationTests {

@Test

void contextLoads() {

}

}

@SpringBootTest注解的参数可按需添加

其他的*Test类可以直接继承这个*ApplicationTests.java类,如

@Slf4j

public class UserServiceImplTest extends SpringbootUnitTestDemoApplicationTests {

@Autowired

private IUserService userService;

@Test

public void getById(){

String id = "1";

User user= userService.getById(id);

log.info(">>>>>>>>>>{}",user.toString());

Assert.assertNotNull(user);

}

}

2、测试类创建方法:打开需要测试的类,在编辑窗口上"右键-->Go To-->Test",选择要测试的类,然后选择要测试的方法,确定便生成测试类

编写测试类,执行结果如下

3、以上是对类似service这类的测试,如果是对Controller接口的测试,则要使用到MockMvc了,如下

@AutoConfigureMockMvc

public class UserControllerTest extends SpringbootUnitTestDemoApplicationTests {

private MockMvc mockMvc;

@Mock

private IUserService service;

@InjectMocks

private UserController userController;

@Before

public void setUp() throws Exception{

MockitoAnnotations.initMocks(this);

}

@Test

public void getByIdTest() throws Exception {

User user = new User("1", "zhangsan", "123456", new Date());

Mockito.when(service.getById("1")).thenReturn(user);

mockMvc = MockMvcBuilders.standaloneSetup(userController).build();

ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/p1/p11")

.contentType(MediaType.APPLICATION_JSON_UTF8)

.content(JSON.toJSONString(user)));

//.andExpect(MockMvcResultMatchers.status().isOk());

String result = resultActions.andReturn().getResponse().getContentAsString();

Assert.hasText(result,JSON.toJSONString(user));

}

}

Mockito.when(service.getById("1")).thenReturn(user);这里表示遇到service.getById("1")则返回user,并不会调用service.getById("1")方法;
mockMvc = MockMvcBuilders.standaloneSetup(userController).build();表示要测试哪个controller;
MockMvcRequestBuilders.get("/p1/p11")表示要执行controller的哪个方法;
content(JSON.toJSONString(user)))表示要向controller的方法传过去的参数。

执行UserControllerTest.getByIdTest()方法结果如下

4、在配置文件配置的config flag 有真假两种情况,怎么在unit test中测试到两种情况?加入配置的默认值为真(true)

如在配置文件中配置了 feature.config.flag=true,如下

1)测试 config flag 默认为真(true)的情况时,直接读配置文件的值即可。如下

2)测试 config flag 为假(false)的情况时,可在@SpringBootTest注解中绑定 config flag 为假(false)的值,如下

5、unit test 在不同的公司主要有以下一些区别

1)对编写 unit test 的有无必要性要求不同

其主要表现在,有些公司对有无编写unit test并不是强制性的,只是建议性的写一些主要功能业务的unit test,但unit test代码仅限在保存在本地,不能提交到GitHub上,这种一般是小公司的要求,开发leader不想去管理那么多的代码;一些大公司则要求必须要写unit test的,unit test要尽可能覆盖到所有的方法,并且unit test代码是必须要提交的,提交的unit test 代码在Jenkins中打包时会跑一遍,所以unit test 代码要必须能跑得通过,leader还会去review这些代码。

2)对编写 unit test 代码的要求不同

其主要表现在,一些公司不要求写Controller api的unit test;对unit test结果的处理不同,有些公司要求在unit test方法中将结果直接输出在控制台,方便看结果,有些公司要求使用断言(Assert)来处理测试结果。

3)在现在流行前后端分离的开发方式下,测试的时候,小公司一般都是把所有的项目都跑起来,然后进行测试;大公司很多都是前后端项目中各自mock数据来测试的。

 

源码示例:https://gitee.com/lion123/springboot-unit-test-demo

 

以上是 在springboot项目中编写unittest 的全部内容, 来源链接: utcz.com/z/514903.html

回到顶部