抽象中的模拟依赖 parent class
Mocking dependency inside abstract parent class
我有以下一组 classes:
public abstract class ParentClass {
@Autowired
private SomeService service;
protected Item getItem() {
return service.foo();
}
protected abstract doSomething();
}
@Component
public ChildClass extends ParentClass {
private final SomeOtherService someOtherService;
@Override
protected doSomething() {
Item item = getItem(); //invoking parent class method
.... do some stuff
}
}
正在尝试测试 Child class:
@RunWith(MockitoJUnitRunner.class)
public class ChildClassTest {
@Mock
private SomeOtherService somerOtherService;
@Mock
private SomerService someService; //dependency at parent class
@InjectMocks
private ChildClass childClass;
public void testDoSomethingMethod() {
Item item = new Item();
when(someService.getItem()).thenReturn(item);
childClass.doSomething();
}
}
问题是我总是收到 NullPointerException,因为 parent 依赖项 (SomeService) 始终为 null。
也尝试过:
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
return new Item();
}
}).when(someService).getItem();
并使用 Spy,没有任何成功。
感谢您的提示。
一个选项是使用 ReflectionTestUtils class 来注入模拟。在下面的代码中,我使用 JUnit 4 执行了单元测试。
@RunWith(MockitoJUnitRunner.class)
public class ChildClassTest {
@Mock
private SomeService someService;
@Test
public void test_something() {
ChildClass childClass = new ChildClass();
ReflectionTestUtils.setField(childClass, "service", someService);
when(someService.foo()).thenReturn("Test Foo");
assertEquals("Child Test Foo", childClass.doSomething());
}
}
我有以下一组 classes:
public abstract class ParentClass {
@Autowired
private SomeService service;
protected Item getItem() {
return service.foo();
}
protected abstract doSomething();
}
@Component
public ChildClass extends ParentClass {
private final SomeOtherService someOtherService;
@Override
protected doSomething() {
Item item = getItem(); //invoking parent class method
.... do some stuff
}
}
正在尝试测试 Child class:
@RunWith(MockitoJUnitRunner.class)
public class ChildClassTest {
@Mock
private SomeOtherService somerOtherService;
@Mock
private SomerService someService; //dependency at parent class
@InjectMocks
private ChildClass childClass;
public void testDoSomethingMethod() {
Item item = new Item();
when(someService.getItem()).thenReturn(item);
childClass.doSomething();
}
}
问题是我总是收到 NullPointerException,因为 parent 依赖项 (SomeService) 始终为 null。
也尝试过:
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
return new Item();
}
}).when(someService).getItem();
并使用 Spy,没有任何成功。
感谢您的提示。
一个选项是使用 ReflectionTestUtils class 来注入模拟。在下面的代码中,我使用 JUnit 4 执行了单元测试。
@RunWith(MockitoJUnitRunner.class)
public class ChildClassTest {
@Mock
private SomeService someService;
@Test
public void test_something() {
ChildClass childClass = new ChildClass();
ReflectionTestUtils.setField(childClass, "service", someService);
when(someService.foo()).thenReturn("Test Foo");
assertEquals("Child Test Foo", childClass.doSomething());
}
}