gpt4 book ai didi

java - 使用 JUnit 比较 2 个对象

转载 作者:行者123 更新时间:2023-12-01 09:55:40 25 4
gpt4 key购买 nike

我有一个想要测试的服务:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com