Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

junit - PowerMock mock static not able to mock a class

I have a test case which uses power mock to run. Below is the code

ParamControllerTest.java

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(XYZ.class)
    public class ParamControllerTest{
    
    private HttpServletRequest request;
    private ParamController paramController;
    private ParamVO2 paramVO2;
    private String userId = "123456";
    private String access = "re";
    
    @Mock
    private XYZdaoMock xYZdaoMock;
    
    @Test
        public void testAddSecty(){
            request = PowerMockito.mock(HttpServletRequest.class);
// You do get request value as HttpServletRequest
            PowerMockito.mockStatic(XYZ.class);
            when(XYZ.getId(request)).thenReturn(id);
            when(XYZ.getAccess(userId, "bcd")).thenReturn("re");
            when(XYZ.getId(request)).thenReturn("123456");
            when(xYZdaoMock.updateKey(paramVO2)).thenReturn(paramVO2);
            paramController.addParam(paramVO2);
            verify(xYZdaoMock).updateKey(paramVO2); 
            
        }
    }

ParamController.java

@Controller
@RequestMapping(value="/param")
@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class)
public class ParamController {
    
@RequestMapping(value="/abc", method = RequestMethod.POST)
    public @ResponseBody ParamVO addParam(@RequestBody ParamVO paramVO){
        ParamVO paramVO = null;
        String userId = XYZ.getId(request);
// userId result null as request is null and due to which after this it result in NullPointerException
        // Code
        return paramVO;             
        
    }
}

Considering the null pointer getting on userId in controller, PowerMockito.mockStatic(XYZ.class) i think this is giving error. I read about this and apparently this does not work with proper version of mockito and power mockito. I tried commenting out the mockito dependency in pom so that it does not conflict with version of power mockito, but it did not work. Below are the version that i have tried of power mockito but none of them worked. Spring version used is 4.3.19. Power Mockito version tried is 1.4.12 which gives Intialization error of XYZ.class while running the test case and 1.7.3 and the result is NullPointerException as shown above.

pom.xml

    <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
                <scope>test</scope>
            </dependency>
            <!-- <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>1.10.19</version>
                <scope>test</scope>
            </dependency> -->
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4</artifactId>
                <version>1.7.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4-rule</artifactId>
                <version>1.7.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-classloading-xstream</artifactId>
                <version>1.7.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito</artifactId>
                <version>1.7.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4-legacy</artifactId>
                <version>1.7.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-easymock</artifactId>
                <version>1.7.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.easymock</groupId>
                <artifactId>easymock</artifactId>
                <version>3.0</version>
                <scope>test</scope>
            </dependency>
<!-- <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.20.0.GA</version>
        </dependency> -->

Error Trace

java.lang.NullPointerException
    at com.Value(ParamController.java:184)
    at com..testAddSecty(ParamControllerTest.java:218)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)

Any help how to fix this.

question from:https://stackoverflow.com/questions/65915284/powermock-mock-static-not-able-to-mock-a-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...