- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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 尝试在字段中设置字符串值?
我放置了 2 个字段,一个是整数,另一个是字符串用于测试目的。
您可以找到源代码here .
请查看并提出解决方法。
最佳答案
测试时,您必须提供属性源。如果您不添加属性,它会将值注入(inject)变量的 @Value
内。在您的情况下,它尝试将字符串添加到给出 NumberFormatException
的整数中。尝试添加如下:
@RunWith(SpringRunner.class)
@TestPropertySource(properties = {"app.count=1", "app.countStr=sampleString"})
public class SampleServiceTest{...}
当您使用@Autowired
时,在ReflectionTestUtils
之前它会尝试在@Value
中添加值。
关于java - Spring ReflectionTestUtils 不设置原始数据类型字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49335226/
我正在为一个使用静态类ProcessTimer的类编写单元测试。准确地说,ProcessTimer.startTimer()。 我正在寻找一种方法来模拟此调用。我以前使用过普通的 Reflection
我使用 ReflectionTestUtils 在我的服务类中设置 int 字段来测试该类。我的服务类别如下: @Service public class SampleService { @V
我有一个像这样的静态最终字段: class SomeClass { static final String CONST = "oldValue"; } 我正在尝试在测试中更改该字段,如下所示:
我正在努力解决如何覆盖 .yml 文件内的属性文件 我们使用Spring框架并使用注释(例如@InjectMocks)。 我在配置项目 YML 文件中声明了一个名为“one-platform-prop
我正在尝试使用 Mockito 测试 Spring Boot Controller 。我正在关注本教程:https://www.javacodegeeks.com/2013/07/getting-st
我有一个实现接口(interface)的类。该类有一个私有(private)双字段 field1 ,它使用 @Value spring 注释从应用程序属性中读取值。 我正在开发测试,我需要从该类填充该
我正在开发一个名为 acme-platform 的多模块 Maven 项目,模块设置如下: 顶点管理员 极致通用 极致全局 极致服务 acme-客户端 acme-注册 顶点属性 极限测试 (它们在 a
我是 JUnittesting 的新手,所以我有一个问题。谁能告诉我为什么我们在 Junit 测试中使用 ReflectionTestUtils.setField() 示例。 最佳答案 正如评论中提到
我正在尝试在 CourseServiceImpl 的 courseDao 字段中添加模拟对象,但它不起作用。 @RunWith(SpringJUnit4ClassRunner.class) @Cont
我是一名优秀的程序员,十分优秀!