Jonathan Souza
Jonathan Souza

Reputation: 23

Micronaut: test is failing with Page not foun, but the page can be accessed normally via browser

I was learning how to use Micronaut to create REST services but something I can't see is wrong with my tests settings and whatever I change in my code or in gradle I always get an annoying 'Page not found' error when running the tests but not when the application is running normally.

I have a class named App in the package br.com.myproject. In this class I have only the main method, as follows:

public static void main(final String[] args) {
    Micronaut.run(App.class);
}

In a subpackage br.com.myproject.controllers I have a class HelloController annotated with @Get("/hello") that should respond with a single "Hello, World!" text and it does normally when I access it through the browser:

package br.com.myproject.controllers;

@Controller("/hello")
public class HelloController {

    @Get("/")
    @Produces(MediaType.TEXT_PLAIN)
    public String index() {
        return "Hello, World!";
    }

}

In the tests directory, I have the class HelloControllerTest that should assure my /hello endpoint works properly. But unfortunately, my test is failing with a PageNotFound exception.

My class test is as follows:

package br.com.myproject.controllers;

public class HelloControllerTest {

    private static EmbeddedServer server;
    private static HttpClient client;

    @BeforeClass
    public static void setupServer() {
        server = ApplicationContext.run(EmbeddedServer.class);
        client = server
                .getApplicationContext()
                .createBean(HttpClient.class, server.getURL());
    }

    @AfterClass
    public static void stopServer() {
        if (client != null)
            client.stop();

        if (server != null)
            server.stop();
    }

    @Test
    public void testHello() throws Exception {
        HttpRequest<String> httpRequest = HttpRequest.GET("/hello");
        String body = client.toBlocking().retrieve(httpRequest);
        Assert.assertNotNull(body);
        Assert.assertEquals(body, "Hello, World!");
    }

}

And, for last, my gradle settings:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.3'
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE'
        classpath 'net.ltgt.gradle:gradle-apt-plugin:0.15'
    }
}

plugins {
    id 'io.spring.dependency-management' version '1.0.6.RELEASE'
    id 'com.github.johnrengelman.shadow' version '4.0.3'
    id 'application'
    id 'java'
    id 'net.ltgt.apt-idea' version '0.15'
}

group 'br.com.myproject'
version '1.0.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
    maven { url "https://plugins.gradle.org/m2/" }
}

dependencies {
    annotationProcessor 'io.micronaut:micronaut-inject-java:1.0.0'

    compile 'io.micronaut:micronaut-http-client:1.0.0'
    compile 'io.micronaut:micronaut-http-server-netty:1.0.0'
    compile 'io.micronaut:micronaut-inject:1.0.0'
    compile 'io.micronaut:micronaut-runtime:1.0.0'
    compileOnly 'io.micronaut:micronaut-inject-java:1.0.0'

    runtime 'ch.qos.logback:logback-classic:1.2.3'

    testCompile 'junit:junit:4.12'
    testCompile 'io.micronaut:micronaut-inject-java:1.0.0'
}

shadowJar {
    mergeServiceFiles()
}

mainClassName = 'br.com.myproject.App'

Both my tests and my gradle settings were written based on a code example in Micronaut documentation (this one). And somehow the tests in the code example works properly.

For instance, these are what I tried before asking here:

Everything works fine, however, when I run the application and type the address into the browser.

I have googled the keywords "micronaut test page not found" and similars but I found no useful articles that helped me to fix this error.

Just to mention: I am not completely familiar with gradle configuration and thus I suspect I might be lacking something here.

Does anyone have any idea what I may be missing?

I appreciate any tip.

Thanks =)

Upvotes: 2

Views: 3266

Answers (3)

kooskoos
kooskoos

Reputation: 4859

Referred from Jeff's comment and answered it here to make it more visible

Make sure annotation processing is enabled in the IDE if you running the tests through IDE.

enter image description here

Upvotes: 0

issam
issam

Reputation: 93

 @Get("/")
    @Produces (MediaType.TEXT_PLAIN)
    public String index(){
        return "Hello World";
    }

Upvotes: 0

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27220

See the project at https://github.com/jeffbrown/jonathansouzanotfound. I have pasted your code directly into that project (and added missing import statements) and the test passes.

 $ ./gradlew clean test

> Task :compileJava

... 

BUILD SUCCESSFUL in 4s
5 actionable tasks: 5 executed

Upvotes: 2

Related Questions