无法加载TestContextBootstrapper [null]。指定@BootstrapWith和依赖关系树OK

时间:2018-11-10 03:05:59

标签: spring junit

我在Spring测试中相对较新。我正在尝试测试存储库。

在阅读了有关同一问题的不同文章之后,这似乎是一个依赖性问题。 但检查后我没有发现任何冲突(请参见下面的内容)。

这是测试类:

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ActionRepositoryTest2 {

@Autowired ActionRepository actionRepository;



@Test
public void actionRepository() {

    Command command = new Command();
    Action action = new Action();

    action.setText("TestAction");

    actionRepository.save(action);

    List<Action> actions = (List<Action>) 
    actionRepository.findAll();




    assertNotNull(action);

    assertEquals(actions.size(),1);



}


@Configuration
public static class InnerConf2 {

    @Bean
    ActionRepository actionRepository() {
        return new ActionRepositoryImpl();
    } 

    @Bean
    CommandRepository commandRepository() {
        return new CommandRepositoryImpl();
    } 
    @Bean
    OrderRepository orderRepository() {
        return new OrderRepositoryImpl();
    } 


}

}

这是我得到的例外:

ava.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:144)

...

这是依赖项检查:

[INFO] 
[INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ emorobots ---
[INFO] Verbose not supported since maven-dependency-plugin 3.0
[INFO] com.res:emorobots:jar:0.0.1-SNAPSHOT
[INFO] +- org.jpl7:jpl:jar:7.8.0:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.slf4j:slf4j-api:jar:1.8.0-beta2:runtime
[INFO] +- org.apache.commons:commons-lang3:jar:3.8.1:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.11.1:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.11.1:runtime
[INFO] +- org.apache.logging.log4j:log4j-jcl:jar:2.11.1:runtime
[INFO] |  \- commons-logging:commons-logging:jar:1.2:runtime
[INFO] +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.11.1:runtime
[INFO] +- org.picocontainer:picocontainer:jar:2.15:compile
[INFO] +- javax.inject:javax.inject:jar:1:compile
[INFO] +- javax.annotation:javax.annotation-api:jar:1.3.2:runtime
[INFO] +- org.springframework:spring-core:jar:5.1.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-jcl:jar:5.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:5.1.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.1.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:5.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:5.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:5.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-oxm:jar:5.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-test:jar:5.1.2.RELEASE:compile
[INFO] +- org.springframework.data:spring-data-jpa:jar:1.10.1.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.12.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-orm:jar:4.2.5.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-tx:jar:4.2.5.RELEASE:compile
[INFO] |  \- org.aspectj:aspectjrt:jar:1.8.9:compile
[INFO] +- org.eclipse.persistence:javax.persistence:jar:2.1.0:compile
[INFO] +- javax.transaction:javax.transaction-api:jar:1.3:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:5.3.7.Final:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:5.3.7.Final:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  +- org.jboss:jandex:jar:2.0.5.Final:compile
[INFO] |  |  +- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  |  \- javax.activation:javax.activation-api:jar:1.2.0:compile
[INFO] |  +- org.dom4j:dom4j:jar:2.1.1:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.0.4.Final:compile
[INFO] |  +- javax.persistence:javax.persistence-api:jar:2.2:compile
[INFO] |  \- net.bytebuddy:byte-buddy:jar:1.8.17:compile
[INFO] +- org.javassist:javassist:jar:3.24.0-GA:compile
[INFO] \- mysql:mysql-connector-java:jar:8.0.13:runtime
[INFO]    \- com.google.protobuf:protobuf-java:jar:3.6.1:runtime
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

有没有办法(我考虑的最后一个选项)来跟踪SpringJUnit4ClassRunner? 谢谢。

1 个答案:

答案 0 :(得分:0)

问题与Maven依赖有关。需要保持相同的spring-core和spring-test版本,以解决该问题。