gpt4 book ai didi

java - Junit 5 测试,获取 java.lang.IllegalStateException : Test classes cannot include @Bean methods

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:30 26 4
gpt4 key购买 nike

@ContextConfiguration(classes = ConfigureCustomConfigurationModelProviderTest.class)
public class ConfigureCustomConfigurationModelProviderTest extends AbstractContextTest {
@Bean(name = "smth")
public static ConfigurationModelProvider get() {
return AnnotationConfigurationModelProvider.getInstance();
}
/*...*/
}

自从从 junit4 迁移到 junit5 后,我遇到了这个错误。为什么?

最佳答案

您应该将每个 bean 移动到 @Configuration 类,例如 TestConfig:

@Configuration
public class TestConfig {

@Bean(name = "smth")
public static ConfigurationModelProvider get() {
return AnnotationConfigurationModelProvider.getInstance();
}
}

并通过@Import导入:

@Import({TestConfig.class})
@ContextConfiguration(classes = ConfigureCustomConfigurationModelProviderTest.class)
public class ConfigureCustomConfigurationModelProviderTest extends AbstractContextTest {
}

关于java - Junit 5 测试,获取 java.lang.IllegalStateException : Test classes cannot include @Bean methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47498089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com