Reputation:
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
@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.
@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:
Upvotes: 0
Views: 1609
Reputation: 2874
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.
Upvotes: 0