我有:
public class A extends B {
public ObjectC methodToTest() {
return getSomething();
}
}
/* this class is in other project and compiles in project I want test */
public class B {
public ObjectC getSomething() {
//some stuff calling external WS
}
}
并且在测试中:
@RunWith(MockitoJUnitRunner.class)
public class ATest {
@Mock
B bMock;
@InjectMocks
A classTotest;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getMethodToTestShouldReturnObjectC() {
Mockito.when(bMock.getSomething()).thenReturn(new ObjectC());
assertEquals(classTotest.methodToTest().getClass, ObjectC.class);
}
}
但是当我运行测试时,Mockito正在调用B(并且因为调用ws而失败...)
我读了很多关于此的内容,但我无法解决。
¿我如何模拟getSomething()来返回ObjectC?
答案 0 :(得分:1)
Mockito.when(bMock.getSomething()).thenReturn(new ObjectC());
此方法仅更改 bMock 。它不会更改任何其他类 B (或 A )实例。