springboot junit 测试出现的错误

来源:互联网 发布:java中的多态如何理解 编辑:程序博客网 时间:2024/06/06 11:46

spring boot  junit 测试代码如下

package com.zyc.ew;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest(classes =App.class)//App是springboot 的启动类
public class ApplicationTest {

        //@Autowired  ClassService classService; //此处可注入自己需要的bean
    @Test
    public void testQuartz(){
        System.out.println("test");
    }

}

在springboot junit 测试中 springboot 1.4以上的要使用@RunWith(SpringRunner.class),@SpringBootTest(classes =App.class)注解

springboot 1.4 以下使用

// SpringJUnit支持,由此引入Spring-Test框架支持!
@RunWith(SpringJUnit4ClassRunner.class)
// 指定我们SpringBoot工程的Application启动类
@SpringApplicationConfiguration(classes = App.class)
//由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration
@WebAppConfiguration


需要的jar 不需要引入org.junit jar

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

由于项目中用到了quartz 需要额外的引入

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.12.RELEASE</version></dependency><dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>4.1.2.RELEASE</version>        </dependency>

如果引入的spring-context jar版本过低 会引起Caused by: java.lang.ClassNotFoundException: org.springframework.context.event.GenericApplicationListener

错误,更正方法就是升级spring-context jar 版本




阅读全文
0 0