[英]Mock private field being initialized by OSGI Felix container
我試圖在我的類中模擬一個私有字段,該私有字段由運行我的應用程序的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 */);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.