使用Easymock进行方法的单元测试 [英] Unit testing a method with easymock

查看:233
本文介绍了使用Easymock进行方法的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有下面的方法,我想对它进行单元测试.

public List<Project> getProjects(Task task) {

    Criteria<Project> criteria = this.myRepository.getCriteria(Project.class);
    criteria.add(Comparison.eq("order", task.getOrder()));
    criteria.addOrder(Order.asc("projectNumber"));
    return this.myRepository.findList(Project.class, criteria);
}

因此,它实际上获取了任务对象(它是一个JPA模型对象)并抛出了项目表,并找到了具有该项目订单的所有项目.在两个表中顺序都是相同的.

无论如何,查询本身并不是那个问题.它查询db并返回一些数据.现在我的问题是如何使用easymock对此进行单元测试?

@Test
public void testGetProjects() throws Exception {
    myRepository = new CreateMyRepositoryWrapper(); --> This is a class which just returns the entityManger. but here we can consider this as a pojo.

    Task task = EasyMock.createNiceMock(Task.class);
    Order bom = EasyMock.createNiceMock(Order.class);        
    Project project= EasyMock.createNiceMock(Project.class);

    project.setProjectName("project"); ------> Can I call a seeter on a mocked object?

    project.setProjectNumber("1");

    EasyMock.replay(project);

    List projects= new ArrayList(Arrays.asList(project));
    bom.setProjects(projects);  ------------> Does it make sense to do this?

    EasyMock.expect(task.getOrders()).andReturn(bom);
    TestClass instance = new TestClass();
    instance.setMyRepository(myRepository);

    EasyMock.replay(task,bom);
   instance.getProjects(task);

}

因此,这通过了测试用例.但是我不确定所有这些嘲笑我实际上正在测试的东西.因为这只是表明这些方法正在被调用.但是由于它们是被嘲笑的,所以我不确定是否可以使用assertEquals,即使我可以得到异常,也必须在上述代码中添加更多内容.

所以我的问题是:对于提到的方法,什么是正确的单元测试用例?

谢谢.

解决方案

我认为您对此有嘲笑.模拟myRepostory,然后设置myRepository模拟以返回Criteria对象,并在将Criteria对象传递给findList时返回项目列表.

任务,订单和项目可能只是被实例化了.

现在,instance.getProjects(task)将返回一些内容.您可以检查以确保返回的内容与您所说的应该从findList返回的内容相同.现在,您实际上已经进行了测试,尽管没有什么特别有趣的.

您可能希望在将条件对象传递给findList之前验证其是否正确设置.为此,您要么必须使标准成为模拟,然后就可以对所调用的方法设置期望.这里最棘手的部分是Hibernate Restriction类没有非默认的equals实现,因此您必须编写自己的匹配器,以检查传递给条件的Restrictions是否与期望的Restrictions相同(在功能上). /p>

另一种可能性是将标准设置为实际的Criteria对象. (您仍然设置myRepository模拟以返回它.)然后,在调用该函数之后,可以使用toString()方法或任何其他已知的检查Criteria对象的方式来检查带有某些子字符串匹配的内容.

最后(单元测试)的可能性是不对Criteria对象使用模拟框架,而是使用一些您编写的手工编码的框架,使您可以检查添加到该框架的所有限制.

所有这些为实际使用集成测试进行测试的方法提供了一个很好的案例.您需要做很多工作来验证一些不太有趣的事情,并且如果您尝试重构代码,则测试可能会变得很脆弱. (我自己做了,所以我从经验上讲.)

So I have the bellow method which I want to perform a unit test on.

public List<Project> getProjects(Task task) {

    Criteria<Project> criteria = this.myRepository.getCriteria(Project.class);
    criteria.add(Comparison.eq("order", task.getOrder()));
    criteria.addOrder(Order.asc("projectNumber"));
    return this.myRepository.findList(Project.class, criteria);
}

So it actually gets the task object(It is a JPA model object) and goes throw the project table and finds all the projects which have this project's orders. Order is common in both tables.

Anyways, query itself is not that imp. It queries db and returns some data. Now my problem how can I perform a unit test on this with easymock?

@Test
public void testGetProjects() throws Exception {
    myRepository = new CreateMyRepositoryWrapper(); --> This is a class which just returns the entityManger. but here we can consider this as a pojo.

    Task task = EasyMock.createNiceMock(Task.class);
    Order bom = EasyMock.createNiceMock(Order.class);        
    Project project= EasyMock.createNiceMock(Project.class);

    project.setProjectName("project"); ------> Can I call a seeter on a mocked object?

    project.setProjectNumber("1");

    EasyMock.replay(project);

    List projects= new ArrayList(Arrays.asList(project));
    bom.setProjects(projects);  ------------> Does it make sense to do this?

    EasyMock.expect(task.getOrders()).andReturn(bom);
    TestClass instance = new TestClass();
    instance.setMyRepository(myRepository);

    EasyMock.replay(task,bom);
   instance.getProjects(task);

}

So this passes the test case. But I am not sure with all those mocking what I am actually testing.. Because it just shows that those methods are being called. But since they are mocked I am not sure if I can use assertEquals or not and even if I can I am getting an exception cuz I have to add more to the above code I think.

So my question: For the method mentioned what should be the proper unit test case?

Thanks.

解决方案

I think you have this mocking backwards. Mock myRepostory, then set up the myRepository mock to return a Criteria object and to return a project list when that Criteria object is passed to findList.

Task, Order and Project can probably just be instantiated.

Now, instance.getProjects(task) will return something. You can check to make sure that thing that got returned is the same thing you said should be returned from findList. Now you've actually tested something, albeit nothing particularly interesting.

You probably want to validate that the criteria object was set up correctly before it was passed to findList. To do that, you either have to make criteria a mock, then you can set up your expectations for what methods are called. The tricky part here is that Hibernate Restriction classes do not have a non-default equals implementation, so you have to write your own matcher to check that the Restrictions being passed to the criteria are the same (functionally) as the Restrictions you expect.

Another possibility is to set up criteria as an actual Criteria object. (You still set up your myRepository mock to return it.) Then, after the function is called, you can check the contents with some substring matching on the toString() method or any other ways you know to inspect the Criteria object.

A final (unit test) possibility is to not use a mocking framework for the Criteria object, but some hand-coded one that you write that allows you to inspect all of the Restrictions that were added to it.

All of this makes a good case for this method actually being tested with integration testing instead. You wind up doing a lot of work to verify some not very interesting things and your tests can become quite brittle if you try to refactor the code. (I've done it myself, so I speak from experience.)

这篇关于使用Easymock进行方法的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆