本文将介绍PowerMockito:使用匹配器模拟静态方法时收到InvalidUseOfMatchersException的详细情况,特别是关于模态匹配分析的相关信息。我们将通过案例分析、数据研究等多
本文将介绍PowerMockito:使用匹配器模拟静态方法时收到InvalidUseOfMatchersException的详细情况,特别是关于模态匹配分析的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于c# – 模拟一个DataReader并获取Rhino.Mocks.Exceptions.ExpectationViolationException:IDisposable.Dispose();预期#0,实际#1、Failed with exception java.io.IOException....FileFormatException: Malforme... Invalid postscript.、java – Ambiguous Mockito – 0匹配预期,1记录(InvalidUseOfMatchersException)、java – Mockito Problems – InvalidUseOfMatchersException的知识。
本文目录一览:- PowerMockito:使用匹配器模拟静态方法时收到InvalidUseOfMatchersException(模态匹配分析)
- c# – 模拟一个DataReader并获取Rhino.Mocks.Exceptions.ExpectationViolationException:IDisposable.Dispose();预期#0,实际#1
- Failed with exception java.io.IOException....FileFormatException: Malforme... Invalid postscript.
- java – Ambiguous Mockito – 0匹配预期,1记录(InvalidUseOfMatchersException)
- java – Mockito Problems – InvalidUseOfMatchersException
PowerMockito:使用匹配器模拟静态方法时收到InvalidUseOfMatchersException(模态匹配分析)
当我测试此静态方法时
public class SomeClass { public static long someMethod(Map map, String string, Long l, Log log) { ... }}
与
import org.apache.commons.logging.Log;@RunWith(PowerMockRunner.class)//@PrepareForTest(SomeClass.class)public class Tests { @Test public void test() { ... PowerMockito.mockStatic(SomeClass.class); Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L); ... }}
我知道了InvalidUseOfMatchersException
。我的问题是:
- 当所有参数都使用匹配器时,为什么会出现此异常?怎么解决呢?我已经调试了它,发现
isA(Log.class)
返回null。 - 当我将
@PrepareForTest
注释添加到测试类并运行测试时,junit不会响应。为什么?
编辑
我试图不使用参数匹配器,并得到
org.mockito.exceptions.misusing.MissingMethodInvocationException:when()需要一个参数,该参数必须是“模拟的方法调用”。例如:when(mock.getArticles())。thenReturn(articles);
同样,由于以下原因,可能会出现此错误:
您存根以下方法之一:final / private / equals()/ hashCode()方法。这些方法 不能 存根/验证。
在when()内部,您不会在模拟对象上调用方法,而是在其他一些对象上调用方法。
在 …
因此,似乎是由于someMethod
本身。该方法中有 同步 块。我想知道Powermockito是否可以模拟这种方法。
答案1
小编典典尝试将isA()替换为另一个any()这样的调用
Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), any(Log.class))).thenReturn(1L);
[编辑]
收到异常时检查堆栈跟踪。您看到任何NoClassDefFoundError
举报了吗?我注意到当我没有javassist.jar
在项目中包含时,我也遇到了类似的错误。
我使用PowerMockito,这些是我添加到一个新项目中的jar,用于运行@Tom发布的代码
- powermock-mockito-1.4.10-full.jar
- mockito-all-1.8.5.jar
- javassist-3.15.0-GA.jar
- junit-4.8.2.jar
- common-logging-1.1.1.jar
检查您是否正在使用兼容的JAR版本,并检查项目的类路径中是否还有其他冲突的JAR,始终是一个好主意。
c# – 模拟一个DataReader并获取Rhino.Mocks.Exceptions.ExpectationViolationException:IDisposable.Dispose();预期#0,实际#1
sqlDataReader reader = mocks.CreateMock<sqlDataReader>(); Expect.Call(reader.Read()).Return(true).Repeat.Times(1); Expect.Call(reader.Read()).Return(false); Expect.Call(reader.HasRows).Return(true); Expect.Call(reader.dispose); Expect.Call(reader["City"]).Return("Boise"); Expect.Call(reader["State"]).Return("State"); Expect.Call(reader["LAT"]).Return(100); Expect.Call(reader["LON"]).Return(-100); mocks.ReplayAll();
但我不断得到一个Rhino.Mocks.Exceptions.ExpectationViolationException:Idisposable.dispose();预期#0,我的方法中的实际#1错误
using (reader) { if (reader.HasRows) { while (reader.Read()) { CityState myCity = new CityState { City = reader["City"].ToString(),State = reader["State"].ToString(),Lat = Convert.ToInt32(reader["LAT"]),Lon = Convert.ToInt32(reader["LON"]) }; myCities.Add(myCity); } } }
我错过了什么吗?
解决方法
// Arrange IDataReader reader = MockRepository.GenerateStub<IDataReader>(); reader.Stub(x => x.Read()).Repeat.Once().Return(true); reader.Stub(x => x.Read()).Return(false); reader.Stub(x => x["City"]).Return("Boise"); reader.Stub(x => x["State"]).Return("State"); reader.Stub(x => x["LAT"]).Return(100); reader.Stub(x => x["LON"]).Return(-100); // Act var myCities = new List<CityState>(); using (reader) { while (reader.Read()) { CityState myCity = new CityState { City = reader["City"].ToString(),Lon = Convert.ToInt32(reader["LON"]) }; myCities.Add(myCity); } } // Assert Assert.AreEqual(1,myCities.Count); Assert.AreEqual("Boise",myCities[0].City); ...
Failed with exception java.io.IOException....FileFormatException: Malforme... Invalid postscript.
Hive相关的问题。
.txt数据文件导入后再查询报这个错,说明表不是text格式,需要把表修改成text格式后,导入再查询即正确。
java – Ambiguous Mockito – 0匹配预期,1记录(InvalidUseOfMatchersException)
URL = "/my/specific/url/"; when(this.restHelperMock.post( eq(myEnum),eq(this.config.apiEndpoint() + URL),any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
甚至包含
URL = "/my/specific/url/"; when(this.restHelperMock.post( eq(myEnum),contains(this.config.apiEndpoint() + URL),any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
给我
org.mockito.exceptions.misusing.InvalidUSEOfMatchersException: Invalid use of argument matchers! 0 matchers expected,1 recorded: This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(),"raw String"); When using matchers,all arguments have to be provided by matchers. For example: //correct: someMethod(anyObject(),eq("String by matcher")); For more info see javadoc for Matchers class.
即使我不使用RAW表达式.
奇怪的是,如果我将contains方法更改为:
URL = "/my/specific/url/"; when(this.restHelperMock.post( eq(myEnum),contains(URL),any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
省略端点,它的工作原理.
Config和RestHelper都被嘲笑:
this.restHelperMock = mock(RESTHelper.class); this.config = mock(MyBMWConfiguration.class); when(this.config.apiEndpoint()).thenReturn("http://host:port/api");
ApiEndpoint的URL等于我想要模拟的,
即使它不会,我应该得到一个NullpointerException,因为虚假的嘲弄.
但在这里,我没有任何想法.
谢谢您的回答.
解决方法
说实话,我不是那么深入Mockito,我可以解释为什么会发生这种情况,但有一天我可能会尝试调试它;-)
编辑:奇怪的是,我似乎无法用更简单的测试用例重现它.这里似乎有更多,而不是满足眼睛.
java – Mockito Problems – InvalidUseOfMatchersException
org.mockito.exceptions.misusing.InvalidUSEOfMatchersException: Invalid use of argument matchers! 2 matchers expected,1 recorded: -> at cl.gps.tms.planifications.planification.test.PlanificationCreateTest.setUp(PlanificationCreateTest.java:68) This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(),"raw String"); When using matchers,all arguments have to be provided by matchers. For example: //correct: someMethod(anyObject(),eq("String by matcher")); ...
PlanificationCreateTest使用SimpleQueryBus创建一个通用查询,其中de first参数指示返回的对象类型,第二个参数是查询的过滤器.
我想要存根SimpleQueryBus类(外部库)返回null(仅限现在)
SimpleQueryBus代码
public class SimpleQueryBus implements QueryBus { public <T,R> R handle(Class<R> clazz,T query) throws Exception { ... } }
我的测试代码
public class PlanificationCreateTest { private QueryBus queryBus; @Before public void setUp() throws Exception { queryBus = Mockito.mock(SimpleQueryBus.class); when(queryBus.handle(VehicleCollection.class,any(GetVehicle.class))).thenAnswer(null); .... } }
更新(已解决):
public class PlanificationCreateTest { private QueryBus queryBus; @Before public void setUp() throws Exception { queryBus = Mockito.mock(SimpleQueryBus.class); // first example when(queryBus.handle(any(Class.class),isA(VehicleAvailable.class))).thenReturn(null); // second example vehicle = new VehicleCollection("001","name","tag","brand","model"); when(queryBus.handle(any(Class.class),isA(GetVehicle.class))).thenReturn(vehicle); .... } }
谢谢…
解决方法
改变就像下面一样,你应该没问题:
when(queryBus.handle(any(VehicleCollection.class),any(GetVehicle.class))).thenAnswer(null);
Mockito解释了原因:http://mockito.googlecode.com/svn/tags/1.7/javadoc/org/mockito/Matchers.html
If you are using argument matchers,all arguments have to be provided
by matchers.E.g: (example shows verification but the same applies to stubbing):
06001
今天关于PowerMockito:使用匹配器模拟静态方法时收到InvalidUseOfMatchersException和模态匹配分析的讲解已经结束,谢谢您的阅读,如果想了解更多关于c# – 模拟一个DataReader并获取Rhino.Mocks.Exceptions.ExpectationViolationException:IDisposable.Dispose();预期#0,实际#1、Failed with exception java.io.IOException....FileFormatException: Malforme... Invalid postscript.、java – Ambiguous Mockito – 0匹配预期,1记录(InvalidUseOfMatchersException)、java – Mockito Problems – InvalidUseOfMatchersException的相关知识,请在本站搜索。
本文标签: