有了Mockito,我可以做以下事情:
verify(someService).process(any(Person.class));
Run Code Online (Sandbox Code Playgroud)
但是如果process需要一个代码Collection<Person>呢?无法弄清楚如何正确地写它.刚收到语法错误......
protected int parseExpire(CacheContext ctx) throws AttributeDefineException {
Method targetMethod = ctx.getTargetMethod();
CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);
ExpireExpr cacheExpire = targetMethod.getAnnotation(ExpireExpr.class);
// check for duplicate setting
if (cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE && cacheExpire != null) {
throw new AttributeDefineException("expire are defined both in @CacheEnable and @ExpireExpr");
}
// expire time defined in @CacheEnable or @ExpireExpr
return cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE ? cacheEnable.expire() : parseExpireExpr(cacheExpire, ctx.getArgument());
}
Run Code Online (Sandbox Code Playgroud)
这是测试的方法,
Method targetMethod = ctx.getTargetMethod();
CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);
Run Code Online (Sandbox Code Playgroud)
我必须模拟三个CacheContext,Method和CacheEnable.有什么想法让测试用例更简单吗?
是否有可能在代码中告知给定的对象是否是Mockito模拟?
我想这样做的原因是在使用模拟时返回不同的错误消息.这将用于向其他开发人员建议他们应该使用预先准备好的模拟,该模拟已经设置为以有用的方式应答调用,而不是自己创建模拟.
目前我所拥有的最好的是object.getClass().getName().contains("EnhancerByMockitoWithCGLIB")但这感觉很骇人.
我的项目中有一些静态的util方法,其中一些只是传递或抛出异常.关于如何模拟具有除void之外的返回类型的静态方法,有很多例子.但是我如何模拟一个将void返回到" doNothing()" 的静态方法?
非void版本使用以下代码行:
@PrepareForTest(StaticResource.class)
Run Code Online (Sandbox Code Playgroud)
...
PowerMockito.mockStatic(StaticResource.class);
Run Code Online (Sandbox Code Playgroud)
...
Mockito.when(StaticResource.getResource("string")).thenReturn("string");
Run Code Online (Sandbox Code Playgroud)
但是,如果应用于StaticResources返回void,编译将抱怨when(T)不适用于void ...
有任何想法吗?
一个解决方法可能是让所有静态方法返回一些Boolean成功,但我不喜欢变通方法.
我有一些代码
service.doAction(request, Callback<Response> callback);
Run Code Online (Sandbox Code Playgroud)
如何使用Mockito获取回调对象,并调用callback.reply(x)
我面临的问题是Matchers.anyObject()回报null.当用于仅接受非可空类型的mock方法时,它会导致抛出"不应为null"异常.
`when`(mockedBackend.login(anyObject())).thenAnswer { invocationOnMock -> someResponse }
Run Code Online (Sandbox Code Playgroud)
模拟方法:
public open fun login(userCredentials: UserCredentials): Response
Run Code Online (Sandbox Code Playgroud) 我正在尝试用@InjectMocks和进行单元测试@Mock.
@RunWith(MockitoJUnitRunner.class)
public class ProblemDefinitionTest {
@InjectMocks
ProblemDefinition problemDefinition;
@Mock
Matrix matrixMock;
@Test
public void sanityCheck() {
Assert.assertNotNull(problemDefinition);
Assert.assertNotNull(matrixMock);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我不包含@RunWith注释,则测试失败.但
不推荐使用MockitoJUnitRunner类型
我正在使用Mockito 2.6.9.我该怎么办呢?
我需要使用mockito使用final方法模拟一些类.我写过这样的东西
@Test
public void test() {
B b = mock(B.class);
doReturn("bar called").when(b).bar();
assertEquals("must be \"overrided\"", "bar called", b.bar());
//bla-bla
}
class B {
public final String bar() {
return "fail";
}
}
Run Code Online (Sandbox Code Playgroud)
但它失败了.我尝试了一些"黑客",它的确有效.
@Test
public void hackTest() {
class NewB extends B {
public String barForTest() {
return bar();
}
}
NewB b = mock(NewB.class);
doReturn("bar called").when(b).barForTest();
assertEquals("must be \"overrided\"", "bar called", b.barForTest());
}
Run Code Online (Sandbox Code Playgroud)
它有效,但"闻起来".
那么,正确的方式在哪里?
谢谢.
我有一个类,我想用一个调用私有的公共方法测试.我想假设私有方法正常工作.例如,我喜欢类似的东西doReturn....when....我发现有可能使用PowerMock的解决方案,但这个解决方案对我不起作用.怎么做?有没有人有这个问题?
如何使用Mockito和JUnit 5注射?
在JUnit4中,我可以使用@RunWith(MockitoJUnitRunner.class)Annotation.在JUnit5中没有@RunWith注释?
mockito ×10
java ×9
mocking ×4
testing ×3
unit-testing ×3
powermock ×2
deprecated ×1
generics ×1
junit ×1
junit5 ×1
kotlin ×1
parameters ×1
static ×1
verification ×1
void ×1