我有一个实体 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();
}
}
欢迎任何建议,想法
谢谢