使用EasyMock我希望能够说我期望在我的模拟上调用一个特定的方法,但我不关心用于调用模拟的参数。SomeInterfacemock=EasyMock.createMock(SomeInterface.class);mock.send(/*anything*/);replay(mock);/*Performactionsthatwilleventuallyinvokemock*/verify(mock);这可能吗?如何实现?另外,如果我想接受派生自特定基类的任何对象,我该如何指定? 最佳答案 接受任何对象作为参数:mock.sen
使用EasyMock我希望能够说我期望在我的模拟上调用一个特定的方法,但我不关心用于调用模拟的参数。SomeInterfacemock=EasyMock.createMock(SomeInterface.class);mock.send(/*anything*/);replay(mock);/*Performactionsthatwilleventuallyinvokemock*/verify(mock);这可能吗?如何实现?另外,如果我想接受派生自特定基类的任何对象,我该如何指定? 最佳答案 接受任何对象作为参数:mock.sen
我有一个方法,我正在尝试进行单元测试。此方法将参数作为ArrayList并对其进行处理。我试图定义的模拟是:ArrayListmocked=mock(ArrayList.class);这会给出[unchecked]uncheckedconversion”警告。ArrayListmocked=mock(ArrayList.class);给我一个错误。有人愿意告诉我我做错了什么吗? 最佳答案 替代方法是使用@Mock注解,因为Mockito可以使用类型反射来查找泛型类型:publicclassMyTest{@MockprivateA
我有一个方法,我正在尝试进行单元测试。此方法将参数作为ArrayList并对其进行处理。我试图定义的模拟是:ArrayListmocked=mock(ArrayList.class);这会给出[unchecked]uncheckedconversion”警告。ArrayListmocked=mock(ArrayList.class);给我一个错误。有人愿意告诉我我做错了什么吗? 最佳答案 替代方法是使用@Mock注解,因为Mockito可以使用类型反射来查找泛型类型:publicclassMyTest{@MockprivateA
我遇到了我认为可能是Mockito的错误,但想知道是否有其他人可以解释为什么这个测试不起作用。基本上,我有两个对象,如下所示:publicclassFirstObject{privateSecondObjectsecondObject;publicSecondObjectgetSecondObject(){returnsecondObject;}}publicclassSecondObject{privateStringname;publicStringgetName(){returnname;}}第一个对象是通过注解和before方法模拟的:@MockFirstObjectmocke
我遇到了我认为可能是Mockito的错误,但想知道是否有其他人可以解释为什么这个测试不起作用。基本上,我有两个对象,如下所示:publicclassFirstObject{privateSecondObjectsecondObject;publicSecondObjectgetSecondObject(){returnsecondObject;}}publicclassSecondObject{privateStringname;publicStringgetName(){returnname;}}第一个对象是通过注解和before方法模拟的:@MockFirstObjectmocke
是否可以模拟Java类的单个方法?例如:classA{longmethod1();Stringmethod2();intmethod3();}//insomeotherclassclassB{voidsomeMethod(Aa){//howwouldImockA.method1(...)suchthata.method1()returnsavalueofmy//choosing;//whilstleavinga.method2()anda.method3()untouched.}} 最佳答案 使用Mockito的spy机制:Aa=n
是否可以模拟Java类的单个方法?例如:classA{longmethod1();Stringmethod2();intmethod3();}//insomeotherclassclassB{voidsomeMethod(Aa){//howwouldImockA.method1(...)suchthata.method1()returnsavalueofmy//choosing;//whilstleavinga.method2()anda.method3()untouched.}} 最佳答案 使用Mockito的spy机制:Aa=n
publicObjectdoSomething(Objecto);我想模拟的。它应该只返回它的参数。我试过了:CapturecopyCaptcher=newCapture();expect(mock.doSomething(capture(copyCaptcher))).andReturn(copyCatcher.getValue());但没有成功,我得到一个AssertionError作为java.lang.AssertionError:Nothingcaptureyet。有什么想法吗? 最佳答案 嗯,最简单的方法是在IAnswe
publicObjectdoSomething(Objecto);我想模拟的。它应该只返回它的参数。我试过了:CapturecopyCaptcher=newCapture();expect(mock.doSomething(capture(copyCaptcher))).andReturn(copyCatcher.getValue());但没有成功,我得到一个AssertionError作为java.lang.AssertionError:Nothingcaptureyet。有什么想法吗? 最佳答案 嗯,最简单的方法是在IAnswe