6

Description:

I cannot seem to have my stubs or mocks take affect in the class I have under test. I am trying to use the whenNew action so I can mock a return object and then mock a operation on that object with a returned value.

I imagine its something simple I am missing but not seeing it.

SOLUTION: Originally I was running with MockitoRunner.class and it required being changed to PowerMockRunner.class. Code below reflects the solution.

Jars on the classpath:

  • powermock-mockito-1.4.11-full.jar
  • mockoito-all-1.9.0.jar
  • javassist-3.15.0-GA.jar
  • junit-4.8.2.jaf
  • objensis-1.2.jar
  • cglib-nodep-2.2.2.jar

TEST CLASS

   import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import static org.powermock.api.mockito.PowerMockito.*;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    import static org.mockito.Matchers.any;
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(ClassA.class)
    public class ClassATest {

        @Test
        public void test() throws Exception
        {
                String[] returnSomeValue = {"PowerMockTest"};
                String[] inputValue = {"Test1"};
                ClassB mockedClassB = mock(ClassB.class);
                whenNew( ClassB.class).withNoArguments().thenReturn( mockedClassB );
                when( mockedClassB, "getResult", any(String[].class) ).thenReturn(returnSomeValue);       

                IClassA classUnderTest = new ClassA();
                String[] expectedValue = classUnderTest.runTest(inputValue);      
        }

    }

Class A Implementation

public class ClassA implements IClassA {

    @Override
    public String[] runTest(String[] inputValues) {

        String[] result;
        IClassB classB = new ClassB();
        result = classB.getResult(inputValues);

        return result;
    }

} 
observer
  • 2,925
  • 1
  • 19
  • 38
haju
  • 1,278
  • 4
  • 20
  • 38

1 Answers1

10

Since you are using powermock features (@PrepareForTest, PowerMockito.whenNew etc.), you have to run your test with the PowerMockRunner.

@RunWith(PowerMockRunner.class)

Because ClassB#geResult is not private, you may also simplify your code and replace

when( mockedClassB, "getResult", any(String[].class) ).thenReturn(someValue); 

by

when(mockedClassB.getResult(any(String[].class))).thenReturn(someValue);
Niks
  • 4,802
  • 4
  • 36
  • 55
gontard
  • 28,720
  • 11
  • 94
  • 117
  • Tried that and it fails on the any(String[].class) ) saying "misplaced argument matcher detected here. Seems like its very hard to pass an array when mocking. – haju Aug 14 '12 at 20:00
  • I dont understand, it works in my environment.Try with this also : "when(mockedClassB.getResult(any(String[].class))).thenReturn(someValue);" – gontard Aug 14 '12 at 20:17
  • I complete my answer. Please complete your question by providing some details on your environment : junit, powermock, powermockito and mockito versions. Provides also the value of someValue in ClassATest#mockingTest. – gontard Aug 15 '12 at 06:28
  • I deleted all everything and started fresh and resolved the issue I was having. Updated description to show root issue, resolution and additional configuration. – haju Aug 15 '12 at 14:09