Commit aa44e0a0 by Yiming Liu

Add more testcase for exception

parent 93d1ce67
package com.ctrip.apollo.adminservice.controller;
import javax.annotation.PostConstruct;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.AdminServiceTestConfiguration;
......@@ -17,6 +20,11 @@ public abstract class AbstractControllerTest {
RestTemplate restTemplate = new TestRestTemplate("user", "");
@PostConstruct
private void postConstruct() {
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
}
@Value("${local.server.port}")
int port;
}
......@@ -7,6 +7,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.web.client.HttpClientErrorException;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.repository.AppRepository;
......@@ -78,12 +79,11 @@ public class AppControllerTest extends AbstractControllerTest {
Assert.assertEquals(dto.getName(), result.getName());
}
@Test
@Test(expected = HttpClientErrorException.class)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testFindNotExist() {
ResponseEntity<AppDTO> result =
restTemplate.getForEntity(getBaseAppUrl() + "notExists", AppDTO.class);
Assert.assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
}
@Test
......
package com.ctrip.apollo.adminservice.controller;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.util.ReflectionTestUtils;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.service.AdminService;
import com.ctrip.apollo.biz.service.AppService;
import com.ctrip.apollo.core.dto.AppDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
import com.ctrip.apollo.core.exception.ServiceException;
@RunWith(MockitoJUnitRunner.class)
public class ControllerExceptionTest {
private AppController appController;
@Mock
private AppService appService;
@Mock
private AdminService adminService;
@Before
public void setUp() {
appController = new AppController();
ReflectionTestUtils.setField(appController, "appService", appService);
ReflectionTestUtils.setField(appController, "adminService", adminService);
}
@Test(expected = NotFoundException.class)
public void testFindNotExists() {
when(appService.findOne(any(String.class))).thenReturn(null);
appController.get("unexist");
}
@Test(expected = NotFoundException.class)
public void testDeleteNotExists() {
when(appService.findOne(any(String.class))).thenReturn(null);
appController.delete("unexist", null);
}
@Test
public void testFindEmpty() {
when(appService.findAll(any(Pageable.class))).thenReturn(new ArrayList<App>());
Pageable pageable = new PageRequest(0, 10);
List<AppDTO> appDTOs = appController.find(null, pageable);
Assert.assertNotNull(appDTOs);
Assert.assertEquals(0, appDTOs.size());
appDTOs = appController.find("", pageable);
Assert.assertNotNull(appDTOs);
Assert.assertEquals(0, appDTOs.size());
}
@Test
public void testFindByName() {
Pageable pageable = new PageRequest(0, 10);
List<AppDTO> appDTOs = appController.find("unexist", pageable);
Assert.assertNotNull(appDTOs);
Assert.assertEquals(0, appDTOs.size());
}
@Test(expected = ServiceException.class)
public void createFailed() {
AppDTO dto = generateSampleDTOData();
when(appService.findOne(any(String.class))).thenReturn(null);
when(adminService.createNewApp(any(App.class)))
.thenThrow(new ServiceException("create app failed"));
UserDetails user = new User("user", "", new ArrayList<GrantedAuthority>());
appController.createOrUpdate(dto, user);
}
private AppDTO generateSampleDTOData() {
AppDTO dto = new AppDTO();
dto.setAppId("someAppId");
dto.setName("someName");
dto.setOwnerName("someOwner");
dto.setOwnerEmail("someOwner@ctrip.com");
return dto;
}
}
package com.ctrip.apollo.adminservice.controller;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.HttpStatusCodeException;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.service.AdminService;
import com.ctrip.apollo.biz.service.AppService;
import com.ctrip.apollo.core.dto.AppDTO;
import com.google.gson.Gson;
public class ControllerIntegrationExceptionTest extends AbstractControllerTest {
@Autowired
AppController appController;
@Mock
AdminService adminService;
@Autowired
AppService appService;
Gson gson = new Gson();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(appController, "adminService", adminService);
}
private String getBaseAppUrl() {
return "http://localhost:" + port + "/apps/";
}
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateFailed() {
AppDTO dto = generateSampleDTOData();
when(adminService.createNewApp(any(App.class))).thenThrow(new RuntimeException("save failed"));
try {
restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
} catch (HttpStatusCodeException e) {
@SuppressWarnings("unchecked")
Map<String, String> attr = gson.fromJson(e.getResponseBodyAsString(), Map.class);
Assert.assertEquals("save failed", attr.get("message"));
}
App savedApp = appService.findOne(dto.getAppId());
Assert.assertNull(savedApp);
}
private AppDTO generateSampleDTOData() {
AppDTO dto = new AppDTO();
dto.setAppId("someAppId");
dto.setName("someName");
dto.setOwnerName("someOwner");
dto.setOwnerEmail("someOwner@ctrip.com");
return dto;
}
}
......@@ -3,7 +3,7 @@ spring:
name: apollo-adminservice
server:
port: ${port:8080}
port: ${port:8090}
logging:
level:
......
......@@ -3,6 +3,6 @@ eureka:
hostname: ${hostname:localhost}
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8080/eureka/
defaultZone: http://${eureka.instance.hostname}:8090/eureka/
healthcheck:
enabled: true
\ No newline at end of file
......@@ -46,6 +46,7 @@ public class GlobalDefaultExceptionHandler {
errorAttributes.put("timestamp",
LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
errorAttributes.put("exception", resolveError(ex).getClass().getName());
errorAttributes.put("stackTrace", ex.getStackTrace());
if (ex instanceof AbstractBaseException) {
errorAttributes.put("errorCode", ((AbstractBaseException) ex).getErrorCode());
}
......
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