【问题标题】:How do we run multiple tests for the same controller in a MicronautTest class?我们如何在 MicronautTest 类中为同一个控制器运行多个测试?
【发布时间】:2019-06-10 10:37:43
【问题描述】:

有一个 EmployeeController 有两个端点。一种是 /employee/{employeeid}/department/employee/{employeeid}/salary。我已经在 micronaut 中为两个端点编写了测试用例。如果单独运行,它们都会成功运行。 然而,当一起奔跑时,面对io.micronaut.http.client.exceptions.HttpClientResponseException: Not Found。只使用一个 HttpClient 来运行这两个测试。

【问题讨论】:

    标签: junit5 micronaut


    【解决方案1】:

    有很多方法可以做到这一点。请参阅https://github.com/jeffbrown/nacharymntests 上的项目。

    package nacharymntests;
    
    import io.micronaut.http.annotation.Controller;
    import io.micronaut.http.annotation.Get;
    
    @Controller("/employee")
    public class EmployeeController {
    
        @Get("/hello/{name}")
        public String hello(String name) {
            return "Hello " + name;
        }
    
        @Get("/goodbye/{name}")
        public String goodbye(String name) {
            return "Goodbye " + name;
        }
    }
    

    测试 1:

    package nacharymntests
    
    import io.micronaut.context.ApplicationContext
    import io.micronaut.http.client.HttpClient
    import io.micronaut.runtime.server.EmbeddedServer
    import spock.lang.AutoCleanup
    import spock.lang.Shared
    import spock.lang.Specification
    
    class LowLevelClientSpec extends Specification {
    
        @Shared
        @AutoCleanup
        EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer)
    
        @Shared
        @AutoCleanup
        HttpClient client = HttpClient.create(embeddedServer.URL)
    
        void "test hello"() {
            expect:
            client.toBlocking().retrieve("/employee/hello/Jeff") == 'Hello Jeff'
        }
    
        void "test goodbye"() {
            expect:
            client.toBlocking().retrieve("/employee/goodbye/Jeff") == 'Goodbye Jeff'
        }
    }
    

    测试 2:

    package nacharymntests
    
    import io.micronaut.context.ApplicationContext
    import io.micronaut.http.annotation.Get
    import io.micronaut.http.client.annotation.Client
    import io.micronaut.runtime.server.EmbeddedServer
    import spock.lang.AutoCleanup
    import spock.lang.Shared
    import spock.lang.Specification
    
    class DeclarativeClientSpec extends Specification {
    
        @Shared
        @AutoCleanup
        EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer)
    
        @Shared
        AnotherEmployeeClient client = embeddedServer.applicationContext.getBean(AnotherEmployeeClient)
    
        void "test hello"() {
            expect:
            client.hello('Jeff') == 'Hello Jeff'
        }
    
        void "test goodbye"() {
            expect:
            client.goodbye('Jeff') == 'Goodbye Jeff'
        }
    }
    
    @Client(value = '/', path = '/employee')
    interface AnotherEmployeeClient {
    
        @Get('/hello/{name}')
        String hello(String name)
    
        @Get('/goodbye/{name}')
        String goodbye(String name)
    }
    

    测试 3:

    package nacharymntests
    
    import io.micronaut.http.client.HttpClient
    import io.micronaut.http.client.annotation.Client
    import io.micronaut.test.annotation.MicronautTest
    import spock.lang.AutoCleanup
    import spock.lang.Shared
    import spock.lang.Specification
    
    import javax.inject.Inject
    
    @MicronautTest
    class LowLevelClientWithTestFrameworkSpec extends Specification {
    
        @AutoCleanup
        @Shared
        @Inject
        @Client("/")
        HttpClient client
    
        void "test hello"() {
            expect:
            client.toBlocking().retrieve("/employee/hello/Jeff") == 'Hello Jeff'
        }
    
        void "test goodbye"() {
            expect:
            client.toBlocking().retrieve("/employee/goodbye/Jeff") == 'Goodbye Jeff'
        }
    }
    

    测试 4:

    package nacharymntests
    
    import io.micronaut.http.annotation.Get
    import io.micronaut.http.client.annotation.Client
    import io.micronaut.test.annotation.MicronautTest
    import spock.lang.Shared
    import spock.lang.Specification
    
    import javax.inject.Inject
    
    @MicronautTest
    class DeclarativeClientWithTestFrameworkSpec extends Specification {
    
        @Shared
        @Inject
        EmployeeClient client
    
        void "test hello"() {
            expect:
            client.hello('Jeff') == 'Hello Jeff'
        }
    
        void "test goodbye"() {
            expect:
            client.goodbye('Jeff') == 'Goodbye Jeff'
        }
    }
    
    @Client(value = '/', path = '/employee')
    interface EmployeeClient {
    
        @Get('/hello/{name}')
        String hello(String name)
    
        @Get('/goodbye/{name}')
        String goodbye(String name)
    }
    

    【讨论】:

    • 您没有指明您使用的是哪个测试框架。上面的例子都使用了Spock,但基本上同样的事情可以用JUnit来完成。
    • 我现在看到您在问题上使用了标签junit5,我最初没有注意到。在use-junit 分支上github.com/jeffbrown/nacharymntests/commit/… 的提交显示了一些通过的JUnit 测试。我希望这会有所帮助。
    • 是的,我可以在所有测试中使用 HttpClient,方法是在使用 client = HttpClient.create(server.getURL()); 运行之前对其进行模拟。就我而言,我在每个测试中都使用client.toBlocking() 阻止客户端,这就是为什么它只适用于一个测试而不适用于其他测试的原因。谢谢!
    • 这对我来说没有意义。如您所见,LowLevelClientWithTestFrameworkSpec 在每个测试中都使用toBlocking(),这很好。 toBlocking() 应该不是问题。
    • 以前,我只是使用@Inject@Client("/")RxHttpClient client; 来注入客户端实例,如文档中所示。这仅在我进行一项测试时才有效。我尝试了两个测试,只有一个测试通过,而另一个测试失败,给出了io.micronaut.http.client.exceptions.HttpClientResponseException: Not Found 异常,因为该客户端实例已被测试方法使用(或阻止)并且无法被其他测试方法使用。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2022-09-23
    • 1970-01-01
    相关资源
    最近更新 更多