我们先来看下笔者的单元测试的依赖版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
我用的是2.x的spring-boot,查阅的网上的资料,发现在新版本中,spring-boot用的是junit 5.x,这就发现问题了,在junit4.x版本之前,生效的是@Before主键,这没错。
然而在junit 5.x中,@Before主键被@BeforeEach所替代,因此就不生效了。