简体   繁体   English

ReflectionTestUtils 设置字段方法

[英]ReflectionTestUtils set field method

Is there a way to use ReflectionTestUtils to set the return value of a method inside a field that represents a class to a mocked value instead of mocking the entire field?有没有办法使用 ReflectionTestUtils 将表示类的字段内的方法的返回值设置为模拟值而不是模拟整个字段? I was trying .setField(), but that only seems to work for the entire field.我正在尝试 .setField(),但这似乎只适用于整个领域。 If there isn't, what would be a good substitute?如果没有,什么是好的替代品? Example of what I mean below:我在下面的意思的例子:

public class Example() {
    private ClassField field;
    
    public methodThatUsesField() {

        methodReturnType type = field.method(); // I was trying to call .setField() to change the field's method to a mocked value, but can't figure out how to do it
        ...
    }
}

The class that is called in the field is very complicated, but there is a very simple public method that acts as the root of the class, and I want to set that to a specific value.在字段中调用的类非常复杂,但是有一个非常简单的公共方法充当类的根,我想将其设置为特定值。 The class itself does not have a constructor, so I need a way to get around that.类本身没有构造函数,所以我需要一种方法来解决这个问题。

This is a spring boot project written in Java, and I need to use ReflectionTestUtils to be able to pass an argument into Mockito mock这是一个用 Java 编写的 spring boot 项目,我需要使用 ReflectionTestUtils 才能将参数传递给 Mockito mock

You can use Mockito.spy(field) and inject spied field using ReflectionTestUtils .您可以使用Mockito.spy(field)并使用ReflectionTestUtils注入间谍字段。 Something like this像这样的东西

@SpringBootTest
class ExampleTest {

    @Autowired
    private Example example;

    @Autowired
    private ClassField field;

    @Test
    void testMethodThatUsesField() {
        ClassField spiedField = Mockito.spy(field);
        Mockito.when(spiedField.method()).thenReturn(methodReturnTypeValue);
        ReflectionTestUtils.setField(example, "field", spiedField);
        example.methodThatUsesField();
     // assertions
    }
}

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

相关问题 Spring ReflectionTestUtils没有设置静态的final字段 - Spring ReflectionTestUtils does not set static final field Spring ReflectionTestUtils没有设置原始数据类型字段 - Spring ReflectionTestUtils does not set primitive data type field ReflectionTestUtils 设置带有弹簧值注释返回 NPE 的字段 - ReflectionTestUtils set field with spring value annotation returning NPE ReflectionTestUtils.setField(Mockito),无法识别字段。 - ReflectionTestUtils.setField (Mockito), not recognizing field. ReflectionTestUtils - 在目标 object 上设置 [null] 类型的字段 - ReflectionTestUtils - Setting field of type [null] on target object 使用org.springframework.test.util.ReflectionTestUtils测试方法 - Test a method using the org.springframework.test.util.ReflectionTestUtils 如何使用 ReflectionTestUtils.setField() 设置私有字符串数组? - How to use ReflectionTestUtils.setField() to set a private String array? Spring bean 方法不使用 ReflectionTestUtils.setField 注入的字段值 - Spring bean methods don't use the field value injected by ReflectionTestUtils.setField ReflectionTestUtils是否仅适用于类的字段,而不适用于该类的方法内部定义的变量? - Does ReflectionTestUtils works only on fields of a class, not on variables defined inside a method of that class? ReflectionTestUtils无法与MockitoJunitRunner一起使用 - ReflectionTestUtils not working with MockitoJunitRunner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM