![](/img/trans.png)
[英]Spring ReflectionTestUtils does not set static final field
[英]Spring ReflectionTestUtils does not set primitive data type field
我使用ReflectionTestUtils在服务类中设置int字段来测试该类。 我的服务类别如下:
@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);
}
}
测试类是这样的:
@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);
}
}
当我运行此测试用例时,它给出以下错误:
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
为什么ReflectionTestUtils尝试在字段中设置字符串值?
我输入了两个字段,一个是整数,另一个是用于测试目的的字符串。
您可以在此处找到源代码。
请看看并提出解决方法。
您的问题是,在初始化spring上下文并将服务由Spring注入您的测试类之后 ,将调用用@Before
注释的方法。
这意味着这两个字段:
@Value("${app.count}")
private int count;
@Value("${app.countStr}")
private String countStr;
将具有@Value
值中定义的值作为值。
String countStr
可以与估价"${app.countStr}"
String
(即使它是没有意义的)。
但是int count
不能用"${app.count}"
String
来赋值,因为不能将"${app.count}"
转换为int值 。
而作为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)
若要解决此问题, @TestPropertySource
按照Nisheeth Shah的建议使用@TestPropertySource
,以在适当的时间提供属性的值。
作为一般建议,限制反射的使用。 它仅在运行时检查,并且通常更加不透明。
测试时,您必须提供属性源。 如果不添加属性,它将在变量@Value
中插入值。 在您的情况下,它将尝试将字符串添加到给出NumberFormatException
的整数中。 尝试如下添加:
@RunWith(SpringRunner.class)
@TestPropertySource(properties = {"app.count=1", "app.countStr=sampleString"})
public class SampleServiceTest{...}
希望对您有所帮助。
在使用@Autowired
,在ReflectionTestUtils
之前,它将尝试在@Value
添加值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.