SpringBoot 2.6 和 JUnit 5 的测试用例注解和排序方式
JUnit5 的测试注解
在JUnit5中, 不再使用 @RunWith 注解, 改为使用 @ExtendWith(SpringExtension.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class AccountTest {
@Resource
AccountMapper accountMapper;
@Test
void test1_list() {
System.out.println("hello");
List<AccountDTO> accounts = accountMapper.list(new HashMap<>());
System.out.println(accounts.size());
}
}
对比JUnit4的测试注解
在JUnit4中, 使用的测试注解(非SpringBoot)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/spring-commons.xml"})
public class IpBlacklistTest {
private static final Logger logger = LoggerFactory.getLogger(IpBlacklistTest.class);
@Resource(name="sessionTokenDTOService")
private SessionTokenDTOService sessionTokenDTOService;
@Test
public void test() {
long from = TimeUtil.shiftByHour(new Date(), 48, 0).getTime();
ResultDTO result = sessionTokenDTOService.resetIpBlackList(from, 100);
System.out.println(result.getMessage());
}
}
在SrpingBoot中为
@RunWith(SpringRunner.class)
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RabbitMQTest {
private static Logger logger = LoggerFactory.getLogger(RabbitMQTest.class);
private static final TimeflakeId SFID = new TimeflakeId(100);
@Resource(name="requestPOService")
private RequestPOService requestPOService;
@Autowired
private SysConfig sysConfig;
@Test
public void test1_freeze() {
EsbEntity<Head, Freeze> request = requestPOService.genPiaFreezRequest(302040, 100);
requestPOService.send(JacksonUtils.compressObject(request));
}
}
测试用例的顺序
https://www.baeldung.com/junit-5-test-order
In JUnit 5, we can use @TestMethodOrder to control the execution order of tests.
排序方式有
- Alphanumeric
- OrderAnnotation
- Random
- Custom Order
按方法字符排序
@TestMethodOrder(MethodOrderer.MethodName.class)
public class AlphanumericOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
output.append("A");
}
@Test
public void myBTest() {
output.append("B");
}
@Test
public void myaTest() {
output.append("a");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ABa");
}
}
使用 @Order 注解排序
@TestMethodOrder(OrderAnnotation.class)
public class OrderAnnotationUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
@Order(1)
public void firstTest() {
output.append("a");
}
@Test
@Order(2)
public void secondTest() {
output.append("b");
}
@Test
@Order(3)
public void thirdTest() {
output.append("c");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "abc");
}
}
使用随机排序
使用 MethodOrderer.Random 实现的伪随机排序测试
@TestMethodOrder(MethodOrderer.Random.class)
public class RandomOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
output.append("A");
}
@Test
public void myBTest() {
output.append("B");
}
@Test
public void myCTest() {
output.append("C");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ACB");
}
}
使用自定义排序
public class CustomOrder implements MethodOrderer {
@Override
public void orderMethods(MethodOrdererContext context) {
context.getMethodDescriptors().sort(
(MethodDescriptor m1, MethodDescriptor m2)->
m1.getMethod().getName().compareToIgnoreCase(m2.getMethod().getName()));
}
}
@TestMethodOrder(CustomOrder.class)
public class CustomOrderUnitTest {
// ...
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "AaB");
}
}