【问题标题】:How to get input argument from a mock method using PowerMock / EasyMock如何使用 PowerMock / EasyMock 从模拟方法中获取输入参数
【发布时间】:2012-03-01 20:16:11
【问题描述】:

我有一个实体 Person 类。 需要测试包含 process() 方法的 ProcessPerson 类。

我需要的是在 process() 方法中创建对象,并通过模拟对象 personDao 的模拟方法 create() 调用。

public class Person { 
    private String firstName;
    private String lastName;

      public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

public class ProcessPerson {
    @Autowired
    private PersonDao personDao;

    public void process() {
        Person person1 = new Person();
        person1.setFirstName("FN");
        //Do more logic to fill values into person object


        personDao.create(person1);
    }
}

import static org.powermock.api.easymock.PowerMock.*;
import static org.easymock.EasyMock.expect;

@RunWith(PowerMockRunner.class)
@PrepareForTest ( {Person.class} )
public class TestClass {

    @Test
    public void TestCase1() {
        ProcessPerson process = new ProcessPerson();

        //Create dependent DAO mock object
        PersonDao personDaoMock = createMock(PersonDao.class);

        //Inject mock object
        ReflectionTestUtils.setField(process, "personDao", personDaoMock);

        replayAll();

        process.process();

        //QUESTION here: can I get the object person1 which was created in process() method 
        //and called via create() function of my mock object
        //
        //I need the object to do verification like this
        //Assert.assertEqual(person1.getFirstName(), "FN");

        verifyAll();
    }
}

欢迎任何建议,想法

谢谢

【问题讨论】:

    标签: mocking easymock powermock


    【解决方案1】:

    EasyMock 具有“andDelegateTo”,可用于您想做的事情。它允许您在调用模拟时调用一些本地代码。我使用 AtomicReference 从匿名内部类中获取 person 对象。

    @Test
    public void TestCase1() {
        ProcessPerson process = new ProcessPerson();
    
        //Create dependent DAO mock object
        PersonDao personDaoMock = createMock(PersonDao.class);
    
        //Inject mock object
        ReflectionTestUtils.setField(process, "personDao", personDaoMock);
    
        final AtomicReference<Person> ref = new AtomicReference<Person>();
    
        personDaoMock.create(anyPerson());
        //expectLastCall().andDelegateTo(null);
        expectLastCall().andDelegateTo(new PersonDao() {
                @Override
                public void create(Person person1) {
                    ref.set(person1);
                }
        });
    
        replayAll();
    
        process.process();
    
        assertNotNull(ref.get());
    
        verifyAll();
    }
    
    public static Person anyPerson()
    {
        reportMatcher(Any.ANY);
        return null;
    }
    

    有关 andDelegateTo() 的更多详细信息,请参阅EasyMock Documentation

    【讨论】:

    • 谢谢centic。有用!。只是多了1件事。如果 PersonDao 接口有 100 个函数,我必须重写其他 99 个函数,其中没有任何内容。有没有其他方法可以解决这个问题?
    • 有一个方法andStubDelegateTo()。我还没有在任何地方使用它,文档也不是很具体,但我认为它可以用来只实现一种方法而不是整个接口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多