在 Micronaut 测试中覆盖属性值

huangapple go评论134阅读模式
英文:

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 = trueHttpClient 不会使用新的端口重新配置,第二个测试将因以下错误而失败:

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 = &quot;greeting&quot;)
    String greeting;

    @Get
    String hello() {
        return greeting + &quot; World!&quot;;
    }

    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(&quot;/&quot;)
    HttpClient client;

    @Test
    void test1() {
        assertEquals(
                &quot;Hello World!&quot;,
                client.toBlocking().retrieve(GET(&quot;/&quot;))
        );
    }

    @Property(name = &quot;greeting&quot;, value = &quot;Bonjour&quot;)
    @Test
    void test2() {
        assertEquals(
                &quot;Bonjour World!&quot;,
                client.toBlocking().retrieve(GET(&quot;/&quot;))
        );
    }
}

Output

org.opentest4j.AssertionFailedError: expected: &lt;Bonjour World!&gt; but was: &lt;Hello World!&gt;

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(&quot;/&quot;)
    HttpClient client;

    @Inject
    EmbeddedServer server;

    @Test
    void test1() {
        assertEquals(
                &quot;Hello World!&quot;,
                client.toBlocking().retrieve(GET(&quot;/&quot;))
        );
    }

    @Property(name = &quot;greeting&quot;, value = &quot;Bonjour&quot;)
    @Test
    void test2() {
        server.refresh();
        assertEquals(
                &quot;Bonjour World!&quot;,
                client.toBlocking().retrieve(GET(&quot;/&quot;))
        );
    }
}

But that's more of a workaround than a solution because the docs states that it should be automatically picked up.

huangapple
  • 本文由 发表于 2020年10月20日 08:51:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64436993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定