Testing Functions
Functions can also be run as part of the Micronaut application context for ease of testing. Similarly to the example above, this approach requires the function-web
and an HTTP server dependency on the classpath for tests. For example, in build.gradle
:
testImplementation("io.micronaut:micronaut-http-server-netty")
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>test</scope>
</dependency>
testImplementation("io.micronaut:micronaut-function-web")
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-function-web</artifactId>
<scope>test</scope>
</dependency>
In order to run the function as a web application, you will need an HTTP server, such as the http-server-netty dependency |
Create a @FunctionClient interface as shown below:
MathClient.groovy
import io.micronaut.function.client.FunctionClient
import javax.inject.Named
@FunctionClient
static interface MathClient {
Long max() (1)
@Named("round")
int rnd(float value)
}
For further information on the use of @FunctionClient , please see Calling Functions. |
Now you can start up the Micronaut application and access your function via the client interface in your test.
void "test invoking a local function"() {
given:
EmbeddedServer server = ApplicationContext.run(EmbeddedServer)
MathClient mathClient = server.getApplicationContext().getBean(MathClient)
expect:
mathClient.max() == Integer.MAX_VALUE.toLong()
mathClient.rnd(1.6) == 2
mathClient.sum(new Sum(a:5,b:10)) == 15
}