Java @MicronautTest-限制实例化的内容
每当我在我的项目中使用Java @MicronautTest-限制实例化的内容,java,micronaut,Java,Micronaut,每当我在我的项目中使用@MicronautTest时,整个应用程序都作为一个整体启动,因为某些事情需要服务在启动时完成,所以它们也会被执行。 当我测试一个简单的控制器时,我不需要整个单例shebang 我的问题是,在测试Micronaut时,如何限制加载哪些控制器和单例 我有一个小的演示例子来说明这个问题 @Singleton public class SingletonService { @EventListener public void init(ApplicationS
@MicronautTest
时,整个应用程序都作为一个整体启动,因为某些事情需要服务在启动时完成,所以它们也会被执行。
当我测试一个简单的控制器时,我不需要整个单例shebang
我的问题是,在测试Micronaut时,如何限制加载哪些控制器和单例
我有一个小的演示例子来说明这个问题
@Singleton
public class SingletonService {
@EventListener
public void init(ApplicationStartupEvent event) {
throw new RuntimeException("I was initialized");
}
}
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>micronaut-test</artifactId>
<version>0.1</version>
<packaging>${packaging}</packaging>
<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>2.5.1</version>
</parent>
<properties>
<packaging>jar</packaging>
<jdk.version>11</jdk.version>
<release.version>11</release.version>
<micronaut.version>2.5.1</micronaut.version>
<exec.mainClass>com.example.Application</exec.mainClass>
<micronaut.runtime>netty</micronaut.runtime>
</properties>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.children="append">
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=com.example</arg>
<arg>-Amicronaut.processing.module=micronaut-test</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
一个简单的服务,在实例化时抛出以指示问题
@Singleton
public class SingletonService {
@EventListener
public void init(ApplicationStartupEvent event) {
throw new RuntimeException("I was initialized");
}
}
简单控制器
@Controller
public class TestController {
@Get
public Simple findAll() {
return new Simple("ATest");
}
public static class Simple {
private final String test;
public Simple(String test) {
this.test = test;
}
public String getTest() {
return test;
}
}
}
一个简单的测试
@MicronautTest
class TestControllerTest {
@Inject
@Client("/")
RxHttpClient rxHttpClient;
@Test
void controller() {
HttpResponse<ByteBuffer> byteBufferHttpResponse = rxHttpClient.exchange("/").blockingFirst();
assert byteBufferHttpResponse.getStatus().getCode() == 200;
}
}
@MicronautTest
类TestControllerTest{
@注入
@客户(“/”)
RxHttpClient RxHttpClient;
@试验
空控制器(){
HttpResponse byteBufferHttpResponse=rxHttpClient.exchange(“/”).blockingFirst();
assert byteBufferHttpResponse.getStatus().getCode()==200;
}
}
测试结果如下:
[INFO] Running com.example.controller.TestControllerTest
11:01:52.804 [main] INFO i.m.context.env.DefaultEnvironment - Established active environments: [test]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 7.232 s <<< FAILURE! - in com.example.controller.TestControllerTest
[ERROR] com.example.controller.TestControllerTest Time elapsed: 7.229 s <<< ERROR!
java.lang.RuntimeException: I was initialized
[INFO]运行com.example.controller.TestControllerTest
11:01:52.804[主]信息i.m.context.env.DefaultEnvironment-已建立的活动环境:[测试]
[错误]测试运行:1,失败:0,错误:1,跳过:0,经过的时间:7.232秒
我的问题是如何限制哪些控制器和单例是可用的
测试Micronaut时加载
至少有@Requires
注释允许在当前环境中加载bean时进行灵活的配置
例如,在您的情况下,它应该类似于:
import javax.inject.Singleton;
import io.micronaut.context.annotation.Requires;
@Singleton
@Requires(notEnv = {Environment.TEST})
public class SingletonService {
@EventListener
public void init(ApplicationStartupEvent event) {
throw new RuntimeException("I was initialized");
}
}
因此,不会引发来自服务的异常
然而,我真的不知道为什么需要排除该服务。也许其他方法更方便
例如,请参阅micronaut测试文档中的模仿合作者
部分:假设我有一个检查网站是否每分钟都在运行的单例。这是如何连接到只打印hello的控制器的?这就是为什么我想排除某些单身人士。那些与我试图测试的东西没有任何关系的东西不应该以生产形式运行
@MicronautTest(contextBuilder = TestControllerTest.TestContextBuilder.class)
class TestControllerTest {
public static class TestContextBuilder extends DefaultApplicationContextBuilder {
public TestContextBuilder() {
eagerInitSingletons(false);
eagerInitAnnotated(null);
eagerInitConfiguration(false);
}
}
@Inject
@Client("/")
RxHttpClient rxHttpClient;
@Test
void controller() {
HttpResponse<ByteBuffer> byteBufferHttpResponse = rxHttpClient.exchange("/").blockingFirst();
assert byteBufferHttpResponse.getStatus().getCode() == 200;
}
}
import javax.inject.Singleton;
import io.micronaut.context.annotation.Requires;
@Singleton
@Requires(notEnv = {Environment.TEST})
public class SingletonService {
@EventListener
public void init(ApplicationStartupEvent event) {
throw new RuntimeException("I was initialized");
}
}