我试图模拟我的类中的一个私有字段,该字段由运行我的应用程序的 OSGI 容器初始化。我放了一个示例代码供参考,请提供任何线索:
import org.apache.felix.scr.annotations.*
@Component (name = "MyServiceImpl ", ds = true, immediate = true)
@Service
public class MyServiceImpl extends MyBasee implements MyService {
@Reference (name = "MyOtherService", bind = "bind", unbind = "unbind", policy = ReferencePolicy.STATIC)
private MyOtherService myServiceRegistryConsumer;
}
在这里我试图嘲笑私人领域MyOtherService myServiceRegistryConsumer
使用 Mockito,您可以使用以下命令模拟和注入字段@InjectMocks
注解。
@RunWith(MockitoJUnitRunner.class)
public class AppTest {
@Mock
private MyOtherService myServiceRegistryConsumer;
@InjectMocks
private MyServiceImpl myServiceImpl;
@Test
public void testSomething() {
// e.g. define behavior for the injected field
when(myServiceRegistryConsumer.methodA()).thenReturn(/* mocked return value */);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)