Tag: powermock

Apache Spark Streaming的失败集成测试

我一直试图找出我为Apache Spark项目编写的一些单元/集成测试的问题。 当使用Spark 1.1.1时,我的测试通过了。 当我尝试升级到1.4.0(也尝试过1.4.1)时,测试开始失败。 我已经设法将重现问题所需的代码减少到下面的小集成测试。 有趣的是,如果我在测试中注释掉@RunWith注释,那么测试就会正确传递。 显然我不需要@RunWith注释来进行这种减少测试,但真正的测试相当广泛地使用了模拟,所以我宁愿不必使用PowerMock。 package com.example; import org.apache.spark.SparkConf; import org.apache.spark.streaming.Duration; import org.apache.spark.streaming.api.java.JavaStreamingContext; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) public class SampleTest { @Before public void setup() throws Exception { SparkConf conf = new SparkConf(false).setMaster(“local[2]”).setAppName(“My app”); JavaStreamingContext jsc = new JavaStreamingContext(conf, new Duration(1000)); } @Test public void exampleTest() { […]

测试powermock模拟http服务器超时以进行客户端调用

我需要为connectTimeout和SocketTimeoutexception编写测试用例。 我使用powerMock来创建模拟对象。 以下是我的代码。 但我的模拟对象得到空指针exception。 任何帮助赞赏 package com.util; import java.net.ConnectException; import java.net.SocketTimeoutException; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import org.json.JSONObject; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.testng.PowerMockObjectFactory; import org.powermock.modules.testng.PowerMockTestCase; import org.testng.Assert; import org.testng.IObjectFactory; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; @PowerMockIgnore(“javax.management.*”) @SuppressStaticInitializationFor(“com.Sender”) @PrepareForTest(Sender.class) public class SenderCatalogTest extends PowerMockTestCase{ @ObjectFactory public IObjectFactory getObjectFactory() { return new PowerMockObjectFactory(); } @Test […]

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”); } }

如何使用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和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 […]

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 […]

Mockito在spring的间谍对象

当我尝试在unit testing中窥探一个对象时,我得到了一个例外。 这是我的unit testing文件: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { “classpath:spring/applicationContext.xml” }) public class BookingSuperManTest { BookInfoParams bookInfoParams; HttpAttributeParams httpAttributeParams; AbstractRequester requester; public void beforeStartTest(){ bookInfoParams = Mockito.spy(new BookInfoParams()); httpAttributeParams = Mockito.spy(new HttpAttributeParams()); } @Test public void step1GoToHomePage() throws BookingException{ beforeStartTest(); requester = new Step1HomePage(bookInfoParams, httpAttributeParams); requester.executeRequest(); Assert.assertNotNull(httpAttributeParams.getResponseGetRequest()); } } 我在链接分配bookInfoParams spy时得到了exception: java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.(PowerMockMaker.java:43) at […]

PowerMock Mockito:如何模拟所有静态方法?

在使用PowerMock(使用Mockito)时,我们是否需要模拟类的所有静态方法? 我的意思是,假设我们有: class MockMe { public static MockMe getInstance(){ //return new Instance via complex process; } public static List anotherStaticMethod(){ // does xyz } } 我的问题是,如果我需要模拟getInstance方法,是否有必要模拟“anotherStaticMethod”? PowerMock版本:1.3,Mockito版本:1.8

Mockito / Powermockito模拟私有空方法

我需要使用mockito和powermock来模拟一个不使用参数的私有void方法。 该方法属于间谍的实例。 我知道我需要这样做的事实表明代码不好但我正在使用一个旧项目将unit testing从一个测试框架转换到另一个测试框架。 如果有人有任何建议,将不胜感激。 谢谢! 到目前为止,我试过这个: PowerMockito.doNothing().when(Whitebox.invokeMethod(spy,”method”,null)); 但我得到这个错误: No method found with name ‘method’ with parameter types: [ ]

PowerMockito:使用matchers模拟静态方法时得到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 。 我的问题是: 当所有参数都使用匹配器时,为什么会出现此exception? 怎么解决? 我调试了它,发现isA(Log.class)返回null。 当我将@PrepareForTest批注添加到测试类并运行测试时,junit没有响应。 为什么? 编辑 我试图不使用参数匹配器,并得到了 org.mockito.exceptions.misusing.MissingMethodInvocationException:when()需要一个必须是’模拟上的方法调用’的参数。 例如:when(mock.getArticles())。thenReturn(articles); […]