我有一个单元测试和一个助手类。不幸的是,Helper 类的自动装配不起作用。它在 MyTest 类中运行良好。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:context.xml"})
@Component
public class MyTest {
@Autowired
private Something something1;
@Autowired
private Something something2;
..
@Test
public void test1()
{
// something1 and something2 are fine
new Helper().initDB();
..
}
}
// Same package
public class Helper {
@Autowired
private Something something1;
@Autowired
private Something something2;
..
public void initDB()
{
// something1 and something2 are null. I have tried various annotations.
}
}
我想避免使用 setter,因为我有 10 个这样的对象,并且不同的测试有不同的对象。那么让@Autowired 在 Helper 类中工作需要什么?谢谢!