如前所述,Micronaut 包括一个 HTTP 服务器和一个 HTTP 客户端。提供了一个低级别的 HTTP 客户端,您可以使用它来测试在上一节中创建的 HelloController。
Testing Hello World
Java |
Groovy |
Kotlin |
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import jakarta.inject.Inject;
import static org.junit.jupiter.api.Assertions.assertEquals;
@MicronautTest
public class HelloControllerSpec {
@Inject
EmbeddedServer server; // (1)
@Inject
@Client("/")
HttpClient client; // (2)
@Test
void testHelloWorldResponse() {
String response = client.toBlocking() // (3)
.retrieve(HttpRequest.GET("/hello"));
assertEquals("Hello World", response); // (4)
}
}
|
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Specification
import jakarta.inject.Inject
@MicronautTest
class HelloControllerSpec extends Specification {
@Inject
EmbeddedServer embeddedServer // (1)
@Inject
@Client("/")
HttpClient client // (2)
void "test hello world response"() {
expect:
client.toBlocking() // (3)
.retrieve(HttpRequest.GET('/hello')) == "Hello World" // (4)
}
}
|
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import jakarta.inject.Inject
@MicronautTest
class HelloControllerSpec {
@Inject
lateinit var server: EmbeddedServer // (1)
@Inject
@field:Client("/")
lateinit var client: HttpClient // (2)
@Test
fun testHelloWorldResponse() {
val rsp: String = client.toBlocking() // (3)
.retrieve("/hello")
assertEquals("Hello World", rsp) // (4)
}
}
|
EmbeddedServer 配置为共享测试域
还定义了一个 HttpClient 实例共享字段
测试使用 toBlocking() 方法进行阻塞调用
retrieve 方法将控制器响应作为字符串返回
除了低级客户端之外,Micronaut 还具有一个声明式的编译时 HTTP 客户端,由 Client 注释提供支持。
要创建客户端,请创建一个使用@Client 注释的接口,例如:
src/main/java/hello/world/HelloClient.java
Java |
Groovy |
Kotlin |
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.client.annotation.Client;
import org.reactivestreams.Publisher;
import io.micronaut.core.async.annotation.SingleResult;
@Client("/hello") // (1)
public interface HelloClient {
@Get(consumes = MediaType.TEXT_PLAIN) // (2)
@SingleResult
Publisher<String> hello(); // (3)
}
|
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import org.reactivestreams.Publisher
import io.micronaut.core.async.annotation.SingleResult
@Client("/hello") // (1)
interface HelloClient {
@Get(consumes = MediaType.TEXT_PLAIN) // (2)
@SingleResult
Publisher<String> hello() // (3)
}
|
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.core.async.annotation.SingleResult
import org.reactivestreams.Publisher
@Client("/hello") // (1)
interface HelloClient {
@Get(consumes = [MediaType.TEXT_PLAIN]) // (2)
@SingleResult
fun hello(): Publisher<String> // (3)
}
|
@Client 注释与一个值一起使用,该值是当前服务器的相对路径
服务器上使用的相同@Get 注释用于定义客户端映射
返回带有从服务器读取的值的带有 SingleResult 注释的 Publisher
要测试 HelloClient,请从与服务器关联的 ApplicationContext 中检索它:
Testing HelloClient
Java |
Groovy |
Kotlin |
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import io.micronaut.core.async.annotation.SingleResult;
import jakarta.inject.Inject;
import reactor.core.publisher.Mono;
import static org.junit.jupiter.api.Assertions.assertEquals;
@MicronautTest // (1)
public class HelloClientSpec {
@Inject
HelloClient client; // (2)
@Test
public void testHelloWorldResponse(){
assertEquals("Hello World", Mono.from(client.hello()).block());// (3)
}
}
|
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import reactor.core.publisher.Mono
import spock.lang.Specification
import jakarta.inject.Inject
@MicronautTest // (1)
class HelloClientSpec extends Specification {
@Inject HelloClient client // (2)
void "test hello world response"() {
expect:
Mono.from(client.hello()).block() == "Hello World" // (3)
}
}
|
import io.micronaut.context.annotation.Property
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import jakarta.inject.Inject
import reactor.core.publisher.Mono
@MicronautTest // (1)
class HelloClientSpec {
@Inject
lateinit var client: HelloClient // (2)
@Test
fun testHelloWorldResponse() {
assertEquals("Hello World", Mono.from(client.hello()).block())// (3)
}
}
|
@MicronautTest 注解定义测试
HelloClient 是从 ApplicationContext 注入的
使用 Project Reactor Mono::block 方法调用客户端
Client 注释在编译时自动为您生成一个实现,无需使用代理或运行时反射。
Client 注解非常灵活。
更多建议: