使用@Autowired注释进行Spring JUnit测试
在测试的一个类中引入@Autowired之后,我的测试用例出现了问题 我的测试用例现在如下所示:使用@Autowired注释进行Spring JUnit测试,spring,unit-testing,junit,Spring,Unit Testing,Junit,在测试的一个类中引入@Autowired之后,我的测试用例出现了问题 我的测试用例现在如下所示: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/applicationContext.xml", "/spring-security.xml"}) public class StudentRepositoryTest extends AbstractDatabaseTestCase { priva
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml", "/spring-security.xml"})
public class StudentRepositoryTest extends AbstractDatabaseTestCase {
private StudentRepository studentRepository;
private CompanyRepository companyRepository;
private Student testStudent;
private Company testCompany;
@Before
public void setUp() {
studentRepository = new StudentRepository();
studentRepository.setJdbcTemplate(getJdbcTemplate());
testStudent = Utils.testStudentNoApplication();
}
@Test
....
@Service
public class StudentRepository extends AbstractRepository<Student> {
...
private PasswordEncoder passwordEncoder;
private MailService mailService;
public StudentRepository() {
// TODO Auto-generated constructor stub
}
@Autowired
public StudentRepository(MailService mailService, PasswordEncoder passwordEncoder) {
this.mailService = mailService;
this.passwordEncoder = passwordEncoder;
}
}
StudentRepository现在看起来像这样:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml", "/spring-security.xml"})
public class StudentRepositoryTest extends AbstractDatabaseTestCase {
private StudentRepository studentRepository;
private CompanyRepository companyRepository;
private Student testStudent;
private Company testCompany;
@Before
public void setUp() {
studentRepository = new StudentRepository();
studentRepository.setJdbcTemplate(getJdbcTemplate());
testStudent = Utils.testStudentNoApplication();
}
@Test
....
@Service
public class StudentRepository extends AbstractRepository<Student> {
...
private PasswordEncoder passwordEncoder;
private MailService mailService;
public StudentRepository() {
// TODO Auto-generated constructor stub
}
@Autowired
public StudentRepository(MailService mailService, PasswordEncoder passwordEncoder) {
this.mailService = mailService;
this.passwordEncoder = passwordEncoder;
}
我的测试用例现在运行正常,但我的测试套件失败,出现了NullPointerException。
我猜在运行测试套件时,应用程序上下文由于某种原因没有自动连接?如果您不想在
@ContextConfiguration
引用的某个XML文件中声明您的StudentRepository
,并将其自动连接到测试中,您可以尝试使用自动连接BeanFactory
,如下所示:
...
public class StudentRepositoryTest extends AbstractDatabaseTestCase {
...
@Autowired ApplicationContext ctx;
@Before
public void setUp() {
studentRepository = ctx.getAutowireCapableBeanFactory()
.createBean(StudentRepository.class);
...
}
...
}
这只是测试中的一个问题吗?Spring是否以某种方式失败并出现异常?如果是单元测试,您可能应该将模拟邮件服务和PasswordEncoder实例传递给StudentRepository的构造函数。查看Mockito、EasyMock或任何其他mocking API。谢谢,这很有效,至少效果更好。我遇到了一个新的异常(UnsatifiedDependence),但正如上面的评论中提到的,我可能应该将mock传递给构造函数。@Daniel:是的,我的解决方案假设
MailService
和PasswordEncoder
是在@ContextConfiguration
引用的配置中声明的,并且您希望对它们进行测试。如果你需要模拟,那就使用模拟。虽然有些成功,但仍然存在一些问题。我已经更新了我的第一篇文章,原来我在testsuit中使用了JUnit3风格的测试,它不会检测测试类中的注释。