首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法生成带有空“contextLoader”的ApplicationContext。考虑用@ContextConfiguration注释测试类

无法生成带有空“contextLoader”的ApplicationContext。考虑用@ContextConfiguration注释测试类
EN

Stack Overflow用户
提问于 2017-02-06 22:00:08
回答 1查看 1.6K关注 0票数 0

我有一个使用junit的简单测试类。不幸的是,当我想运行测试用例时,它会抱怨。

这是maven:

代码语言:javascript
复制
<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <version>1.4.3.RELEASE</version>
        <scope>test</scope>
    </dependency>

然后在测试类中是:

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RestUploaderApplication.class)
public class RestUploaderApplicationTests {

    Station station1;
    Station station2;
    Content content1;
    Content content2;

    ContentRepository contentRepo;
    StationRepository stationRepo;

    @Before
    public void createObjects(){
        Station station1=new Station("UT","Livii 2");
        Station station2=new Station("City Center","Kissing Square");
        Content content1=new Content(station1,"BMW","Text","google.com",10,true);
        Content content2=new Content(station2,"SWB","Image","swb.com",100,true);

    }

    @Test
    public void insertInstancesTest() {
        int size=station1.getContents().size();
        assertEquals(1,size);
    }}

最后,在运行时出现错误:

代码语言:javascript
复制
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/runner/Description;Ljava/lang/Throwable;)V
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@67b64c45] to prepare test instance  a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
    at org.springframework.util.Assert.notNull(Assert.java:115)
EN

回答 1

Stack Overflow用户

发布于 2017-02-07 04:12:28

我相信@SpringApplicationConfiguration在SpringBoot1.4.0中被否决了。

我不确定,但可以试着用@SpringBootTest代替@SpringBootTest

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RestUploaderApplicationTests {

SpringBootTest应该自动配置春季上下文加载器(同时希望重新设置错误:...instance a NULL 'contextLoader‘。考虑用@ContextConfiguration注释您的测试类),并自动配置您的MockMVC (虽然在本例中您似乎不需要)

顺便说一句,如果这是实际的测试计划,它看起来不需要被Spring感知,似乎没有任何使用的spring组件,(@Beans / @MockBeans,@Autowired,@ JUnit等等)和常规的旧JUnit应该是很好的

希望这能有所帮助

编辑//添加源

@SpringApplicationConfiguration docs

@SpringBootTest文档

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42078027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档