Search code examples
javajunitpowermockito

PowerMockito.whenNew is not working


Hi folks i am new to PowerMockito and i am trying to use whenNew in PoweMockito and its not working for me, can anyone please help me out with this??

Below is my Test method which is used to test Class2, and i have Used PowerMockito.whenNew for mocking mockTestMethod inside Class2 and return String Value as "MOCKED VALUE" but that is not happening and actually the method is being executed and output is "PassedString". If i am not wrong the Output should have string as "Inside Class2 method MOCKED VALUE" but i am getting output as "Inside Class2 method PassedString." Please help me with the issue, Thanks In Advance.

Below is the complete program which i am working on

package com.hpe.testing2;

public class Class2 {

    public void testingMethod(){
        Class1 class1 = new Class1();
        String result = class1.mockTestMethod("PassedString");
        System.out.println("Inside Class2 method " + result);
    }

}

package com.hpe.testing2;

public class Class1 {

    public String mockTestMethod(String str2){
        String str1="SomeString";
        str1 = str2;
        System.out.println("Inside MockTest Method " + str1);
        return str1;
    }

}

class2 is invoking Class1 mockTestMethod internally as shown above.

package com.hpe.testing2;


import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


@RunWith(PowerMockRunner.class)
@PrepareForTest({Class2.class,Class1.class})
public class ClassTest {

    public static void main(String[] args) throws Exception {
        ClassTest testing = new ClassTest();
        testing.runMethod();
    }

    public void runMethod() throws Exception{
        Class2 class2 = new Class2();
        Class1 class1 = PowerMockito.mock(Class1.class);
        PowerMockito.whenNew(Class1.class).withAnyArguments().thenReturn(class1);
        PowerMockito.when(class1.mockTestMethod(Mockito.anyString())).thenReturn("MOCKED
 VALUE");
        class2.testingMethod();
    }

}

Solution

  • You cannot start a test class via main method. Instead it should be run with JUnit. Therefore a @Test annotation has to be present at the test method. Look here for getting started with JUnit.

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({ Class2.class, Class1.class })
    public class ClassTest {
    
        @Test
        public void runMethod() throws Exception {
            Class2 class2 = new Class2();
            Class1 class1 = PowerMockito.mock(Class1.class);
    
            PowerMockito.whenNew(Class1.class).withAnyArguments().thenReturn(class1);
            PowerMockito.when(class1.mockTestMethod(Mockito.anyString())).thenReturn("MOCKED VALUE");
            class2.testingMethod();
        }
    
    }
    

    (I left out imports in your testclass)