英文:
Overriding a property value in a Micronaut test
问题
使用@Property
在测试方法上似乎没有生效。
这是我的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);
}
}
现在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("/"))
);
}
}
输出:
org.opentest4j.AssertionFailedError: expected: <Bonjour World!> but was: <Hello World!>
如果我使用了 rebuildContext = true
,HttpClient
不会使用新的端口重新配置,第二个测试将因以下错误而失败:
Connect Error: Connection refused: no further information: localhost/127.0.0.1:[一些随机端口]
我在 GitHub 上放置了这段代码,网址是 https://github.com/salah3x/micronaut-test-property-override 。
这是一个 bug 还是我遗漏了什么?
英文:
Using @Property
on a test method seems to not take effect.
This is my 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.
@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:
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
Is this a bug or I'm missing something?
答案1
得分: 3
以下是您提供的代码的翻译部分:
似乎手动刷新 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("/"))
);
}
}
但这更像是一种权宜之计,而不是一个解决方案,因为文档指出它应该会自动被识别。
英文:
It seems that manually refreshing the EmbeddedServer
combined with @MicronautTest(rebuildContext = true)
makes the tests pass.
@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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论