java - OSGI Felix 容器正在初始化模拟私有(private)字段

标签 java mockito apache-felix osgi-bundle

我正在尝试模拟我的类中的一个私有(private)字段,该字段由运行我的应用程序的 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;
}

在这里,我尝试模拟私有(private)字段MyOtherService myServiceRegistryConsumer

最佳答案

使用 Mockito,您可以使用 @InjectMocks 注释来模拟和注入(inject)字段。

@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 */);
    }
} 

关于java - OSGI Felix 容器正在初始化模拟私有(private)字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44839906/

相关文章:

java - Guice 辅助注入(inject)已配置

java - 如何使用 Mockito @InjectMocks 将 HttpServletRequest 注入(inject) ContainerRequestFilter

osgi - 这些OSGi命令实际上是做什么的?

java - MockRestServiceServer 在集成测试中模拟后端超时

java - 禁用 osgi 临时文件夹

java - 无法在 OSGi Apache Felix Web 控制台中添加加号按钮来创建新的配置实例

java - Liquibase 输入中的空 <databaseChangeLog> 会为我的 Spring 实体创建初始 DDL 脚本吗?

java - spring-amqp- 较新的调用监听器

java - registerNatives() 方法有什么作用?

java - Mockito 正在调用真正的方法而不是 stub