powermockito

PowerMock is throwing java.lang.ClassFormatError with Struts2 Action

时光毁灭记忆、已成空白 提交于 2019-12-11 11:03:39
问题 I am using Powermockito with below JAR mockito-all-1.10.19.jar, cglib-nodep-2.2.2.jar, javassist-3.19.0-GA.jar, junit-4.12.jar, objenesis-2.1.jar, powermock-mockito-1.6.2-full.jar, hamcrest-all-1.3.jar When running test for my action I am getting initializationError with failure stacktrace as follows java.lang.ClassFormatError: JVMCFRE013 local variable PC length invalid; class=com/xyz/action/ActionCommon, offset=9351 at java.lang.ClassLoader.defineClassImpl(Native Method) at java.lang

Faking Method parameters using Mockito/PowerMockito

主宰稳场 提交于 2019-12-11 10:56:03
问题 public class FakeExcelSheet implements Sheet { private final Map<Integer, FakeExcelRow> fakeSheet = new HashMap<Integer, FakeExcelRow>(); @Override public Row createRow(int rowNum) { Row row = new FakeExcelRow(rowNum, fakeSheet); fakeSheet.put(rowNum, (FakeExcelRow)row); return row; } @Override public Row getRow(int rowNum) { if (fakeSheet.get(rowNum) != null) { return fakeSheet.get(rowNum); } else { return createRow(rowNum); } } /**Other unimplemented methods of Apache POI class Sheet**/ }

How to use PowerMockito to mock new class instance inside a static method

此生再无相见时 提交于 2019-12-11 10:17:19
问题 I have a class with static method, inside the static method I creating an instance of a different class and save it inside a map. I need to write unit tests for this method. What is the best way to do it? For example: public class MyClass { public static synchronized void method (String str1, String str2, Object obj){ do something..... DifferentClass dc = new DifferentClass(str1,str2,obj); dc.methodCall(); do something..... } } Tried to do it with: DifferentClass dc = PowerMockito.mock

Unable to use mocking to throw an exception - the thrown exception is not being caught

▼魔方 西西 提交于 2019-12-11 07:45:50
问题 I have a method where I'd like to mock an exception being thrown so that the catch statement is entered: public static String func(String val) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); return Base64.encode(md5.digest(val.getBytes())); } catch (NoSuchAlgorithmException toCatch) { return "*"; } } The test I've written is this: @Test public void testFunc() throws Exception { MessageDigest md5 = PowerMockito.mock(MessageDigest.class); PowerMockito.when(md5.getInstance

Mocking of a constructor which gives the base class reference using Powermockito in java

痴心易碎 提交于 2019-12-11 06:48:15
问题 Here is the scenario, I have something like this in one of my method of a class MyClass public class MyClass{ public Object build(Map map) { BaseClass cls; if(SomeconditionTrue) { cls = new ChildClass1(new ABC()); } else { cls = new ChildClass2(new ABC()); } cls.callMethod(); } } For the above scenario, I am writing a test case using PowerMockito, I want to mock this method call, cls.callMethod() . When i am trying to mock, it calls the actual Method callMethod() which is failing. can some

Mockito + PowerMock + TestNG + Libgdx

僤鯓⒐⒋嵵緔 提交于 2019-12-11 05:32:17
问题 I'm trying add the powermock library to the working project, but I'm getting errors. How I add it: 1) AbsTest extends PowerMockTestCase 2) Build.gradle dependencies 3) In some test add @PrepareForTest({SomeClass.class}) . After this step this error occured. In build.gradle all libraries are included. Error occured in a place where initialize HeadlessApplication for use Gdx.* static vars. All tests in project extend this class: abstract public class AbsTest extends PowerMockTestCase { static {

Android test method called after presenter executes retrofit call

大憨熊 提交于 2019-12-11 04:39:19
问题 I'm using Mockito to test my views but my tests are failing because of a method that is supposed to be called after a retrofit call is complete. How can I mock a view who's method is called by presenter after completion of a retrofit call? I'd like to verify that unBlockUI() below has been called. My tests show blockUI() is called but unblockUI() is not being called. I get a fail message Wanted but not invoked: view.unBlockUI(); In my presenter I have the method public void fetchResults(){

org.powermock.reflect.exceptions.MethodNotFoundException: - when mocking java sql classes

放肆的年华 提交于 2019-12-11 04:37:20
问题 I am trying to write unit test for a servlet containing a call to a Postgres database. I am mocking the Connection , Statement and ResultSet classes. However, any method calls on the mocks of those calls give me a MethodNotFoundException . There are some references where it mentions some functionality was broken with Java 8 and PowerMock 1.5.x but I am on PowerMock 1.6.6. Any help is highly appreciated. Here's the code: public class CitiesServlet extends HttpServlet { private static final

PowerMockito+Junit - Mocking System.getenv

佐手、 提交于 2019-12-11 04:07:08
问题 The line i try to mock in class is: String x[] = System.getenv("values").split(",") for(int i=0;i<=x.length;i++){ //do something } As far I have written is as follows: @RunWith(PowerMockRunner.class) @PrepareForTest({System.class}) public class test{ @Test public void junk(){ PowerMockito.mockStatic(System.class); PowerMockito.when( System.getenv("values"))).thenReturn("ab,cd"); } } On debug, I get null pointer in for loop line. On inspecting System.getenv("values") in codebase, it is still

Scala / PowerMockito - Java final classes causing build error in Scala Tests using Powermockito

时间秒杀一切 提交于 2019-12-10 17:32:19
问题 I have a final class: public final class AClass { private final AConfig aClassConfig; public final static BeanName = "aClass" } and I'm trying to mock it in tests: @RunWith(classOf[PowerMockRunner]) @PrepareForTest(Array(classOf[AClass])) class AClassTests extends FunSuite { test("mock final class test") { val aClass = PowerMockito.mock(classOf[AClass]) assert(aClass != null) } } This causes the error: Cannot subclass final class class com.me.AClass java.lang.IllegalArgumentException: Cannot