[英]Overriding a property value in a Micronaut test
Using @Property
on a test method seems to not take effect.在测试方法上使用
@Property
似乎不起作用。
This is my application.yml
这是我的
application.yml
greeting: Hello
Application.java
@Controller
public class Application {
@Property(name = "greeting")
String greeting;
@Get
String hello() {
return greeting + " World!";
}
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
Now test1
passes as expected, but test2
fails.现在
test1
按预期通过,但test2
失败。
@MicronautTest//(rebuildContext = true)
public class DemoTest {
@Inject
@Client("/")
HttpClient client;
@Test
void test1() {
assertEquals(
"Hello World!",
client.toBlocking().retrieve(GET("/"))
);
}
@Property(name = "greeting", value = "Bonjour")
@Test
void test2() {
assertEquals(
"Bonjour World!",
client.toBlocking().retrieve(GET("/"))
);
}
}
Output输出
org.opentest4j.AssertionFailedError: expected: <Bonjour World!> but was: <Hello World!>
If I used rebuildContext = true
, the HttpClient
is not re-configured with the new port, and the second test fails with:如果我使用了
rebuildContext = true
,则不会使用新端口重新配置HttpClient
,并且第二个测试失败:
Connect Error: Connection refused: no further information: localhost/127.0.0.1:[some random port]
I put this code on GitHub at https://github.com/salah3x/micronaut-test-property-override我将此代码放在 GitHub 上https://github.com/salah3x/micronaut-test-property-override
Is this a bug or I'm missing something?这是一个错误还是我遗漏了什么?
It seems that manually refreshing the EmbeddedServer
combined with @MicronautTest(rebuildContext = true)
makes the tests pass.似乎手动刷新
EmbeddedServer
并结合@MicronautTest(rebuildContext = true)
使测试通过。
@MicronautTest(rebuildContext = true)
public class DemoTest {
@Inject
@Client("/")
HttpClient client;
@Inject
EmbeddedServer server;
@Test
void test1() {
assertEquals(
"Hello World!",
client.toBlocking().retrieve(GET("/"))
);
}
@Property(name = "greeting", value = "Bonjour")
@Test
void test2() {
server.refresh();
assertEquals(
"Bonjour World!",
client.toBlocking().retrieve(GET("/"))
);
}
}
But that's more of a workaround than a solution because the docs states that it should be automatically picked up.但这更像是一种变通方法而不是解决方案,因为文档指出它应该被自动拾取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.