我们如何在MicronautTest类中为同一控制器运行多个测试?

时间:2019-06-10 10:37:43

标签: junit5 micronaut

有一个带有两个端点的EmployeeController。一个是 / employee / {employeeid} / department / employee / {employeeid} / salary 。我已经在两个端点的micronaut中编写了测试用例。如果单独运行,它们都可以成功运行。  但是,当一起运行时,面对io.micronaut.http.client.exceptions.HttpClientResponseException: Not Found。仅使用一个HttpClient来运行两个测试。

1 个答案:

答案 0 :(得分:0)

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

[42601] ERROR: syntax error at or near "as"

测试1:

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;
    }
}

测试2:

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'
    }
}

测试3:

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)
}

测试4:

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'
    }
}