Search Results

Search found 10 results on 1 pages for 'powermock'.

Page 1/1 | 1 

  • Junit Parameterized tests together with Powermock - how!?

    - by Subtwo
    Hi! I've been trying to figure out how to run parameterized tests in Junit4 together with PowerMock. The problem is that to use PowerMock you need to decorate your test class with @RunWith(PowerMock.class) and to use parameterized tests you have to decorate with @RunWith(Parameterized.class) From what I can see they seem mutually excluded!? Is this true? Is there any way around this? I've tried to create a parameterized class within a class running with PowerMock; something like this: @RunWith(PowerMock.class) class MyTestClass { @RunWith(Parameterized.class) class ParamTestClass { // Yadayada } } But unfortunately this doesn't do much good... The ParamTestClass still doesn't run with PowerMock support (not that surprisingly maybe)... And I've kind of run out of ideas so any help is greatly appreciated!

    Read the article

  • PowerMock Mockito static methods

    - by anergy
    Do we need to mock all static methods of a class when using PowerMock (with Mockito)? I mean, suppose we have: class MockMe { public static MockMe getInstance(){ //return new Instance via complex process; } public static List<X> anotherStaticMethod(){ // does xyz } } My question, if I need to mock getInstance method, is it necessary to mock "anotherStaticMethod" as well? PowerMock version:1.3, Mockito version:1.8

    Read the article

  • Powermock Slows Down Test Startup on Eclipse/Fedora 10 when on NTFS partition

    - by MrWiggles
    I've just started having a proper play with Powermock and noticed that it slows down test startup immensely. A quick look at top while it was running shows that mount.nfts-3g was taking up most of the CPU. I moved Eclipse and my source directory to ext3 partitions to see if that was a problem and the tests now startup quicker but there's still a noticeable delay. Is this normal with Powermock or am I missing something obvious?

    Read the article

  • powermock : ProcessBuilder redirectErrorStream giving nullPointerException

    - by kaustubh9
    I am using powermock to mock some native command invocation using process builder. the strange thing is these test pass sometimes and fail sometimes giving a NPE. Is this a powermock issue or some gotcha in the program. the snippet of the class under test is.. public void method1(String jsonString, String filename) { try { JSONObject jObj = new JSONObject(jsonString); JSONArray jArr = jObj.getJSONArray("something"); String cmd = "/home/y/bin/perl <perlscript>.pl<someConstant>" + " -k " + <someConstant> + " -t " + <someConstant>; cmd += vmArr.getJSONObject(i).getString("jsonKey"); ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd); pb.redirectErrorStream(false); Process shell = pb.start(); shell.waitFor(); if (shell.exitValue() != 0) { throw new RuntimeException("Error in Collecting the logs. cmd="+cmd); } StringBuilder error = new StringBuilder(); InputStream iError = shell.getErrorStream(); BufferedReader bfr = new BufferedReader( new InputStreamReader(iError)); String line = null; while ((line = bfr.readLine()) != null) { error.append(line + "\n"); } if (!error.toString().isEmpty()) { LOGGER.error(error`enter code here`); } iError.close(); bfr.close(); } catch (Exception e) { throw new RuntimeException(e); } and the unit test case is .. @PrepareForTest( {.class, ProcessBuilder.class,Process.class, InputStream.class,InputStreamReader.class, BufferedReader.class} ) @Test(sequential=true) public class TestClass { @Test(groups = {"unit"}) public void testMethod() { try { ProcessBuilder prBuilderMock = createMock(ProcessBuilder.class); Process processMock = createMock(Process.class); InputStream iStreamMock = createMock(InputStream.class); InputStreamReader iStrRdrMock = createMock(InputStreamReader.class); BufferedReader bRdrMock = createMock(BufferedReader.class); String errorStr =" Error occured"; String json = <jsonStringInput>; String cmd = "/home/y/bin/perl <perlscript>.pl -k "+<someConstant>+" -t "+<someConstant>+" "+<jsonValue>; expectNew(ProcessBuilder.class, "bash", "-c", cmd).andReturn(prBuilderMock); expect(prBuilderMock.redirectErrorStream(false)).andReturn(prBuilderMock); expect(prBuilderMock.start()).andReturn(processMock); expect(processMock.waitFor()).andReturn(0); expect(processMock.exitValue()).andReturn(0); expect(processMock.getErrorStream()).andReturn(iStreamMock); expectNew(InputStreamReader.class, iStreamMock) .andReturn(iStrRdrMock); expectNew(BufferedReader.class, iStrRdrMock) .andReturn(bRdrMock); expect(bRdrMock.readLine()).andReturn(errorStr); expect(bRdrMock.readLine()).andReturn(null); iStreamMock.close(); bRdrMock.close(); expectLastCall().once(); replayAll(); <ClassToBeTested> instance = new <ClassToBeTested>(); instance.method1(json, fileName); verifyAll(); } catch (Exception e) { Assert.fail("failed while collecting log.", e); } } I get an error on execution an the test case fails.. Caused by: java.lang.NullPointerException at java.lang.ProcessBuilder.start(ProcessBuilder.java:438) Note : I do not get this error on all executions. Sometimes it passes and sometimes it fails. I am not able to understand this behavior.

    Read the article

  • java - powermock whenNew doesnt seem to work, calls the actual constructor

    - by user1331243
    I have two final classes that are used in my unit test. I am trying to use whenNew on the constructor of a final class, but I see that it calls the actual constructor. The code is @PrepareForTest({A.class, B.class, Provider.class}) @Test public void testGetStatus() throws Exception { B b = mock(B.class); when(b.getStatus()).thenReturn(1); whenNew(B.class).withArguments(anyString()).thenReturn(b); Provider p = new Provider(); int val = p.getStatus(); assertTrue((val == 1)); } public class Provider { public int getStatus() { B b = new B("test"); return b.getStatus(); } } public final class A { private void init() { // ...do soemthing } private static A a; private A() { } public static A getInstance() { if (a == null) { a = new A(); a.init(); } return a; } } public final class B { public B() { } public B(String s) { this(A.getInstance(), s); } public B(A a, String s) { } public int getStatus() { return 0; } } On debug, I find that its the actual class B instance created and not the mock instance that is returned for new usage and assertion fails. Any pointers on how to get this working. Thanks

    Read the article

  • Error while using PowerMockito.whenNew()

    - by Ankush
    I am using PowerMockito(1.3.6) with Mockito(1.8.3) and Junit(4.7) for testing. I am quite stuck on an error: [junit] Testcase: testNextPNRFromQueueHappyPath(com.orbitz.galileo.host.robot.GalpoQueueConnectionTest): Caused an ERROR [junit] org/mockito/internal/IMockHandler [junit] java.lang.NoClassDefFoundError: org/mockito/internal/IMockHandler [junit] at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubsituteMock(DefaultConstructorExpectationSetup.java:80) [junit] at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withArguments(DefaultConstructorExpectationSetup.java:48) [junit] at com.orbitz.galileo.host.robot.GalpoQueueConnectionTest.testNextPNRFromQueueHappyPath(GalpoQueueConnectionTest.java:62) [junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) My class under test : GalpoQueueConnection is basically creating a new object of another class : QueueReadBuilder. I am trying to do something like: @RunWith(PowerMockRunner.class) @PrepareForTest({GalpoQueueConnection.class, ApolloProcUtils.class}) public class GalpoQueueConnectionTest extends TestCase{ @Test public void testNextPNRFromQueueHappyPath() throws Exception { QueueReadBuilder mockBuilder = mock(QueueReadBuilder.class); PowerMockito.whenNew(QueueReadBuilder.class).withArguments(anyString(), anyString(), anyString()).thenReturn(mockBuilder); } } It seems like somehow powermockito is trying to call IMockHandler, and I looked at Mockito 1.8.3 api, and there isn't any. Any suggestions/ clues would be welcome.

    Read the article

  • mockito mock a constructor with parameter

    - by Shengjie
    I have a class as below: public class A { public A(String test) { bla bla bla } public String check() { bla bla bla } } The logic in the constructor A(String test) and check() are the things I am trying to mock. I want any calls like: new A($$$any string$$$).check() returns a dummy string "test". I tried: A a = mock(A.class); when(a.check()).thenReturn("test"); String test = a.check(); // to this point, everything works. test shows as "tests" whenNew(A.class).withArguments(Matchers.anyString()).thenReturn(rk); // also tried: //whenNew(A.class).withParameterTypes(String.class).withArguments(Matchers.anyString()).thenReturn(rk); new A("random string").check(); // this doesn't work But it doesn't seem to be working. new A($$$any string$$$).check() is still going through the constructor logic instead of fetch the mocked object of A.

    Read the article

  • What are tangible advantages to proper Unit Tests over Functional Test called unit tests

    - by Jackie
    A project I am working on has a bunch of legacy tests that were not properly mocked out. Because of this the only dependency it has is EasyMock, which doesn't support statics, constructors with arguments, etc. The tests instead rely on database connections and such to "run" the tests. Adding powermock to handle these cases is being shot down as cost prohibitive due to the need to upgrade the existing project to support it (Another discussion). My questions are, what are the REAL world tangible benifits of proper unit testing I can use to push back? Are there any? Am I just being a stickler by saying that bad unit tests (even if they work) are bad? Is code coverage just as effective?

    Read the article

  • How to deal with static utility classes when designing for testability

    - by Benedikt
    We are trying to design our system to be testable and in most parts developed using TDD. Currently we are trying to solve the following problem: In various places it is necessary for us to use static helper methods like ImageIO and URLEncoder (both standard Java API) and various other libraries that consist mostly of static methods (like the Apache Commons libraries). But it is extremely difficult to test those methods that use such static helper classes. I have several ideas for solving this problem: Use a mock framework that can mock static classes (like PowerMock). This may be the simplest solution but somehow feels like giving up. Create instantiable wrapper classes around all those static utilities so they can be injected into the classes that use them. This sounds like a relatively clean solution but I fear we'll end up creating an awful lot of those wrapper classes. Extract every call to these static helper classes into a function that can be overridden and test a subclass of the class I actually want to test. But I keep thinking that this just has to be a problem that many people have to face when doing TDD - so there must already be solutions for this problem. What is the best strategy to keep classes that use these static helpers testable?

    Read the article

1