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

az31mfrm  于 2023-10-20  发布在  Bootstrap
关注(0)|答案(2)|浏览(193)

我对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?谢谢.

kuhbmx9i

kuhbmx9i1#

问题在于maven依赖性。需要保持相同的spring核心和spring测试版本,这将解决这个问题。

wgeznvg7

wgeznvg72#

我在dependencies.gradle中删除了下面的实现,它可以工作,或者保持相同的版本

implementation group: 'org.springframework', name: 'spring-core', version: '5.3.18'
implementation group: 'org.springframework', name: 'spring-test', version: '5.3.18'

相关问题