我正在尝试模拟在同一个类中使用的方法,但它给了我异常。
期望是newId()模拟返回String id,它被添加到List idList并被模拟为insert(idList)方法。
测试类是:
public final JUnitRuleMockery mock= new ClassMockery();
private CheckImpl check;
private IdDao daoMock;
private CheckImpl checkImpl;
@Before
public void setUp()
{
check= new CheckImpl ();
daoMock = mock.mock(IdDao.class);
check.IdDao = daoMock;
checkImpl = mock.mock(CheckImpl.class);
}
@Test
public void mainLogic()
{
List<Id> id = // List is created here
List<Id> idList = // another list is created here.
mock.checking(new ExtendedExpectations()
{
{
oneOf(checkImpl).newId();
will(returnValue(empId));
oneOf(daoMock).insert(idList);
will(returnValue(new int[] { 1 }));
oneOf checkImpl永远不会在实际的类中被模拟。 IdDao在CheckImpl类中作为@Resource传递。
这是CheckImpl类。这不是完整的课程,但它告诉我要找的是什么。
public class CheckImpl
{
@Resource
IdDao idDao;
public String newId()
{
return String.valueOf(id);
}
public List MainLogic(List){
String s = newId();
listInsert.add(s);
idDao.insert(listInsert);
}
错误如下,
java.lang.AssertionError: unexpected invocation:
idDao.insert(<[com.Id@24a35978]>)
expectations:
expected once, already invoked 1 time: idDao.find(<3>); returns
<[com.Id@5204062d, com.Id@4fcd19b3]>
expected once, never invoked: checkImpl.newId(); returns "123"
expected once, never invoked: idDao.insert(<[com.Id@24a35978]>); returns
[<1>]
parameter 0 did not match: <[com.Id@24a35978]>, because was
<[com.Id@24a35978]>
what happened before this:
idDao.find(<3>)
at org.jmock.api.ExpectationError.unexpected(ExpectationError.java:23)
at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:85)
at org.jmock.Mockery.dispatch(Mockery.java:244)
at org.jmock.Mockery.access$100(Mockery.java:29)
at org.jmock.Mockery$MockObject.invoke(Mockery.java:284)
at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
at org.jmock.internal.SingleThreadedPolicy$1.invoke(SingleThreadedPolicy.java:21)
at org.jmock.lib.legacy.ClassImposteriser$4.invoke(ClassImposteriser.java:136)