如何使用 java 中的嵌套静态 class 对 class 进行单元测试?
How to Unit Test a class with a nested static class in java?
我必须为此 class 编写一个包含嵌套静态 class 的单元测试。我不知道如何初始化“_Deferred”的class变量“mUri”来断言它不为空。
public class _Deferred {
@SerializedName("uri")
private String mUri;
public String getUri() {
return mUri;
}
public static class Builder {
private String mUri;
public _Deferred.Builder withUri(String uri) {
mUri = uri;
return this;
}
public _Deferred build() {
_Deferred _Deferred = new _Deferred();
_Deferred.mUri = mUri;
return _Deferred;
}
}
}
感谢任何有关如何初始化“mUri”的帮助。
我试过的是:
@Test
public void testGetUriNotNull() {
//preparations
_deferred = new _Deferred();
_Deferred.Builder builder = new _Deferred.Builder();
}
但是我不知道如何设置“mUri”。
只需为 mUri
字段
调用 Builder
方法 0n _Deferred
class
public class Whosebug {
@Test
public void deferredTest(){
_Deferred deferred = new _Deferred.Builder().withUri("some_uri").build();
Assertions.assertNotNull(deferred.getUri());
}
}
测试将成功通过。
我必须为此 class 编写一个包含嵌套静态 class 的单元测试。我不知道如何初始化“_Deferred”的class变量“mUri”来断言它不为空。
public class _Deferred {
@SerializedName("uri")
private String mUri;
public String getUri() {
return mUri;
}
public static class Builder {
private String mUri;
public _Deferred.Builder withUri(String uri) {
mUri = uri;
return this;
}
public _Deferred build() {
_Deferred _Deferred = new _Deferred();
_Deferred.mUri = mUri;
return _Deferred;
}
}
}
感谢任何有关如何初始化“mUri”的帮助。
我试过的是:
@Test
public void testGetUriNotNull() {
//preparations
_deferred = new _Deferred();
_Deferred.Builder builder = new _Deferred.Builder();
}
但是我不知道如何设置“mUri”。
只需为 mUri
字段
Builder
方法 0n _Deferred
class
public class Whosebug {
@Test
public void deferredTest(){
_Deferred deferred = new _Deferred.Builder().withUri("some_uri").build();
Assertions.assertNotNull(deferred.getUri());
}
}
测试将成功通过。