Java Spring ReflectionTestUtils未设置基元数据类型字段

Java Spring ReflectionTestUtils未设置基元数据类型字段,java,spring-boot,junit,spring-test,Java,Spring Boot,Junit,Spring Test,我使用ReflectionTestUtils在我的服务类中设置int字段来测试该类。 我的服务级别如下: @Service public class SampleService { @Value("${app.count}") private int count; @Value("${app.countStr}") private String countStr; public int getCount() { return coun

我使用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尝试在字段中设置字符串值

我放了两个字段,一个是整数,另一个是字符串,用于测试

你可以找到源代码


请查看并建议解决方法。

测试时,您必须提供源属性。如果不添加属性,它将在变量的
@value
中注入值。在您的例子中,它尝试在整数中添加字符串,该整数给出
NumberFormatException
。 尝试按以下方式添加:

@RunWith(SpringRunner.class)
@TestPropertySource(properties = {"app.count=1", "app.countStr=sampleString"})
public class SampleServiceTest{...}
我希望有帮助


当您使用
@Autowired
时,在
ReflectionTestUtils
之前,它尝试在测试时在
@value

中添加值,您必须提供源属性。如果不添加属性,它将在变量的
@value
中注入值。在您的例子中,它尝试在整数中添加字符串,该整数给出
NumberFormatException
。 尝试按以下方式添加:

@RunWith(SpringRunner.class)
@TestPropertySource(properties = {"app.count=1", "app.countStr=sampleString"})
public class SampleServiceTest{...}
我希望有帮助


当您使用自动连线时,在
ReflectionTestUtils
之前,它尝试在
@value

中添加值。您的问题是,在初始化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}”进行赋值,因为
无法将“${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) 原因:java.lang.NumberFormatException:对于输入字符串:“${app.count}” 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:569) 要解决此问题,请按照Nisheeth Shah的建议使用
@TestPropertySource
,在适当的时间提供属性的值


一般建议限制反射的使用。它只在运行时被检查,这通常更加不透明

您的问题是,在初始化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}”进行赋值,因为
无法将“${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) 原因:java.lang.NumberFormatException:对于输入字符串:“${app.count}” 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:569) 要解决此问题,请按照Nisheeth Shah的建议使用
@TestPropertySource
,在适当的时间提供属性的值


一般建议限制反射的使用。它只在运行时被检查,这通常更加不透明

如果您将
count
的类型更改为String,那么它将工作如果您将
count
的类型更改为String,那么它将工作这是一个解决问题的方法。
ReflectionTestUtils.setField
有一个对象类型的参数,那么它不应该注入String。它根本没有注入任何东西。该错误将在代码出现在图片中之前产生,伙计。这是该问题的解决方法。
ReflectionTestUtils.setField
有一个对象类型的参数,那么它不应该注入字符串。它根本不注入任何内容。该错误将在代码出现之前产生,伙计。修改bean定义不起作用。它会引发相同的错误。Nisheeth建议的方法正在起作用。我在GitHub中修改了bean定义,您可以检查它是否起作用。@Nitin Vavdiya确保
@TestPropertySource
方法正在起作用。为了使另一种方法有效,您应该为
@value
指定一个默认的空值。它使解决方案更加复杂,而且我认为,
TestConfiguration
中定义的bean可能不是必需的。您可能应该删除它。修改bean定义不起作用。它会引发相同的错误。Nisheeth建议的方法起作用。我在GitHub中修改了bean定义,您可以检查它是否起作用。@Nitin Vavdiya确保
@TestPropertySource
方法起作用。为了使另一种方法有效,您应该为
@value
指定一个默认的空值。它使解决方案更加复杂,而且我认为,
TestConfiguration
中定义的bean可能不是必需的。您可能应该删除它。