@Spring控制器中的值整型变量导致jUnit错误(jUnit不使用此变量)

@Spring控制器中的值整型变量导致jUnit错误(jUnit不使用此变量),spring,spring-mvc,junit,Spring,Spring Mvc,Junit,对于SpringMVC控制器,我的jUnit中有一个奇怪的错误,它有一个@值整数类变量: @Value("${max.insert.participants.batch.size}") private Integer maxInsertParticipantsBatchSize; 该设置来自属性文件(所有其他设置也会从中正确读取) 这是属性文件中的变量。它未在jUnit中使用或检查,但jUnit失败,原因是: 12:38:21 Caused by: java.lang.NumberFormat

对于SpringMVC控制器,我的jUnit中有一个奇怪的错误,它有一个
@值
整数
类变量:

@Value("${max.insert.participants.batch.size}")
private Integer maxInsertParticipantsBatchSize;
该设置来自属性文件(所有其他设置也会从中正确读取)

这是属性文件中的变量。它未在jUnit中使用或检查,但jUnit失败,原因是:

12:38:21 Caused by: java.lang.NumberFormatException: For input string: "${max.insert.participants.batch.size}"
12:38:21    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:?]
只要我将
@值
类型更改为
字符串
布尔值
jUnit就开始工作。比如说

@Value("${max.insert.participants.batch.size}")
private String maxInsertParticipantsBatchSize;

再一次,jUnit没有使用这个变量。它仅在实际应用程序中使用。

注意:我认为您没有在属性
max.insert.particients.batch.size
中发送整数值

例如:

假设将属性
max.insert.particients.batch.size
设置为6

max.insert.participants.batch.size = 6 
下面的代码可以工作

@Value("${max.insert.participants.batch.size}")
private Integer maxInsertParticipantsBatchSize;
但如果发送非整数值,例如:

max.insert.participants.batch.size = six
下面的代码无法为您提供
java.lang.NumberFormatException
,因为类型转换失败

@Value("${max.insert.participants.batch.size}")
private Integer maxInsertParticipantsBatchSize;

我已经用一个演示代码进行了测试。它正在工作。问题出在别处。请检查applicationContext和Spring组件类。

注意:我认为您没有在属性
max.insert.particients.batch.size
中发送整数值

例如:

假设将属性
max.insert.particients.batch.size
设置为6

max.insert.participants.batch.size = 6 
下面的代码可以工作

@Value("${max.insert.participants.batch.size}")
private Integer maxInsertParticipantsBatchSize;
但如果发送非整数值,例如:

max.insert.participants.batch.size = six
下面的代码无法为您提供
java.lang.NumberFormatException
,因为类型转换失败

@Value("${max.insert.participants.batch.size}")
private Integer maxInsertParticipantsBatchSize;

我已经用一个演示代码进行了测试。它正在工作。问题出在别处。请检查applicationContext和Spring组件类。

如果您的测试用例是单元测试,那么这将不起作用。因为spring上下文没有通过单元测试进行初始化。在单元测试中使用此变量值的方法是如下设置变量值

ReflectionUtils.setField(字段名、对象、值)

如果您的测试用例是一个组件测试用例,其中spring上下文正在初始化,那么您需要在应用程序属性文件中提供如下值

max.insert.participants.batch.size:3

您还可以提供如下默认值

@Value(${max.insert.participants.batch.size:3})
私有整数maxInsertParticipantsBatchSize


默认值将被您在应用程序属性文件中提供的值覆盖。

如果您的测试用例是单元测试,那么这将不起作用。因为spring上下文没有通过单元测试进行初始化。在单元测试中使用此变量值的方法是如下设置变量值

ReflectionUtils.setField(字段名、对象、值)

如果您的测试用例是一个组件测试用例,其中spring上下文正在初始化,那么您需要在应用程序属性文件中提供如下值

max.insert.participants.batch.size:3

您还可以提供如下默认值

@Value(${max.insert.participants.batch.size:3})
私有整数maxInsertParticipantsBatchSize


默认值将被您在应用程序属性文件中提供的值覆盖。

测试将加载它,但显然没有属性值,或者属性替换对于您的测试无效,因此它将失败(因为它无法转换)。您能提供您的测试类代码以获取更多信息吗?因此,实际上我在属性文件中提供了值。就在那里。我还更新了我的OP以反映这一点。测试加载了它,但显然没有属性值,或者属性替换对于您的测试不处于活动状态,因此它将失败(因为它无法转换)。您可以提供您的测试类代码以获取更多信息吗?因此,实际上我在属性文件中提供了该值。就在那里。我还更新了我的OP以反映这一点。我正在设置它。我更新了OP。问题不是属性文件中缺少该设置,而是它在那里。@geneb。好啊looking@geneb. 我已经测试过了。它正在工作。问题在项目的其他地方。请检查applicationContext和Spring组件。@geneb。出了什么问题?我的问题还没有解决。它仍然没有读取属性。但我很欣赏你的研究,我正在做。我更新了OP。问题不是属性文件中缺少该设置,而是它在那里。@geneb。好啊looking@geneb. 我已经测试过了。它正在工作。问题在项目的其他地方。请检查applicationContext和Spring组件。@geneb。出了什么问题?我的问题还没有解决。它仍然没有读取属性。但我很欣赏你的研究。