[英]Test Spring/SpringBoot without Application
I want to write integration tests that will have to use Spring Framework and a custom JPA provider. 我想编写必须使用Spring Framework和自定义JPA提供程序的集成测试。 Straightforward answer as I thought would be to create a test class and annotate it as follows: 我认为简单的答案是创建一个测试类并对其进行注释,如下所示:
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test { ...
Hoping for the all of the default auto-configuration required to happen on its own. 希望所有必需的默认自动配置都可以自行发生。 But it doesn't the error is: 但这不是错误:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
Can I avoid creating the: 我可以避免创建:
@SpringBootApplication
public class TestApp { ...
and only use src/test/java folder and provide configuration with "@SpringBootTest(classes=...)"? 并且仅使用src / test / java文件夹并提供带有“ @SpringBootTest(classes = ...)”的配置? What kind of configuration class do I need then? 那我需要什么样的配置类?
I just got the problem let me go back to begining; 我刚遇到问题,让我重新开始。 First add a configuration class ApplicationContainer 首先添加一个配置类ApplicationContainer
public final class ApplicationContainer {
private static volatile ApplicationContainer singleton;
private ApplicationContext context;
private ApplicationContainer()
{
}
public static ApplicationContainer getInstance()
{
if(ApplicationContainer.singleton == null)
{
synchronized(ApplicationContainer.class)
{
if(ApplicationContainer.singleton == null)
{
ApplicationContainer.singleton = new ApplicationContainer();
}
}
}
return ApplicationContainer.singleton;
}
public void setApplicationContext(ApplicationContext context)
{
this.context = context;
}
public <T> T getBean(Class<T> requiredType)
{
if(this.context == null)
{
throw new IllegalStateException("ApplicationContainer is not started");
}
return this.context.getBean(requiredType);
}
public void start()
{
if(this.context == null)
{
this.context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
}
}
public void stop()
{
if(this.context == null)
{
return;
}
if(this.context instanceof AnnotationConfigApplicationContext)
{
((AnnotationConfigApplicationContext)this.context).close();
}
this.context = null;
}
@Configuration
@ComponentScan("com.domain.package")
public static class ApplicationConfig
{
}}
And change your test class's like that: 然后像这样更改测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ApplicationContainer.ApplicationConfig.class})
public class YourTestIT {
@Autowired
private ApplicationContext applicationContext;
@Before
public void up() {
ApplicationContainer.getInstance().setApplicationContext(this.applicationContext);
}
@After
public void down() {
ApplicationContainer.getInstance().stop();
}
//test cases
}
then you can @Autowired your repository classes and use directly also you should add IT extension your test class name. 那么您可以@Autowired您的存储库类并直接使用,还应该在测试类名称中添加IT扩展名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.