powermock

Why EclEmma doesn't coverage code with tests with @RunWith(PowerMockRunner.class)

烂漫一生 提交于 2019-12-21 04:03:26
问题 I am using EclEmma with Eclipse to help me to know where is missing code tests in my project, but all tests with @RunWith(PowerMockRunner.class) are not called and thus not tested. I´m using JUnit 4.8.1 with Mockito. What could it be? 回答1: Its a known bug reported for both parties: http://code.google.com/p/powermock/issues/detail?id=402 https://github.com/jacoco/eclemma/issues/15#issuecomment-9565210 eCoberture seems however to provide correct coverage. The only problem, that it seems not to

PowerMockito: NotAMockException on a mock

房东的猫 提交于 2019-12-21 03:45:41
问题 Bit of a complicated setup. Robolectric, PowerMockito rule-based config. @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) // Using "PrepareOnlyThis" prevents powermock from trying to instrument the whole hierarchy, // part of which we've ignored (android.os.* in this case) @PrepareOnlyThisForTest({ServiceCallbackBase.class}) // this class extends Handler, // so we need

How to mock DriverManager.getConnection(…)?

萝らか妹 提交于 2019-12-21 00:46:35
问题 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

Using PowerMock with Spock

别来无恙 提交于 2019-12-20 14:19:57
问题 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 回答1: I was stuck here for a while too. After searching for hours,

testing powermock simulate http server time out for client call

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 07:29:23
问题 i need to write testcase for connectTimeout and SocketTimeout exception. Am using powerMock to create mock objects. Below is my code. But am getting null pointer exception for my mock objects. Any help appreciated 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

Mockito - how to mock/verify a method call which accepts a new object?

瘦欲@ 提交于 2019-12-20 05:47:20
问题 I have a method (method1) that I'd like to test, which based on parameters provided creates an object and calls another method (method2). So I'm mocking method2, which accepts an object (sampleObj). public void method1(booleanParam) { if(booleanParam){ List<SampleObj> fooList = new ArrayList<SampleObj>; fooList.add(new SampleObj("another param")); anotherService.method2(fooList); } //some other smart logic here } And here's my test with same obfuscated names (sorry if I missed any typo):

Powermock - how to mock a specific method and leave the rest of the object as-is

一笑奈何 提交于 2019-12-20 05:18:06
问题 I have a Person class with get set for FirstName, LastName A TestClass to execute TestCase1 Can we just only mock a specific method (getLastName) and leave every thing else (other internal fields, functions ... as-is)? public class Person { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String

PowerMockito throws NullPointerException when trying to stub private overloaded method

*爱你&永不变心* 提交于 2019-12-20 03:25:17
问题 I'm (still) trying to check if bar(Alpha, Baz) called bar(Xray, Baz) using PowerMockito (as bar(Xray, Baz) is private ) - without actually calling the later, given my MCVE class Foo below. (I went through the same class earlier, with all methods in Foo being public - in case you've got a déjà vu...) public class Foo { private String bar(Xray xray, Baz baz) { return "Xray"; } private String bar(Zulu zulu, Baz baz) { return "Zulu"; } public String bar(Alpha alpha, Baz baz) { if(alpha.get()

java.lang.ClassFormatError: Invalid method Code length while using PowerMock

房东的猫 提交于 2019-12-20 02:38:37
问题 I am running JUnit case with PowerMock in jdk7 using the below libraries : cglib-nodep-2.2.2.jar javassist-3.19.0-GA.jar junit-4.12.jar mockito-all-1.10.19.jar objenesis-2.1.jar powermock-mockito-1.6.2-full.jar Below is code snippet from my Unit Test in which I am using PowerMock @RunWith(PowerMockRunner.class) @PrepareForTest(ValidateBindingImpl.class) public class ValidateBindingImplTest{ //more code follows } While executing this test case I am getting following error: java.lang

Spy object by Mockito in Spring

家住魔仙堡 提交于 2019-12-19 19:52:12
问题 When I try to Spy an object in my unit test, I got an exception. This is my unit test file: @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()); }