6
Spring JUnit:如何在自动装配的组件中模拟自动装配的组件
我有一个要测试的Spring组件,该组件具有autowired属性,出于单元测试的目的,我需要更改该属性。问题是,该类在post-construct方法内部使用了自动装配的组件,因此在实际使用它之前,我无法替换它(即通过ReflectionTestUtils)。 我该怎么办? 这是我要测试的课程: @Component public final class TestedClass{ @Autowired private Resource resource; @PostConstruct private void init(){ //I need this to return different result resource.getSomething(); } } 这是一个测试用例的基础: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations= "classpath:applicationContext.xml") public class TestedClassTest{ @Autowired private TestedClass instance; @Before private void setUp(){ //this doesn't work because it's executed after the bean …