项目在投入生产之前,需要进行大量的单元测试,Spring Boot作为分布式微服务架构的脚手架,非常有必要来了解下Spring Boot如何进行单元测试。具体步骤
创建一个Spring Boot项目,
项目创建完成后,在项目的pom.xml配置文件中,可以看到Spring Boot默认已经为我们添加了spring-boot-starter-test插件,具体代码如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
spring-boot-starter-test插件依赖了spring-boot-test、junit、assertj、mockito、hamcrest等测试框架和类库。
开发用户接口UserService和实现类UserServiceImpl。
UserService接口如下:
package com.shrimpking;
import lombok.Data;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/13 21:27
*/
@Data
public class AvUser
{
private String id;
private String name;
}
package com.shrimpking;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/13 21:28
*/
public interface AvUserService
{
AvUser findUser(String id);
}
package com.shrimpking;
import org.springframework.stereotype.Component;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/13 21:29
*/
@Component
public class AvUserServiceImpl implements AvUserService
{
@Override
public AvUser findUser(String id)
{
AvUser avUser = new AvUser();
avUser.setId("1");
avUser.setName("av");
return avUser;
}
}
Spring Boot的测试类主要放置在/src/test/java目录下面。项目创建完成后,Spring Boot会自动生成测试类Tests.java。测试类的代码如下:
package com.shrimpking;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.print.attribute.standard.PrinterURI;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/13 21:30
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class AvTest
{
@Resource
private AvUserService avUserService;
@Test
public void testFindUser(){
AvUser avUser = this.avUserService.findUser("1");
//断言
Assert.assertNotNull("user is null",avUser);
}
}
@RunWith(SpringRunner.class):@RunWith(Parameterized.class)参数化运行器,配合@Parameters使用JUnit的参数化功能。查源代码可知,SpringRunner类继承SpringJUnit4ClassRunner类,此处表明使用SpringJUnit4ClassRunner执行器,此执行器集成了Spring的一些功能。如果只是简单地JUnit单元测试,该注解可以去掉。
@SpringBootTest:此注解能够测试SpringApplication,因为Spring Boot程序的入口是SpringApplication,基本上所有配置都会通过入口类去加载,而该注解可以引用入口类的配置。 @Test:JUnit单元测试的注解,注解在方法上表示一个测试方法。
JUnit框架提供的Assert断言一方面需要提供错误信息,另一方面期望值与实际值到底谁在前谁在后,很容易犯错。好在Spring Boot已经考虑到这些因素,它依赖于AssertJ类库,弥补了JUnit框架在断言方面的不足之处。我们可以轻松地将JUnit断言修改为AssertJ断言,具体代码如下:
package com.shrimpking;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.print.attribute.standard.PrinterURI;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/13 21:30
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class AvTest
{
@Resource
private AvUserService avUserService;
@Test
public void testFindUser(){
AvUser avUser = this.avUserService.findUser("1");
//断言
Assert.assertNotNull("user is null",avUser);
}
@Test
public void test2(){
AvUser avUser = this.avUserService.findUser("1");
int num = 10;
boolean success = false;
//Junit断言
Assert.assertNotNull("user is null",avUser);
Assert.assertTrue("result is not true",success);
Assert.assertEquals("num is not equal 10",10,num);
//AssertJ断言
Assertions.assertThat(avUser).isNotNull();
Assertions.assertThat(success).isTrue();
Assertions.assertThat(num).isEqualTo(10);
}
}