Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
superrain51
apollo
提交
aa44e0a0
apollo
项目概览
superrain51
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
aa44e0a0
编写于
4月 20, 2016
作者:
Y
Yiming Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add more testcase for exception
上级
93d1ce67
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
188 addition
and
4 deletion
+188
-4
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AbstractControllerTest.java
...pollo/adminservice/controller/AbstractControllerTest.java
+8
-0
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AppControllerTest.java
...rip/apollo/adminservice/controller/AppControllerTest.java
+2
-2
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/ControllerExceptionTest.java
...ollo/adminservice/controller/ControllerExceptionTest.java
+100
-0
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/ControllerIntegrationExceptionTest.java
...ervice/controller/ControllerIntegrationExceptionTest.java
+75
-0
apollo-adminservice/src/test/resources/application.yml
apollo-adminservice/src/test/resources/application.yml
+1
-1
apollo-adminservice/src/test/resources/bootstrap.yml
apollo-adminservice/src/test/resources/bootstrap.yml
+1
-1
apollo-common/src/main/java/com/ctrip/apollo/common/controller/GlobalDefaultExceptionHandler.java
...ollo/common/controller/GlobalDefaultExceptionHandler.java
+1
-0
未找到文件。
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AbstractControllerTest.java
浏览文件 @
aa44e0a0
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
;
}
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AppControllerTest.java
浏览文件 @
aa44e0a0
...
...
@@ -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
...
...
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/ControllerExceptionTest.java
0 → 100644
浏览文件 @
aa44e0a0
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
;
}
}
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/ControllerIntegrationExceptionTest.java
0 → 100644
浏览文件 @
aa44e0a0
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
;
}
}
apollo-adminservice/src/test/resources/application.yml
浏览文件 @
aa44e0a0
...
...
@@ -3,7 +3,7 @@ spring:
name
:
apollo-adminservice
server
:
port
:
${port:80
8
0}
port
:
${port:80
9
0}
logging
:
level
:
...
...
apollo-adminservice/src/test/resources/bootstrap.yml
浏览文件 @
aa44e0a0
...
...
@@ -3,6 +3,6 @@ eureka:
hostname
:
${hostname:localhost}
client
:
serviceUrl
:
defaultZone
:
http://${eureka.instance.hostname}:80
8
0/eureka/
defaultZone
:
http://${eureka.instance.hostname}:80
9
0/eureka/
healthcheck
:
enabled
:
true
\ No newline at end of file
apollo-common/src/main/java/com/ctrip/apollo/common/controller/GlobalDefaultExceptionHandler.java
浏览文件 @
aa44e0a0
...
...
@@ -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%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录