powermock

PowerMock, mockito, verify static method

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm trying to get PowerMock to work with mockito, and I'm following the documentation here: http://code.google.com/p/powermock/wiki/MockitoUsage13 . To simplify a bit, lets say that I have a static method: StaticObj . put ( String key , String val ) { ... } And the class to be tested does something like this: public class ClassToTest { public void doSomething ( Params p ) { if ( StringUtils . isNotBlank ( p . getK ()) StaticObj . put ( "k1" , p . getK ()); if ( StringUtils . isNotBlank ( p . getX ()) StaticObj . put ( "x1" , p .

How to mock DriverManager.getConnection(…)?

别说谁变了你拦得住时间么 提交于 2019-12-03 06:51:30
I have a class, which connects to an H2 database and runs several SQL statements. public class H2Persistence implements IPersistence { private Connection conn; @Override public void open() { try { Class.forName("org.h2.Driver"); conn = DriverManager.getConnection(CONN_TYPE_USER_HOME); final Statement stmt = conn.createStatement(); stmt.executeUpdate("CREATE TABLE PERSON(" + "ID BIGINT,"+ "AGEGROUP VARCHAR(255),"+ "MONTHLY_INCOME_LEVEL VARCHAR(255)," + "GENDER VARCHAR(1),"+ "HOUSEHOLD_ID BIGINT)"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e

Which Maven artifacts should I use to import PowerMock?

回眸只為那壹抹淺笑 提交于 2019-12-03 05:50:17
What jars do I need to add to my pom.xml to get PowerMock working with Mockito? I have the following dependencies: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-support</artifactId> <version>1.4.11</version> <scope>test</scope> </dependency> but when I add the @PrepareForTest

Failing integration test for Apache Spark Streaming

送分小仙女□ 提交于 2019-12-03 05:21:35
I've been trying to track down an issue with some unit/integration tests I've been writing for an Apache Spark project. When using Spark 1.1.1 my test passed. When I tried to upgrade to 1.4.0 (also tried 1.4.1) the test starts failing. I've managed to reduce the code needed to reproduce the issue down to the small integration test below. Interestingly, if I comment out the @RunWith annotation on the test then the test passes correctly. Obviously I don't need the @RunWith annotation for this cut down test, but the real tests make use of mocks fairly extensively, so I'd rather not have to drop

Using PowerMock with Spock

丶灬走出姿态 提交于 2019-12-03 03:46:01
I have a class with a few static methods.I need to Mock these static methods. I know PowerMock does this,but I was not able to find any tutorials/materials that shed some light on "Spock+PowerMock" integration. I prefer Spock to Junit,hence the conundrum. Is there a way of getting these 2 frameworks to play ball?Any help is much appreciated.Sample code,even more so. Update: Current Status of the Approach Spock behaving weirdly juanpaolo I was stuck here for a while too. After searching for hours, I saw this github repo: https://github.com/kriegaex/Spock_PowerMock . I tried adding a

How to mock DriverManager.getConnection(…)?

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a class, which connects to an H2 database and runs several SQL statements. public class H2Persistence implements IPersistence { private Connection conn; @Override public void open() { try { Class.forName("org.h2.Driver"); conn = DriverManager.getConnection(CONN_TYPE_USER_HOME); final Statement stmt = conn.createStatement(); stmt.executeUpdate("CREATE TABLE PERSON(" + "ID BIGINT,"+ "AGEGROUP VARCHAR(255),"+ "MONTHLY_INCOME_LEVEL VARCHAR(255)," + "GENDER VARCHAR(1),"+ "HOUSEHOLD_ID BIGINT)"); } catch (ClassNotFoundException e) { e

AndroidStudio/Gradle with powermock

匿名 (未验证) 提交于 2019-12-03 02:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions. Could anybody show a correct way to do it? Thanks. 回答1: Im posting in order to help future readers, you need to add these dependencies for powermock in AS testCompile 'junit:junit:4.12' testCompile 'org.powermock:powermock-api-mockito:1.6.1' testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.1' testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1' testCompile 'org.powermock:powermock

Powermock does not create mock for java.time.ZonedDateTime

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I tried to create mock for java.time.ZonedDateTime using PowerMockito and I was expecting the mock object for ZonedDateTime. Instead, actual object is getting created and hence I cannot mock the methods of ZonedDateTime class. Following is my code snippet import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import static org.mockito.Matchers.any;

Unable to run JUnit test with PowerMockRunner

匿名 (未验证) 提交于 2019-12-03 01:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a Gradle based Java project were I now want to mock a private method using PowerMock. The problem is that I am not able to use the PowerMockRunner as I always get the following exception when I add the @RunWith(org.powermock.modules.junit4.PowerMockRunner.class) annotation. Error: org . powermock . reflect . exceptions . FieldNotFoundException : Field 'fTestClass' was not found in class org . junit . internal . runners . MethodValidator . at org . powermock . reflect . internal . WhiteboxImpl . getInternalState (

AndroidStudio/Gradle with powermock

匿名 (未验证) 提交于 2019-12-03 01:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions. Could anybody show a correct way to do it? Thanks. 回答1: Im posting in order to help future readers, you need to add these dependencies for powermock in AS testCompile 'junit:junit:4.12' testCompile 'org.powermock:powermock-api-mockito:1.6.1' testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.1' testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1' testCompile 'org.powermock:powermock