0
@Component
class ClassA{
@Autowired
ClassB classB;
public void doSomething(){
classD.createValues(a,b);
//create values calls ClassB method
}
}
@Component
class ClassB{
@Autowired
DynamoDBMapper mapper;
public void doSomething(){
mapper.scan(classC.class,new DynamoDBScanExpression()).stream();
}
}
測試類無法模擬的方法
@RunWith(SpringJUnit4ClassRunner.class)
class TestClass{
@InjectMocks
@Autowired
ClassA classA;
@Mock
ClassD classD;
@Autowired
@Qualifier("dynamodbMapper")
private DynamoDBMapper mockedDynamoDBMapper;
// globally mocked in config
@Test
public void testWithValidData() {
A a = new A();
B b = new B();
setUp(classA);
mockDynamoDBCall();
classA.doSomthing();
}
private void setUp(ClassA classA){
Mockito.when(classD.createValues(a,b)).thenReturn(Matchers.any(Reponse.class)); // problem after mockDynamoDBCall()
}
private void mockDynamoDBCall(){
when(mapper.scan(Mockito.eq(Object.class), Mockito.any(DynamoDBScanExpression.class))).
thenReturn(mockPaginatedScanList);
when(mockPaginatedScanList.stream()).thenReturn(createDummyData().stream());
}
}
,當我不是嘲笑DynamoDBMapper其工作的罰款。
嘲諷DynamoDB映射器後,它處在設置方法拋出異常
[junit] Caused an ERROR
[junit]
[junit] Invalid use of argument matchers!
[junit] 2 matchers expected, 1 recorded:
[junit] -> at <class name>
[junit]
[junit] This exception may occur if matchers are combined with raw values:
[junit] //incorrect:
[junit] someMethod(anyObject(), "raw String");
[junit] When using matchers, all arguments have to be provided by matchers.
[junit] For example:
[junit] //correct:
[junit] someMethod(anyObject(), eq("String by matcher"));
[junit]
[junit] For more info see javadoc for Matchers class.
[junit]
[junit] org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
[junit] Invalid use of argument matchers!
[junit] 2 matchers expected, 1 recorded:
[junit] -> at <class name>
[junit]
[junit] This exception may occur if matchers are combined with raw values:
[junit] //incorrect:
[junit] someMethod(anyObject(), "raw String");
[junit] When using matchers, all arguments have to be provided by matchers.
[junit] For example:
[junit] //correct:
[junit] someMethod(anyObject(), eq("String by matcher"));
我試圖通過Matchers.any(類名)和Matcher.any()作爲參數,但仍然我得到相同的異常
我知道,但添加mockDynamoDBCall()之前爲什麼它工作正常?任何想法? –
是的,所有創建「匹配器」的方法實際上都將Mockito自己的內部堆棧上的「匹配器」,等待存根或驗證呼叫。如果您沒有進行存根或驗證電話,Mockito無法檢測到錯誤。 –
你能否向我提供有關這方面的任何文件? –