我有一个想要测试的服务:
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentDao departmentDao;
//... other functions
@Override
public Department getDepartment(int depid) {
return departmentDao.getDepartment(depid);
}
}
这是我的测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:sdnext-servlet.xml")
public class TestDepartmentDetails {
Department department = new Department();
DepartmentServiceImpl service = new DepartmentServiceImpl();
@Autowired
private DepartmentDao myDao;
@Test
public void testGetDepartment(){
assertEquals("lalalalalalala", service.getDepartment(98).getDepName());
}
为什么它会失败并抛出 NullPointerException
?还有其他解决办法吗?
注意:这是一个使用 Spring 和 Hibernate 的应用程序。
最佳答案
试试这个方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:sdnext-servlet.xml")
public class TestDepartmentDetails {
@Autowired
DepartmentService service;
@Test
public void testGetDepartment(){
assertEquals("lalalalalalala", service.getDepartment(98).getDepName());
}
关于java - 使用 JUnit 比较 2 个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37249220/