Commit 92ae3e27 by Yiming Liu

Isolate SpringBootAplication from integration test

parent 5bb2aaf1
package com.ctrip.apollo; package com.ctrip.apollo;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@SpringBootApplication(exclude = {SampleAdminServiceApplication.class, @Configuration
EurekaClientAutoConfiguration.class}) @ComponentScan(excludeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE, value = {
SampleAdminServiceApplication.class, AdminServiceApplication.class})})
@EnableAutoConfiguration
public class AdminServiceTestConfiguration { public class AdminServiceTestConfiguration {
} }
...@@ -4,6 +4,7 @@ import org.junit.Assert; ...@@ -4,6 +4,7 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest; import org.springframework.boot.test.WebIntegrationTest;
...@@ -20,7 +21,7 @@ import com.ctrip.apollo.core.dto.AppDTO; ...@@ -20,7 +21,7 @@ import com.ctrip.apollo.core.dto.AppDTO;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AdminServiceTestConfiguration.class) @SpringApplicationConfiguration(classes = AdminServiceTestConfiguration.class)
@WebIntegrationTest @WebIntegrationTest(randomPort = true)
public class AppControllerTest { public class AppControllerTest {
RestTemplate restTemplate = new TestRestTemplate(); RestTemplate restTemplate = new TestRestTemplate();
...@@ -28,11 +29,18 @@ public class AppControllerTest { ...@@ -28,11 +29,18 @@ public class AppControllerTest {
@Autowired @Autowired
AppRepository appRepository; AppRepository appRepository;
@Value("${local.server.port}")
private int port;
private String getBaseAppUrl(){
return "http://localhost:"+port+"/apps/";
}
@Test @Test
public void testCreate() { public void testCreate() {
AppDTO dto = generateSampleDTOData(); AppDTO dto = generateSampleDTOData();
ResponseEntity<AppDTO> response = ResponseEntity<AppDTO> response =
restTemplate.postForEntity("http://localhost:8090/apps/", dto, AppDTO.class); restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
AppDTO result = response.getBody(); AppDTO result = response.getBody();
Assert.assertEquals(HttpStatus.CREATED, response.getStatusCode()); Assert.assertEquals(HttpStatus.CREATED, response.getStatusCode());
Assert.assertEquals(dto.getAppId(), result.getAppId()); Assert.assertEquals(dto.getAppId(), result.getAppId());
...@@ -52,7 +60,7 @@ public class AppControllerTest { ...@@ -52,7 +60,7 @@ public class AppControllerTest {
app = appRepository.save(app); app = appRepository.save(app);
AppDTO result = AppDTO result =
restTemplate.getForObject("http://localhost:8090/apps/" + dto.getAppId(), AppDTO.class); restTemplate.getForObject(getBaseAppUrl() + dto.getAppId(), AppDTO.class);
Assert.assertEquals(dto.getAppId(), result.getAppId()); Assert.assertEquals(dto.getAppId(), result.getAppId());
Assert.assertEquals(dto.getName(), result.getName()); Assert.assertEquals(dto.getName(), result.getName());
...@@ -62,7 +70,7 @@ public class AppControllerTest { ...@@ -62,7 +70,7 @@ public class AppControllerTest {
@Test @Test
public void testFindNotExist() { public void testFindNotExist() {
ResponseEntity<AppDTO> result = ResponseEntity<AppDTO> result =
restTemplate.getForEntity("http://localhost:8090/apps/" + "notExists", AppDTO.class); restTemplate.getForEntity(getBaseAppUrl() + "notExists", AppDTO.class);
Assert.assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode()); Assert.assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
} }
...@@ -72,7 +80,7 @@ public class AppControllerTest { ...@@ -72,7 +80,7 @@ public class AppControllerTest {
App app = BeanUtils.transfrom(App.class, dto); App app = BeanUtils.transfrom(App.class, dto);
app = appRepository.save(app); app = appRepository.save(app);
restTemplate.delete("http://localhost:8090/apps/" + dto.getAppId()); restTemplate.delete(getBaseAppUrl() + dto.getAppId());
App deletedApp = appRepository.findOne(app.getId()); App deletedApp = appRepository.findOne(app.getId());
Assert.assertNull(deletedApp); Assert.assertNull(deletedApp);
...@@ -85,7 +93,7 @@ public class AppControllerTest { ...@@ -85,7 +93,7 @@ public class AppControllerTest {
app = appRepository.save(app); app = appRepository.save(app);
dto.setName("newName"); dto.setName("newName");
restTemplate.put("http://localhost:8090/apps/" + dto.getAppId(), dto); restTemplate.put(getBaseAppUrl() + dto.getAppId(), dto);
App updatedApp = appRepository.findOne(app.getId()); App updatedApp = appRepository.findOne(app.getId());
Assert.assertEquals(dto.getName(), updatedApp.getName()); Assert.assertEquals(dto.getName(), updatedApp.getName());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment