Код: Выделить всё
myThing:
someValue: 10
Код: Выделить всё
TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:80)edit: я думаю, что мне просто не хватает необходимого класса для этого поведения в @ContextConfiguration, но я не могу найти его хоть убей, и я потратил на это несколько часов.
Код: Выделить всё
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyThing {
@Value("${myThing.myValue}")
private Integer myValue;
public Integer getMyValue() {
return myValue;
}
}
Код: Выделить всё
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {MyThing.class}) // Won't be able to load the context
class DemoApplicationTests {
@Autowired
private MyThing myThing;
@Test
void contextLoads() {
assertEquals(10, myThing.getMyValue());
}
}
Код: Выделить всё
myThing:
myValue: 10
в java.base/java. lang.NumberFormatException.forInputString(NumberFormatException.java:67)
в java.base/java.lang.Integer.parseInt(Integer.java:654)
в java.base/java.lang.Integer. valueOf(Integer.java:999)
в org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:203)
в org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java: 115)
в org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:439)
в org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:412)
в org .springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:161)
на org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:80)
... еще 39
Использование @SpringBootTest загружает полный контекст со службой конвертера, и тест проходит.
Подробнее здесь: https://stackoverflow.com/questions/790 ... extconfigu
Мобильная версия