Micronaut:测试失败,页面无法显示,但是可以通过浏览器正常访问页面

时间:2018-12-03 01:42:50

标签: java gradle junit micronaut

我正在学习如何使用Micronaut创建REST服务,但是我看不到的东西是我的测试设置有问题,无论我在代码或gradle中所做的任何更改,我总是在运行时遇到一个烦人的“找不到页面”错误测试,但当应用程序正常运行时不会。

我在软件包App中有一个名为br.com.myproject的类。在此类中,我只有main方法,如下所示:

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

在子包br.com.myproject.controllers中,我有一个用HelloController注释的类@Get("/hello"),应该用一个“ Hello,World!”作为响应。文本,当我通过浏览器访问它时,它通常会执行该操作:

package br.com.myproject.controllers;

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

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

}

在tests目录中,我拥有HelloControllerTest类,可以确保我的/hello端点正常工作。但不幸的是,我的测试失败并出现PageNotFound异常。

我的课堂测试如下:

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!");
    }

}

最后,我的gradle设置:

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'

我的测试和gradle设置均基于Micronaut文档(this one)中的代码示例编写。并以某种方式在代码示例中的测试正常工作。

例如,这些是我在这里问之前尝试过的:

  • 在代码示例中将依赖项的版本更改为与版本匹配:无效;
  • 将所有类放在同一包(br.com.myproject)下:无效;
  • 将示例代码的gradle设置复制/粘贴到我的gradle设置中,并正确更改程序包名称:它不起作用;
  • 打印出测试中使用的URL只是为了检查它是否正确:它是正确的,但是仍然抛出“找不到页面”。

但是,当我运行该应用程序并在浏览器中键入地址时,一切正常。

我已经在Google搜索中找到了“未找到micronaut测试页”等类似关键字,但没有找到有用的文章来帮助我解决此错误。

只需提一下:我对gradle配置并不完全熟悉,因此我怀疑这里可能缺少一些东西。

有人知道我可能会缺少什么吗?

我感谢任何提示。

谢谢=)

3 个答案:

答案 0 :(得分:1)

查看位于https://github.com/jeffbrown/jonathansouzanotfound的项目。我已经将您的代码直接粘贴到该项目中(并添加了丢失的导入语句),并且测试通过了。

 $ ./gradlew clean test

> Task :compileJava

... 

BUILD SUCCESSFUL in 4s
5 actionable tasks: 5 executed

答案 1 :(得分:0)

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

答案 2 :(得分:0)

引用自 Jeff's 评论并在此处回答以使其更显眼

如果您通过 IDE 运行测试,请确保在 IDE 中启用了注释处理。

enter image description here