SpringBoot集成单元测试

2019年12月19日 作者 刘剑
  • Junit测试出现异常:Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.commons.util.

IntelliJ IDEA正在尝试使用JUnit5运行我的测试用例, 通过pom.xml发现,使用JUnit4.12运行测试用例, 需要升级依赖

		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>5.0.0-M4</version>
			<scope>test</scope>
		</dependency>

使用JUnit4直接使用下面依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>
  • SpringBoot test配置
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({
            "classpath:applicationContext.xml", "classpath:applicationContext-db.xml"
    })
    @SpringBootTest(properties = "classpath:application.yaml")
    @Slf4j
    public class BaseDaoTest {
    
        @Before
        public void start() {
            log.info("## ------------------- Junit start -------------------");
        }
    
        @After
        public void finish() {
            log.info("## ------------------- Junit finish -------------------");
        }
    
    
    }
···