答案:
org.junit.jupiter.api.Test 是Junit5 的
org.junit.Test 是Junit4.
建议Junit5
答案:
@RunWith 是Junit4的东西
@SpringBootTest 是spring framework 里的东西
如果使用Junit4, 两者都需要, 如果使用Junit5, 只使用@SpringBootTest就够了, 使用@RunWith反而会报错
org.junit.runners.model.InvalidTestClassError: Invalid test class 'cn.itcast.order.TestCase1':
1. No runnable methods
at org.junit.runners.ParentRunner.validate(ParentRunner.java:525)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:92)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:74)
答案:
@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "classpath:/app-config.xml"
@ContextConfiguration("/app-config.xml")
参考:
https://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles
答案:
@SpringBootTest()
@ActiveProfiles("uat")
public class TestCase1 {
@Before 会在每次测试方法前执行
@BeforeClass 会在单元测试类被执行时执行, for 1个class只会执行一次
上面两个注解是Junit4的
到了Junit5
改成了
@BeforeEach
@BeforeAll
实际例子:
Spring Cloud config 需要的ENCRYPT_KEY 变量
答案:
本人已經在google查過很多方法。
唯一有效方法的就是放弃这个配置放入项目代码:
solution:
IDE: 为每一次测试用力加上环境变量参数

自动化:
在执行mvn test之前
设置环境变量
例如:
set "ENCRYPT_KEY=xxxx" && mvn test
主要是給別人(你老闆)看的

答案:
使用@DisplayName注解
@Test
@DisplayName("dummy testing case")
public void test1(){

答案:
使用assertThrow()

答案:
使用@TestMethodOrder注解
