Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wd1105040417
retrofit
提交
ce787ed5
R
retrofit
项目概览
wd1105040417
/
retrofit
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
retrofit
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ce787ed5
编写于
3月 31, 2018
作者:
W
Warren Smith
提交者:
Jake Wharton
4月 16, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for creating failed test call instances with any exception.
上级
5c2f505a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
53 addition
and
6 deletion
+53
-6
retrofit-mock/src/main/java/retrofit2/mock/Calls.java
retrofit-mock/src/main/java/retrofit2/mock/Calls.java
+22
-6
retrofit-mock/src/test/java/retrofit2/mock/CallsTest.java
retrofit-mock/src/test/java/retrofit2/mock/CallsTest.java
+31
-0
未找到文件。
retrofit-mock/src/main/java/retrofit2/mock/Calls.java
浏览文件 @
ce787ed5
...
...
@@ -42,7 +42,20 @@ public final class Calls {
return
new
FakeCall
<>(
response
,
null
);
}
/** Creates a failed {@link Call} from {@code failure}. */
public
static
<
T
>
Call
<
T
>
failure
(
IOException
failure
)
{
// TODO delete this overload in Retrofit 3.0.
return
new
FakeCall
<>(
null
,
failure
);
}
/**
* Creates a failed {@link Call} from {@code failure}.
* <p>
* Note: When invoking {@link Call#execute() execute()} on the returned {@link Call}, if
* {@code failure} is a {@link RuntimeException}, {@link Error}, or {@link IOException} subtype
* it is thrown directly. Otherwise it is "sneaky thrown" despite not being declared.
*/
public
static
<
T
>
Call
<
T
>
failure
(
Throwable
failure
)
{
return
new
FakeCall
<>(
null
,
failure
);
}
...
...
@@ -52,11 +65,11 @@ public final class Calls {
static
final
class
FakeCall
<
T
>
implements
Call
<
T
>
{
private
final
Response
<
T
>
response
;
private
final
IOException
error
;
private
final
Throwable
error
;
private
final
AtomicBoolean
canceled
=
new
AtomicBoolean
();
private
final
AtomicBoolean
executed
=
new
AtomicBoolean
();
FakeCall
(
@Nullable
Response
<
T
>
response
,
@Nullable
IOException
error
)
{
FakeCall
(
@Nullable
Response
<
T
>
response
,
@Nullable
Throwable
error
)
{
if
((
response
==
null
)
==
(
error
==
null
))
{
throw
new
AssertionError
(
"Only one of response or error can be set."
);
}
...
...
@@ -74,7 +87,12 @@ public final class Calls {
if
(
response
!=
null
)
{
return
response
;
}
throw
error
;
throw
FakeCall
.<
Error
>
sneakyThrow2
(
error
);
}
@SuppressWarnings
(
"unchecked"
)
// Intentionally abusing this feature.
private
static
<
T
extends
Throwable
>
T
sneakyThrow2
(
Throwable
t
)
throws
T
{
throw
(
T
)
t
;
}
@SuppressWarnings
(
"ConstantConditions"
)
// Guarding public API nullability.
...
...
@@ -131,10 +149,8 @@ public final class Calls {
if
(
delegate
==
null
)
{
try
{
delegate
=
callable
.
call
();
}
catch
(
IOException
e
)
{
delegate
=
failure
(
e
);
}
catch
(
Exception
e
)
{
throw
new
IllegalStateException
(
"Callable threw unrecoverable exception"
,
e
);
delegate
=
failure
(
e
);
}
this
.
delegate
=
delegate
;
}
...
...
retrofit-mock/src/test/java/retrofit2/mock/CallsTest.java
浏览文件 @
ce787ed5
...
...
@@ -15,13 +15,16 @@
*/
package
retrofit2.mock
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.security.cert.CertificateException
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.atomic.AtomicReference
;
import
org.junit.Test
;
import
retrofit2.Call
;
import
retrofit2.Callback
;
import
retrofit2.Response
;
import
retrofit2.Retrofit
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
...
@@ -155,6 +158,19 @@ public final class CallsTest {
assertTrue
(
taco
.
isExecuted
());
}
@Test
public
void
failureExecuteCheckedException
()
{
CertificateException
failure
=
new
CertificateException
(
"Hey"
);
Call
<
Object
>
taco
=
Calls
.
failure
(
failure
);
assertFalse
(
taco
.
isExecuted
());
try
{
taco
.
execute
();
fail
();
}
catch
(
Throwable
e
)
{
assertSame
(
failure
,
e
);
}
assertTrue
(
taco
.
isExecuted
());
}
@Test
public
void
failureEnqueue
()
{
IOException
failure
=
new
IOException
(
"Hey"
);
Call
<
Object
>
taco
=
Calls
.
failure
(
failure
);
...
...
@@ -260,4 +276,19 @@ public final class CallsTest {
});
assertSame
(
failure
,
failureRef
.
get
());
}
@Test
public
void
deferredThrowUncheckedExceptionEnqueue
()
{
final
RuntimeException
failure
=
new
RuntimeException
(
"Hey"
);
final
AtomicReference
<
Throwable
>
failureRef
=
new
AtomicReference
<>();
Calls
.
failure
(
failure
).
enqueue
(
new
Callback
<
Object
>()
{
@Override
public
void
onResponse
(
Call
<
Object
>
call
,
Response
<
Object
>
response
)
{
fail
();
}
@Override
public
void
onFailure
(
Call
<
Object
>
call
,
Throwable
t
)
{
failureRef
.
set
(
t
);
}
});
assertSame
(
failure
,
failureRef
.
get
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录