Есть множество способов сделать это. Посмотреть проект на 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)
}