简体   繁体   English

Mockito-使用@Spy和@InjectMocks时如何注入构造函数标记为@Mock的最终字段

[英]Mockito - how to inject final field marked as @Mock by constructor when using @Spy and @InjectMocks

I have a special case, where I have to mock some fields and use both @Mock and @Spy for my class, which is under test, because at some tests I need to change behaviour of it's own methods. 我有一个特殊情况,我必须模拟某些字段并在正在测试的类中同时使用@Mock和@Spy,因为在某些测试中,我需要更改其自身方法的行为。 I use @InjectMocks for constructor. 我使用@InjectMocks作为构造函数。 It looks like this: 看起来像这样:

Test class: 测试类别:

@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {

    @Mock
    private SomeObject usedAsNonFinal;

    @Mock
    private SomeObject usedAsFinal;

    @Spy
    @InjectMocks
    private ServiceTest service = new ServiceTest(usedAsNonFinal, usedAsFinal);

    @Before
    public void setup() {
        when(usedAsNonFinal.someMethod(Matchers.any()))
                .thenReturn(someObject);

        when(usedAsFinal.someMethod(Matchers.any()))
                .thenReturn(someObject);

        doReturn(someObject).when(service).someMethod(Matchers.any());
    }
}

Service, which is under test: 服务,正在测试中:

@Service
public class Service {

    private SomeObject usedAsNonFinal //injects correctly

    private final SomeObject usedAsFinal; //null - not injected

    @Autowired
    public Service (SomeObject usedAsNonFinal, SomeObject usedAsFinal){
    this.usedAsNonFinal = usedAsNonFinal;
    this.usedAsFinal = usedAsFinal;
    }

    //some methods to mock depending on test
}

The problem is - as commented in code - that final field is not getting injected and it's null. 问题是-正如代码中所注释的-最终字段没有注入并且为空。 When I remove final keyword it gets injected correctly and all tests are working as expected. 当我删除final关键字时,它将正确注入,并且所有测试均按预期进行。 I tried some tricks from other topics - also using reflection, but nothing seems to be workking in this particular case. 我尝试了其他主题的一些技巧-也使用了反射,但是在这种情况下似乎没有任何作用。

Did you try don't instantiate ServiceTest in test? 您是否尝试过不要在测试中实例化ServiceTest

@InjectMocks should do it for you. @InjectMocks应该为您做到这一点。 Try just 尝试一下

@Spy
@InjectMocks
private ServiceTest service;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Mockito 3 中用@InjectMocks 注释的 class 的构造函数中注入最终的 class,例如 String - How to inject final class such as String into constructor of class annotated with @InjectMocks in Mockito 3 Mockito:模拟一个类,但在构造函数受保护时设置final字段的值 - Mockito: Mock a class but set the value of the final field when the constructor is protected 如何在不使用@injectmocks的情况下注入模拟 - how to inject mock without using @injectmocks Mockito @InjectMocks未将@Spy分配给正确的字段 - Mockito @InjectMocks not assigned a @Spy to the correct field Mockito 将模拟注入间谍 object - Mockito Inject mock into Spy object 我该如何模拟通过Powermock和Mockito通过私有构造函数初始化的私有static final字段? - How do I mock a private static final field initialized via a private constructor using Powermock and Mockito? 使用Mockito,如果我使用@InjectMocks将对象注入到我的模拟中,如何避免使其他对象无效? - With Mockito, if I inject an object into my mock with @InjectMocks how do I avoid nulling out other objects? 我如何使用jMockit模拟标记为final并具有私有构造函数的类 - How do I mock a class marked final and has a private constructor using jMockit Mockito @Mock没有使用构造函数注入正确注入命名模拟 - Mockito @Mock does not inject named mocks correctly using constructor injection 使用 mockito 或 Jmockit 模拟私有静态最终字段 - Mock private static final field using mockito or Jmockit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM