简体   繁体   中英

How to start SpringBootApplication inside a SpringBootTest

I'm trying to learn SpringBoot so please bear with me, I have created SpringBootTest project but Im having trouble getting the tests to pass using mvn clean install

The problem is that I don't know how to start the StartLocalServer before I run my tests.

In my project I have two modules

  1. karate (containing the tests)
  2. local-server. The Karate tests use @SpringBootTest and the local-server is using @SpringBootApplication .

On my local machine I can start the server manually and run the tests manually (this will pass), but fails with mvn clean install

How do I start the local-server before running my Tests? I'm not sure where I'm going wrong. Any help would be appreciated.

ServerClass

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

Test

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

    }

Console output

    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: 

Check out: https://spring.io/guides/gs/testing-web/

Testing the Controller layer (handles incoming Http requests). This is an internal (to the application) way of testing the class:

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();
    }
}

Another way you can test the Controller is hooked up correctly in your Spring boot application is to perform an external test that uses the network layer and invokes the correct handler for the request using an HTTP request, rather than an application method call. This will do what you want (start up your application server)

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

This is a generic example that you should be able to copy/paste and adapt which will be a more useful exercise to you than me simply posting the answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM