How to test Micronaut with Gru?

Vladimír Oraný
Stories by Agorapulse
1 min readMay 30, 2018

My first question when I've seen new Micronaut framework in action was: “Can I still use Gru for testing Micronaut applications or has it become obsolete?”. Luckily, tests in Micronaut are based on running the snappy embedded server so one can easily use the Gru's Micronaut client (1.0.1 and newer)

Add following dependency to your build.gradle file:

testImplementation "com.agorapulse:gru-micronaut:1.0.1"

Let's imagine you have a simple controller HelloController:

@Controller("/hello")
class HelloController {

@Get("/")
String index() {
return "Hello World"
}
}

You are able to update the automatically generated specification file to use Gru.

If you are using Java:

@MicronautTest
public class HelloControllerSpec {
@Inject Gru gru @Test
public void testMicronautWithGru() throws Throwable {
gru.verify(test -> test
.get("/hello")
.expect(resp -> resp.text(inline('Hello World!')))
);
}
}

If you are using Spock and Groovy:

@MicronautTest
public class HelloControllerSpec extends Specification {
@Inject Gru gru void 'test micronaut with Gru'() {
expect:
gru.test {
get '/hello'
expect {
text inline('Hello World!')
}
}
}
}

--

--