Spring ReflectionTestUtils не устанавливает поле примитивного типа данных ⇐ JAVA
Spring ReflectionTestUtils не устанавливает поле примитивного типа данных
Я использую ReflectionTestUtils, чтобы установить поле int в моем классе обслуживания для тестирования класса. Мой класс обслуживания:
@Service общественный класс SampleService { @Value("${app.count}") частный счетчик int; @Value("${app.countStr}") частная строка countStr; общественный int getCount() { счетчик возврата; } public void setCount(int count) { this.count = количество; } публичная строка getCountStr() { вернуть счетчикStr; } общественная недействительность setCountStr (String countStr) { this.countStr = countStr; } @PostConstruct public int demoMethod() { вернуть счетчик + Integer.parseInt(countStr); } } и тестовый класс такой:
@RunWith(SpringRunner.class) общественный класс SampleServiceTest { @Autowired частный SampleService sampleService; @TestConfiguration статический класс SampleServiceTestConfig { @Бин общественный SampleService sampleService() { вернуть новый SampleService(); } } @До общественная недействительная инициализация () { ReflectionTestUtils.setField(sampleService, "count", new Integer(100)); ReflectionTestUtils.setField(sampleService, "countStr", 100); } @Тест общественный недействительный testDemoMethod() { int a = sampleService.demoMethod(); Assert.assertTrue(a == 200); } } При запуске этого тестового примера выдается следующая ошибка:
Вызвано: java.lang.NumberFormatException: для входной строки: "${app.count}" в java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) в java.lang.Integer.parseInt(Integer.java:569) в java.lang.Integer.valueOf(Integer.java:766) в org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:210) в org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115) в org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:466) в org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:439) в org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:192) в org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:117) в org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:70) ... еще 47 Почему ReflectionTestUtils пытается установить строковое значение в поле?
Для целей тестирования я добавил 2 поля: одно — целое число, а другое — строку.
Вы можете найти исходный код здесь.
Пожалуйста, посмотрите и предложите обходной путь.
Я использую ReflectionTestUtils, чтобы установить поле int в моем классе обслуживания для тестирования класса. Мой класс обслуживания:
@Service общественный класс SampleService { @Value("${app.count}") частный счетчик int; @Value("${app.countStr}") частная строка countStr; общественный int getCount() { счетчик возврата; } public void setCount(int count) { this.count = количество; } публичная строка getCountStr() { вернуть счетчикStr; } общественная недействительность setCountStr (String countStr) { this.countStr = countStr; } @PostConstruct public int demoMethod() { вернуть счетчик + Integer.parseInt(countStr); } } и тестовый класс такой:
@RunWith(SpringRunner.class) общественный класс SampleServiceTest { @Autowired частный SampleService sampleService; @TestConfiguration статический класс SampleServiceTestConfig { @Бин общественный SampleService sampleService() { вернуть новый SampleService(); } } @До общественная недействительная инициализация () { ReflectionTestUtils.setField(sampleService, "count", new Integer(100)); ReflectionTestUtils.setField(sampleService, "countStr", 100); } @Тест общественный недействительный testDemoMethod() { int a = sampleService.demoMethod(); Assert.assertTrue(a == 200); } } При запуске этого тестового примера выдается следующая ошибка:
Вызвано: java.lang.NumberFormatException: для входной строки: "${app.count}" в java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) в java.lang.Integer.parseInt(Integer.java:569) в java.lang.Integer.valueOf(Integer.java:766) в org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:210) в org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115) в org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:466) в org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:439) в org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:192) в org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:117) в org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:70) ... еще 47 Почему ReflectionTestUtils пытается установить строковое значение в поле?
Для целей тестирования я добавил 2 поля: одно — целое число, а другое — строку.
Вы можете найти исходный код здесь.
Пожалуйста, посмотрите и предложите обходной путь.
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как проверить, является ли объект примитивным типом или массивом примитивного типа в Java?
Anonymous » » в форуме JAVA - 0 Ответы
- 23 Просмотры
-
Последнее сообщение Anonymous
-