由 OSGI Felix 容器初始化的模拟私有字段 [英] Mock private field being initialized by OSGI Felix container
本文介绍了由 OSGI Felix 容器初始化的模拟私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试模拟我的类中的私有字段,该字段由运行我的应用程序的 OSGI 容器初始化.我正在提供示例代码以供参考,请对此提供任何线索:
I'm trying to mock a private field in my class which is being initialized by OSGI container in which my app is running. I'm putting a sample code for reference, any clue on this please:
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
Here I'm trying to mock private field MyOtherService myServiceRegistryConsumer
推荐答案
通过 Mockito,您可以使用 @InjectMocks
注释模拟和注入字段.
With Mockito you can mock and inject fields using the @InjectMocks
annotation.
@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 */);
}
}
这篇关于由 OSGI Felix 容器初始化的模拟私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文