简体   繁体   English

Spring ReflectionTestUtils没有设置原始数据类型字段

[英]Spring ReflectionTestUtils does not set primitive data type field

I use ReflectionTestUtils to set int field in my service class to test the class. 我使用ReflectionTestUtils在服务类中设置int字段来测试该类。 My service class like: 我的服务类别如下:

@Service
public class SampleService {

    @Value("${app.count}")
    private int count;

    @Value("${app.countStr}")
    private String countStr;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getCountStr() {
        return countStr;
    }

    public void setCountStr(String countStr) {
        this.countStr = countStr;
    }

    @PostConstruct
    public int demoMethod() {
        return count + Integer.parseInt(countStr);
    }
}

and test class is like: 测试类是这样的:

@RunWith(SpringRunner.class)
public class SampleServiceTest {

    @Autowired
    private SampleService sampleService;

    @TestConfiguration
    static class SampleServiceTestConfig {

        @Bean
        public SampleService sampleService() {
            return new SampleService();
        }
    }

    @Before
    public void init() {
        ReflectionTestUtils.setField(sampleService, "count", new Integer(100));
        ReflectionTestUtils.setField(sampleService, "countStr", 100);
    }

    @Test
    public void testDemoMethod() {
        int a  = sampleService.demoMethod();
        Assert.assertTrue(a == 200);
    }
}

While I run this test case it gives below error: 当我运行此测试用例时,它给出以下错误:

Caused by: java.lang.NumberFormatException: For input string: "${app.count}"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:569)
    at java.lang.Integer.valueOf(Integer.java:766)
    at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:210)
    at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115)
    at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:466)
    at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:439)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:192)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:117)
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:70)
    ... 47 more

Why ReflectionTestUtils try set string value in the field? 为什么ReflectionTestUtils尝试在字段中设置字符串值?

I put 2 fields, one is an integer and another is a string for testing purpose. 我输入了两个字段,一个是整数,另一个是用于测试目的的字符串。

You can find source code here . 您可以在此处找到源代码。

Please have look and suggest a workaround for the same. 请看看并提出解决方法。

Your problem is that the method annotated with @Before is invoked after that the spring context was initialized and that the service was injected by Spring in your test class. 您的问题是,在初始化spring上下文并将服务由Spring注入您的测试类之后 ,将调用用@Before注释的方法。
Which means that these two fields : 这意味着这两个字段:

@Value("${app.count}")
private int count;

@Value("${app.countStr}")
private String countStr;

will have as value the value defined in their @Value values. 将具有@Value值中定义的值作为值。
String countStr can be valued with the "${app.countStr}" String (even it it makes no sense). String countStr可以与估价"${app.countStr}" String (即使它是没有意义的)。
But int count cannot be valued with the "${app.count}" String as "${app.count}" cannot be converted into an int value . 但是int count不能用"${app.count}" String来赋值,因为不能将"${app.count}"转换为int值
Whereas the thrown exception as Integer.parseInt("${app.count}") is invoked : 而作为Integer.parseInt("${app.count}")引发的异常被调用:

Caused by: java.lang.NumberFormatException: For input string: "${app.count}"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:569)

To address this issue, use @TestPropertySource as suggested by Nisheeth Shah to provide the value for the properties at the suitable time. 若要解决此问题, @TestPropertySource按照Nisheeth Shah的建议使用@TestPropertySource ,以在适当的时间提供属性的值。

As a general advise, limit the reflection use. 作为一般建议,限制反射的使用。 It is checked only at runtime and that is often more opaque. 它仅在运行时检查,并且通常更加不透明。

While testing, you have to provide properties source. 测试时,您必须提供属性源。 If you don't add properties, it will inject the value inside @Value in the variable. 如果不添加属性,它将在变量@Value中插入值。 In your case, it tries to add the string in the integer that gives NumberFormatException . 在您的情况下,它将尝试将字符串添加到给出NumberFormatException的整数中。 Try adding as below: 尝试如下添加:

@RunWith(SpringRunner.class)
@TestPropertySource(properties = {"app.count=1", "app.countStr=sampleString"})
public class SampleServiceTest{...}

I hope it helps. 希望对您有所帮助。

As you are using @Autowired , before ReflectionTestUtils it tries to add the value inside @Value . 在使用@Autowired ,在ReflectionTestUtils之前,它将尝试在@Value添加值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM