【发布时间】:2020-10-20 00:51:23
【问题描述】:
在测试方法上使用@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:[some random port]
我将此代码放在 GitHub 上 https://github.com/salah3x/micronaut-test-property-override
这是一个错误还是我遗漏了什么?
【问题讨论】:
-
如果您将
@Property(name = "greeting", value = "Bonjour")添加到测试类(而不是测试中的方法),这会影响您的一个或两个测试方法吗? -
@JeffScottBrown 将其添加到测试类会影响两个测试,因此
test1失败而test2通过