我有一个失败的单元测试,我不确定为什么.我希望能够看到在被测系统中发生的模拟上的所有调用.这不是我想要的所有测试的行为,只是为了我需要快速调整以便能够弄清楚什么是错误的测试.
然而,它似乎有点像黑客.是否有可能在Mockito本地执行此操作,而不必使用Thread.currentThread().getStackTrace()?
这不是首选,因为堆栈跟踪包括Mockito内部使用的所有其他调用.
当我运行以下代码时:
public class ActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
....
public void testCanCreateMockito() {
List mockedList = Mockito.mock(List.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下例外情况:
java.lang.ExceptionInInitializerError
at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)
at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57)
at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)
at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1285)
at org.mockito.Mockito.mock(Mockito.java:1163)
at com.acesounderglass.hungertracker.ActivityTest.testCanCreateMockito(ActivityTest.java:60)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)
at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
... 23 more
Caused by: java.lang.reflect.InvocationTargetException
at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)
... 28 …Run Code Online (Sandbox Code Playgroud) 假设我有这个对象objectDemo,它使用2个参数String和null调用方法objectDemoMethod.现在我想验证这个方法是用Mockito调用的:
objectDemo.objectDemoMethod("SAMPLE_STRING", null);
Run Code Online (Sandbox Code Playgroud)
我写了这个:
Mockito.verify(objectDemo, Mockito.times(1)).objectDemoMethod(Matchers.any(String.class), null);
Run Code Online (Sandbox Code Playgroud)
但它给出了一个错误:
参数匹配器无效使用null值.
有没有其他方法传递空值?
我想ContentIOException从一个签名看起来像这样的方法中抛出一个.
public void putContent(InputStream is) throws ContentIOException.
Run Code Online (Sandbox Code Playgroud)
当我试图ContentIOException从Mockito那样抛出时:
when(StubbedObject.putContent(contentStream)).thenThrow(ContentIOException.class);
Run Code Online (Sandbox Code Playgroud)
我收到以下编译错误:
The method when(T) in the type Mockito is not applicable for the arguments (void).
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在尝试编写单元测试,为此我正在为Mockito模拟写一个when语句,但我似乎无法通过eclipse来识别我的返回值是有效的.
这是我正在做的事情:
Class<?> userClass = User.class;
when(methodParameter.getParameterType()).thenReturn(userClass);
Run Code Online (Sandbox Code Playgroud)
返回类型.getParameterType()是Class<?>,所以我不明白为什么eclipse说,The method thenReturn(Class<capture#1-of ?>) in the type OngoingStubbing<Class<capture#1-of ?>> is not applicable for the arguments (Class<capture#2-of ?>).它提供了投射我的userClass,但这只是让一些乱码的东西eclipse说它需要再次施放(并且不能施放).
这只是Eclipse的一个问题,还是我做错了什么?
我有一个有2种方法的课程.我想模拟类,然后模拟第一个方法,但不是第二个方法.
例如
class C {
void m1() { ...}
boolean m2() { ... return flag;}
}
Run Code Online (Sandbox Code Playgroud)
单元测试代码:
C cMock = Mockito.mock(C.class);
Mockito.doNothing().when(cMock).m1();
Mockito.when(cMock.m2()).thenCallRealMethod();
Run Code Online (Sandbox Code Playgroud)
奇怪的是m2没有被调用.
我在这里想念什么吗?
在Java中使用Mockito如何验证方法只调用一次精确参数忽略对其他方法的调用?
示例代码:
public class MockitoTest {
interface Foo {
void add(String str);
void clear();
}
@Test
public void testAddWasCalledOnceWith1IgnoringAllOtherInvocations() throws Exception {
// given
Foo foo = Mockito.mock(Foo.class);
// when
foo.add("1"); // call to verify
foo.add("2"); // !!! don't allow any other calls to add()
foo.clear(); // calls to other methods should be ignored
// then
Mockito.verify(foo, Mockito.times(1)).add("1");
// TODO: don't allow all other invocations with add()
// but ignore all other calls (i.e. the call to clear())
}
} …Run Code Online (Sandbox Code Playgroud) 我尝试将junit,mokito和powermock设置在一起,但是当我运行测试时,我得到了ClassNotFoundException :(
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.7.22'
androidTestCompile 'org.mockito:mockito-core:2.7.22'
androidTestCompile "org.mockito:mockito-android:2.7.22"
testCompile 'org.robolectric:robolectric:3.3.2'
testCompile 'org.hamcrest:hamcrest-core:1.3'
testCompile 'org.powermock:powermock-core:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.6'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-classloading-xstream:1.6.6'`
Run Code Online (Sandbox Code Playgroud)
我还尝试添加cglib:
testCompile 'cglib:cglib:3.1'
testCompile 'cglib:cglib-nodep:3.1'
Run Code Online (Sandbox Code Playgroud)
但没有缺乏.
任何人都可以分享工作配置或指出我的错误.
我运行测试时的堆栈跟踪:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mockito/exceptions/Reporter
at sun.reflect.GeneratedSerializationConstructorAccessor5.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:251)
at org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139)
at org.powermock.api.extension.reporter.AbstractMockingFrameworkReporterFactory.getInstanceForClassLoader(AbstractMockingFrameworkReporterFactory.java:41)
at org.powermock.api.extension.reporter.AbstractMockingFrameworkReporterFactory.create(AbstractMockingFrameworkReporterFactory.java:35)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getMockingFrameworkReporter(JUnit4TestSuiteChunkerImpl.java:140)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:119)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) …Run Code Online (Sandbox Code Playgroud) 我正在尝试为 Junit 5 测试用例模拟静态类(org.apache.commons.beanutils.Beanutils)。我发现mockito-inline依赖有助于模拟静态类。我尝试在项目中使用 ,mockito-inline由于一些奇怪的原因,它在没有库的情况下给我编译错误mockito-core。
我mockito-core正在下面:
org.mockito.exceptions.base.MockitoException:
The used MockMaker PowerMockMaker does not support the creation of static mocks
Mockito's inline mock maker supports static mocks based on the Instrumentation API.
You can simply enable this mock mode, by placing the 'mockito-inline' artifact where you are currently using 'mockito-core'.
Note that Mockito's inline mock maker is not supported on Android.
at com.xx.xx.xx.AvroCopyPropertiesInvocationTargetExceptionScenario(CreditOfferServiceTest.java:1197)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124) …Run Code Online (Sandbox Code Playgroud) 我需要使用mockito为现有代码创建单元测试框架.我无法找到开始学习Mockito的好地方.你能指点我一个很好的学习资源吗?(在线资源或其他)
mockito ×10
java ×9
junit ×5
unit-testing ×4
mocking ×2
powermock ×2
android ×1
debugging ×1
mockstatic ×1
testing ×1