在Spring Boot中运行Junit测试用例时进行初始化

时间:2019-07-02 11:54:43

标签: java spring-boot junit

我正在为Spring Boot应用程序编写服务和util类的测试用例。以下是测试用例的片段

FooServiceImplTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class FooServiceImplTest {

    @Test
    //test cases for FooServiceImplTest
}

BarUtilTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class BarUtilTest {

    @Test
    //test cases for BarUtilTest
}

现在,当我运行测试用例时,在控制台中会看到类似以下内容的

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

17:21:07.835 [main] INFO  c.c.l.s.impl.FooServiceImplTest - Starting FooServiceImplTest on Users-MacBook-Pro.local with PID 8944 (started by user in /Users/user/Documents/my-project)
17:21:07.836 [main] DEBUG c.c.l.s.impl.FooServiceImplTest - Running with Spring Boot v2.1.0.RELEASE, Spring v5.1.2.RELEASE
17:21:07.836 [main] INFO  c.c.l.s.impl.FooServiceImplTest - The following profiles are active: prod
17:21:10.828 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-2 - Starting...
17:21:10.890 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-2 - Start completed.

...
// runs test cases for FooServiceImplTest

17:21:33.643 [main] INFO  c.c.l.s.impl.FooServiceImplTest - Started FooServiceImplTest in 40.898 seconds (JVM running for 102.231)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

17:21:49.290 [main] INFO  c.c.l.util.BarUtilTest - Starting BarUtilTest on Users-MacBook-Pro.local with PID 8944 (started by user in /Users/user/Documents/my-project)
17:21:49.290 [main] DEBUG c.c.l.util.BarUtilTest - Running with Spring Boot v2.1.0.RELEASE, Spring v5.1.2.RELEASE
17:21:49.290 [main] INFO  c.c.l.util.BarUtilTest - The following profiles are active: prod
17:21:50.809 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-3 - Starting...
17:21:50.972 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-3 - Start completed.

...
// runs test cases for BarUtilTest
17:22:04.765 [main] INFO  c.c.l.util.BarUtilTest - Started BarUtilTest in 30.635 seconds (JVM running for 133.353)

因此,对于我编写的每个测试套件来说,一切都将再次初始化。这可能是随着我添加更多测试套件而花费大量时间来运行测试用例的原因之一。无论如何,是否要一次初始化所有内容并一次运行所有测试用例?

0 个答案:

没有答案