我尝试使用Mockito来模拟一个类的行为.这使用了Mockito 1.x. 迁移到JUnit 5和Mockito 2似乎不再起作用了.
@ExtendWith(MockitoExtension.class)
public class MockitoExample {
static abstract class TestClass {
public abstract int booleanMethod(boolean arg);
}
@Mock
TestClass testClass;
@BeforeEach
public void beforeEach() {
when(testClass.booleanMethod(eq(true))).thenReturn(1);
when(testClass.booleanMethod(eq(false))).thenReturn(2);
}
@Test
public void test() {
assertEquals(1,testClass.booleanMethod(true));
assertEquals(2,testClass.booleanMethod(false));
}
}
Run Code Online (Sandbox Code Playgroud)
期望的是,模拟的TestClass显示了在测试方法中测试的行为.
我得到的错误是:
org.mockito.exceptions.misusing.PotentialStubbingProblem:
Strict stubbing argument mismatch. Please check:
- this invocation of 'booleanMethod' method:
testClass.booleanMethod(false);
-> at org.oneandone.ejbcdiunit.mockito_example.MockitoExample.beforeEach(MockitoExample.java:30)
- has following stubbing(s) with different arguments:
1. testClass.booleanMethod(false);
-> at org.oneandone.ejbcdiunit.mockito_example.MockitoExample.beforeEach(MockitoExample.java:29)
Typically, stubbing argument mismatch indicates user mistake when …Run Code Online (Sandbox Code Playgroud) 我想处理一个从文件读取为4字节整数的值,就好像它是一个4字节的IEEE Float并将其转换为这样的变量.有没有经验,如何以简单优雅的方式在Kotlin中完成.
forEach哪个,这意味着只会查看20个整数?码:
class Tests {
@Test
fun test() {
var counter = 0
(1..10_000_000).filter { it % 2 == 1 }.forEach {
counter++
if (counter > 10)
return
}
}
}
Run Code Online (Sandbox Code Playgroud) 鉴于以下课程
private static class ProducedInSubClass {
}
private static class ProducedInSuperClass {
}
public static class SuperClass {
@Produces
public ProducedInSuperClass producedInSuperClass = new ProducedInSuperClass();
}
public static class SubClass extends SuperClass {
@Produces
ProducedInSubClass producedInSubClass = new ProducedInSubClass();
}
public static class BeanWithSubClass {
@Inject
SubClass subClass;
@Inject
ProducedInSuperClass producedInSuperClass;
@Inject
ProducedInSubClass producedInSubClass;
}
Run Code Online (Sandbox Code Playgroud)
ProducedInSuperClass的注入仍然不满意.这与CDI-Spec第4.2章一致,我知道.
为了使这项工作,我需要扩展SubClass
@Produces
ProducedInSuperClass producedInSuperClassInSubClass = producedInSuperClass;
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释一下吗?为什么Injects,Annotations Interceptors ...继承但不是生产者?
java ×2
kotlin ×2
cdi ×1
collections ×1
junit5 ×1
mocking ×1
mockito ×1
stream ×1
unit-testing ×1
weld ×1