首页 > 解决方案 > 如何在 SpringBootTest 中启动 SpringBootApplication

问题描述

我正在尝试学习 SpringBoot,所以请多多包涵,我已经创建了 SpringBootTest 项目,但我无法通过使用mvn clean install

StartLocalServer问题是我在运行测试之前不知道如何开始。

在我的项目中,我有两个模块

  1. 空手道(包含测试)
  2. 本地服务器。空手道测试使用@SpringBootTest,本地服务器使用@SpringBootApplication.

在我的本地机器上,我可以手动启动服务器并手动运行测试(这将通过),但失败mvn clean install

如何在运行测试之前启动本地服务器?我不确定我哪里出错了。任何帮助,将不胜感激。

服务器类

    @SpringBootApplication
    public class StartLocalServer {
        public static void main(String[] args) {
            SpringApplication.run(StartLocalServer.class, args);
        }
    }

测试

    @ContextConfiguration(classes = {KarateContextConfiguration.class})
    @SpringBootTest
    public class AbstractTestDefinition {

    }

控制台输出

    2020-10-30 12:33:27.218  INFO 7142 --- [           main] cmccarthyirl.HeroKarateTest              : Starting HeroKarateTest on pc with PID 7142 (started by craig in /home/craig/IdeaProjects/spring-karate-test-harness/karate)
    2020-10-30 12:33:27.222  INFO 7142 --- [           main] cmccarthyirl.HeroKarateTest              : The following profiles are active: prod
    2020-10-30 12:33:31.095  INFO 7142 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-10-30 12:33:31.932  INFO 7142 --- [           main] cmccarthyirl.HeroKarateTest              : Started HeroKarateTest in 5.695 seconds (JVM running for 8.579)
    Warning: Nashorn engine is planned to be removed from a future JDK release
    2020-10-30 12:33:36.343 ERROR 7142 --- [           main] com.intuit.karate                        : org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused (Connection refused), http call failed after 54 milliseconds for URL: http://localhost:8080/hero
    2020-10-30 12:33:36.349 ERROR 7142 --- [           main] com.intuit.karate                        : http request failed: 
    org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
    2020-10-30 12:33:36.430 ERROR 7142 --- [           main] com.intuit.karate                        : org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused (Connection refused), http call failed after 2 milliseconds for URL: http://localhost:8080/hero/1
    2020-10-30 12:33:36.432 ERROR 7142 --- [           main] com.intuit.karate                        : http request failed: 
    org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
    ---------------------------------------------------------
    feature: classpath:cmccarthyirl/GetHerosTest.feature
    scenarios:  2 | passed:  0 | failed:  2 | time: 0.9056
    ---------------------------------------------------------
    2020-10-30 12:33:36.836 ERROR 7142 --- [           main] com.intuit.karate                        : org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused (Connection refused), http call failed after 3 milliseconds for URL: http://localhost:8080/battle
    2020-10-30 12:33:36.837 ERROR 7142 --- [           main] com.intuit.karate                        : http request failed: 

标签: javaspringspring-bootmaven

解决方案


查看:https ://spring.io/guides/gs/testing-web/

测试控制器层(处理传入的 Http 请求)。这是测试类的内部(应用程序)方式:

package com.example.testingweb;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SmokeTest {

    @Autowired
    private HomeController controller;

    @Test
    public void contextLoads() throws Exception {
        assertThat(controller).isNotNull();
    }
}

另一种测试控制器是否正确连接到 Spring Boot 应用程序的方法是执行外部测试,该测试使用网络层并使用 HTTP 请求而不是应用程序方法调用为请求调用正确的处理程序。这将做你想做的(启动你的应用程序服务器)

package com.example.testingweb;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void greetingShouldReturnDefaultMessage() throws Exception {
        assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/",
                String.class)).contains("Hello, World");
    }
}

这是一个通用示例,您应该能够复制/粘贴和调整它,这对您来说比我简单地发布答案更有用。


推荐阅读