java - 使用 JUnit 比较 2 个对象

标签 java spring hibernate junit

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

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/

相关文章:

启动位置服务时出现java.lang.SecurityException

java - 如何将Excel单元格值插入到列表中?

java - Hibernate 3.6.0.Final ClassNotFoundException ReflectionManager

Spring Boot Webflux - 安全 CORS 不起作用

java - 有什么方法可以在使用 spring 执行 rest api 之前验证 token

java - 默认的Hibernate(5.2.5) session 级缓存查杀性能是多少?

Java Hibernate 多对一问题

java - 尝试将 Hibernate 连接到 Postgres 时出错

java - 读取 BufferedReader

Spring 和多模块设置