0
我試過Exception : mockito wanted but not invoked, Actually there were zero interactions with this mock給出的解決方案,這也是Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock但仍然收到相同的錯誤。我錯過了什麼嗎? 下面是我實現: -通緝但未啓用。其實,這個模擬零交互
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.verify;
class ClassOne {
public void method(ClassTwo two) {
}
}
class ClassTwo {
public ClassTwo(String value) {
}
}
class MyClassTwo extends ClassTwo {
public MyClassTwo(String value) {
super(value);
}
}
class ClassToBeTested {
ClassOne classOne;
public ClassToBeTested() {
classOne = new ClassOne();
}
public void methodToBeTested() {
ClassTwo event = new MyClassTwo("String");
classOne.method(event);
}
}
@RunWith(MockitoJUnitRunner.class)
public class ClassToBeTestedTest {
ClassToBeTested presenter;
@Mock
ClassOne classOne;
@Before
public void setUp() {
presenter = new ClassToBeTested();
}
@Test
public void testAbc() {
presenter.methodToBeTested();
ClassTwo event = new MyClassTwo("someString");
verify(classOne).method(event);
}
}
我按照你的建議改變了我的類,使其在構造函數中擁有ClassOne的實例,但仍然沒有運氣。我也嘗試過使用不同的構造函數,它會在調用 'public ClassToBeTested(){new ClassOne())之後調用新的構造函數。 }' – adarsh
如果您提供的代碼實際編譯並運行顯示您的問題,這將有很大幫助。更新與工作示例 –
這正是我的代碼看起來像,但仍然是同樣的問題。我的軟件包中有更多的測試課程,但它並沒有問題,但沒有問題。我想別的是錯的:( – adarsh