Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run spring boot integration test using maven test but the embeded tomcat is not started

I write an integration test for my spring boot application, it is shown as following code. It works fine in Eclipse IDE by run as Junit test and the embeded tomcat is started and shutdown automatically. But when I run it using maven install, it failed just because the embeded tomcat is not started. Should I need more configuration in Maven? How to do it?

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestCoreConfig.class)
@WebAppConfiguration
@IntegrationTest
public class UserRegistrationTest {

@Autowired
private Receiver receiver;


@Test
public void shouldRegisterUser() {
    //Given
    String email = System.currentTimeMillis() + "[email protected]";

    //When
    ResponseEntity<UserInfo> entity = registerUser(email);

    //Then
    assertEquals(HttpStatus.OK, entity.getStatusCode());

    UserInfo registeredUser = entity.getBody();
    System.out.println ("The registered user ID is " + registeredUser.getUserId());

    String receivedMessages = receiver.getMessages().toString();
    assertTrue(receivedMessages + " expected to contain " + email,   receivedMessages.indexOf(email) != -1);
}

private ResponseEntity<UserInfo> registerUser(String email) {
    String password = "secret";
    UserInfo userInfo = new UserInfo(email, password);

    RestTemplate restTemplate = new RestTemplate();
    return restTemplate.postForEntity("http://localhost:8080/users/registration", userInfo,  UserInfo.class);
}
}

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.wangjun</groupId>
<artifactId>userRegistration-microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>userRegistration-microservice</name>
<description>UserRegistration microservice</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
     <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>com.wangjun.microservices.userregistration.Application</start-class>
    <java.version>1.7</java.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

sshouldRegisterUser(com.wangjun.microservices.userregistration.rest.functional.UserRegistrationTest) Time elapsed: 1.031  sec  <<< ERROR! org.springframework.web.client.ResourceAccessException: I/O error on POST request for     :Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at java.net.Socket.connect(Socket.java:528)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
        at sun.net.www.http.HttpClient.New(HttpClient.java:308)
        at sun.net.www.http.HttpClient.New(HttpClient.java:326)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)
        at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpReq uest.java:78)
        at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHtt pRequest.java:48)
        at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:52)
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:551)
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
        at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:363)
        at com.wangjun.microservices.userregistration.rest.functional.UserRegistrationTest.registerUser(UserRegistration Test.java:91)
        at com.wangjun.microservices.userregistration.rest.functional.UserRegistrationTest.shouldRegisterUser(UserRegistrationTest.java:37)
like image 661
Jun Wang Avatar asked Oct 17 '14 09:10

Jun Wang


2 Answers

Update for Spring 2.0.

My Tomcat was starting fine when running the app, but wasn't starting for me in my tests. @Miodigy's answer didn't work for me, but this did:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
class TokenTest {
    @Autowired private SavedSearchesController controller
    @LocalServerPort private int port

Important points are:

@SpringBootTest - make sure to add the webEnvironment parameter.

@LocalServerPort - this is where spring will inject the random port.

Without webEnvironment Tomcat was not starting for me in my tests.

like image 44
Kong Avatar answered Oct 17 '22 02:10

Kong


scan ports, set server port to a free one found

@IntegrationTest("server.port:0")

discover the port number

@Value("${local.server.port}")
int port;

add port to url

restTemplate.postForEntity("http://localhost:"+port+"/users/registration"...
like image 78
Maciej Mioduszewski Avatar answered Oct 17 '22 02:10

Maciej Mioduszewski