Tag: mockito

如何使用MockRestServiceServer模拟RestTemplet?

@RunWith(MockitoJUnitRunner.class) public class FeatureFlipperManagerTest { @Autowired RestTemplate restTemplate = new RestTemplate(); @Autowired Service service = new Service(); MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate); @Test public void test() throws Exception { mockServer.expect(requestTo(Mockito.anyString())) .andRespond(withSuccess(“{\”enabled\”:true}”, MediaType.APPLICATION_JSON)); boolean res = service.isEnabled(“xxx”); mockServer.verify(); Assert.assertEquals(true, res); } } 我有MockRestServiceServer来模拟服务中的restTemplete。 但它总是失败。 它显示错误为java.lang.AssertionError: Further request(s) expected 0 out of 1 were executed 。 任何人都可以让我知道我没有做对的地方。 服务本身看起来像这样: […]

Powermock和Mockito。 在模拟和存根同一个类时,避免对类进行静态初始化

假设我有一个名为Util的类,带有静态字段: public class Util { public static field = Param.getValue(“param1”); } 并且Param类看起来像这样: public class Param { public static field = SomeClass.getValue(“someValue”); } 我想在Util中模拟和stubb Param.getValue(“param1”),但同时我想要抑制Param类的静态初始化。 我怎样才能做到这一点? 这是我的第一次尝试,但它不起作用 @RunWith(PowerMockRunner.class) @PrepareForTest({Param.class}) @SuppressStaticInitializationFor(“py.com.company.Param”) public class Test { @Test public void testSomeMethod() { PowerMockito.mockStatic(Param.class); when(Param.getValue(“value1”)).thenReturn(“someValue1”); } }

validationunit testing中私有方法调用的顺序

我有以下课程: class MyClass { public void doIt() { methodOne(); methodTwo(); methodThree(); } private void methodOne() { // … } // rest of methods similar… } 我的目的是validation当我调用doIt()时,将按顺序调用方法metodOne(),methodTwo()和methodThree()。 我正在使用mockito进行嘲弄。 有谁知道我如何测试这种情况?

如何使用Mockitovalidation重载方法的调用次数?

我如何检查bar(Alpha, Baz)使用Mockito调用bar(Xray, Baz) – 在没有实际调用后者的情况下,给定我的MCVE类Foo : public class Foo { public String bar(Xray xray, Baz baz) { return “Xray”; } public String bar(Zulu zulu, Baz baz) { return “Zulu”; } public String bar(Alpha alpha, Baz baz) { if(alpha.get() instanceof Xray) { return bar((Xray)alpha.get(), baz); } else if(alpha.get() instanceof Zulu) { return bar((Zulu)alpha.get(), baz); } else […]

想要但不要援引:Mockito PrintWriter

嗨,我正在研究一个项目,并使用PrintWriter类在文件中打开和写入。 但是当我为相同的情况编写测试用例时,它会在第153行给出以下错误 Wanted but not invoked: mockPrintWriter.println(“ID url1 “); -> at xyzverify(ProcessImageDataTest.java:153) Actually, there were zero interactions with this mock. 代码:(使用龙目岛图书馆) ProcessImageData.java @Setter @RequiredArgsConstructor public class ProcessImageData implements T { private final File newImageDataTextFile; @Override public void execute() { LineIterator inputFileIterator = null; try { File filteredImageDataTextFile = new File(filteredImageDataTextFilepath); PrintWriter writer = new PrintWriter(newImageDataTextFile); […]

抛出Mockito和PowerMock MethodNotFoundException

使用Powermockito和Mockito为连接池构建一些简单的unit testing时遇到以下错误,我绕过了Hikari CP。 测试的设置如下。 令我感到困惑的是,我有一些未显示的unit testing,它们都使用相同的设置和方法。 只有这一个unit testing继续失败并出现该错误。 when我把它放在顶部的时候,它们都无法找到方法并不重要。 org.powermock.reflect.exceptions.MethodNotFoundException: No methods matching the name(s) getColumnCount were found in the class hierarchy of class java.lang.Object. at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1720) at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1745) at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:983) at org.powermock.core.MockGateway$MockInvocation.findMethodToInvoke(MockGateway.java:317) at org.powermock.core.MockGateway$MockInvocation.init(MockGateway.java:356) at org.powermock.core.MockGateway$MockInvocation.(MockGateway.java:307) at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:142) at org.powermock.core.MockGateway.methodCall(MockGateway.java:125) at com.datafiniti.utils.mysqlconnpool.MysqlConnPoolTests.executeStringQuery(MysqlConnPoolTests.java:149) 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) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68) at […]

模拟使用外部类的方法,mockito

我是mockito的新手,只是想了解它是如何工作的。 我有一个我想测试的方法。 该方法实例化多个类以使用其方法。 例如 methodToTest{ class1 c1 = new class1(); class2 c2 = new class2(); class3 c4 = new class3(); c1.method1; c2.method2; c3.method3; more logic … return result } 我明白为了测试这个方法我需要模拟类。 这是否意味着我需要将其解耦并将每个类作为参数传递给方法? 我想避免使用一个使用大量参数的方法,这些参数只有在模拟时才真正需要。 也许我错过了什么。 感谢您的见解。

Mockito – 奇怪的包范围类inheritance问题

发现非常有趣的问题,并在调试后发现了重现它的场景。 所以,如果我有一个包含范围B的类,它有一些公共方法,公共类A扩展它: package somepackage; class B { public void someMethod() { throw NullPointerException(); } } package somepackage; public class A extends B { } 然后在测试中: A a = mock(A.class); a.someMethod(); 并猜测是什么,我得到了刚刚扔的NullPointerException,所以Mockito以某种方式创建了一个“真正的”对象并调用了一个真正的方法而不是模拟的方法。 为什么这样? java.lang.IllegalArgumentException at test.B.setProxy(B.java:6) at test.A.setProxy(A.java:1) at secretservice.service.TestFDSServiceImpl.testService(TestFDSServiceImpl.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at […]

powermockito:如何在枚举中模拟抽象方法

考虑以下(简化)枚举: MyEnum { ONE public int myMethod() { // Some complex stuff return 1; }, TWO public int myMethod() { // Some complex stuff return 2; }; public abstract int myMethod(); } 这用于以下function: void consumer() { for (MyEnum n : MyEnum.values()) { n.myMethod(); } } 我现在想为consumer编写一个unit testing,在每个枚举实例中模拟对myMethod()的调用。 我尝试过以下方法: @RunWith(PowerMockRunner.class) @PrepareForTest(MyEnum.class) public class MyTestClass { @Test […]

Android模拟相机

是否可以模拟Android Camera类? @Override public void setUp() { _camera = Mockito.mock(Camera.class); } 无法生成模拟(Mockito的createProxyClass ExceptionInitializerError )。 我应该在Camera周围创建一些包装器(不是我最喜欢的解决方案,真的很想嘲笑这个类……)? 或者,我应该使用不同的模拟库然后Mockito? 希望有人能指出我正确的方向。 完成ExceptionInitializerError堆栈跟踪 java.lang.ExceptionInInitializerError at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56) at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51) at org.mockito.Mockito.mock(Mockito.java:1243) at org.mockito.Mockito.mock(Mockito.java:1120) at com.cleancode.lifesaver.flashlight.test.FlashLightTests.setUp(FlashLightTests.java:20) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661) Caused by: java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) […]