我为Spring启动应用程序编写了一个集成测试,它显示如下代码。它可以在Eclipse IDE中通过Junit测试运行,嵌入式tomcat自动启动和关闭。但是当我使用maven install运行它时,它失败只是因为嵌入式tomcat没有启动。我应该在Maven中需要更多配置吗?怎么做?
@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() + "-a-foo@bar.com";
//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)
答案 0 :(得分:10)
扫描端口,将服务器端口设置为找到的免费端口
@IntegrationTest("server.port:0")
发现端口号
@Value("${local.server.port}")
int port;
将端口添加到网址
restTemplate.postForEntity("http://localhost:"+port+"/users/registration"...
答案 1 :(得分:1)
Spring 2.0的更新。
我的Tomcat在运行应用程序时开始很好,但在我的测试中并没有为我开始。 @Miodigy的回答对我没有用,但是这样做了:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
class TokenTest {
@Autowired private SavedSearchesController controller
@LocalServerPort private int port
重点是:
@SpringBootTest
- 确保添加webEnvironment
参数。
@LocalServerPort
- 这是spring将注入随机端口的地方。
没有webEnvironment
Tomcat在我的测试中没有为我开始。