如果删除一行代码,我可以使用spring boot @Value。不幸的是,我需要保留这行代码-因为它确实是我尝试在junit测试中使用的其他库的一部分。如果我不需要这个其他库,那就没问题了。
导致此问题的行是这一行。 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“ mycontext.xml”);
执行此行似乎会更改已经完全加载的上下文,这样就可以找到@Value项。
如何修复我的junit,以使获取该另一个库的Bean实例不会与现有库冲突?
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ContextConfiguration(locations = "/myContext.xml")
@TestPropertySource(locations = {"classpath:test.properties"})
public class MyTest {
@Autowired
MyService myService;
public void myTest() {
// This is really a call to another library (it's the call that fails from a getInstance from its initialize
// Within that initialize call, there's a line that tries to load a new XML applicationContext.
// If I comment this line, I don't have the problem described, but I need to use this class of course.
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("mycontext.xml");
//If above line is commented, I have the value in the mapped variable which uses @Value("${myProperty}")
// but if the line is not commented, I get the IllegalArgumentException:
// Could not resolve placeholder 'myProperty' in value "${myProperty}"