springboot静态方法mock

1.pom文件添加maven依赖:

        
            2.0.2
        

        
        
        
            org.powermock
            powermock-api-mockito2
            ${powermock.version}
            test
        
        
        
            org.powermock
            powermock-module-junit4
            ${powermock.version}
            test
        
        
        
            org.powermock
            powermock-module-junit4-rule
            ${powermock.version}
            test
        

2.单元测试,在测试类上加以下几个注解:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore( {"javax.management.*", "javax.net.*"})
@PrepareForTest(WebTTsUtil.class)
@SpringBootTest()

WebTTsUtil是我要mock的类,替换成你需要mock的类。

    @Test
    @Transactional
    public void speechSynthesize() throws Exception {
        PowerMockito.mockStatic(WebTTsUtil.class);
        PowerMockito.when(WebTTsUtil.speechSynthesize(Mockito.anyString())).thenReturn(returnMap);
        restReadExercisesMockMvc.perform(post("/api/speech/synthesis")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(map)))
                .andExpect(status().isOk());
    }

3.遇到的问题:
①单独测A测试类能通过,测试所有测试类A测试类通不过,可能是powermock和mockito的版本不适配,版本对应关系如下:

Mockito PowerMock
2.8.9+ 2.x
2.8.0-2.8.9 1.7.x
2.7.5 1.7.0RC4
2.4.0 1.7.0RC2
2.0.0-beta - 2.0.42-beta 1.6.5-1.7.0RC
1.10.8 - 1.10.x 1.6.2 - 2.0
1.9.5-rc1 - 1.9.5 1.5.0 - 1.5.6
1.9.0-rc1 & 1.9.0 1.4.10 - 1.4.12
1.8.5 1.3.9 - 1.4.9
1.8.4 1.3.7 & 1.3.8
1.8.3 1.3.6
1.8.1 & 1.8.2 1.3.5
1.8 1.3
1.7 1.2.5

修改pom文件(junit用的是4.12):

        
                1.7.0RC4
                2.7.5
        
        
            org.springframework.boot
            spring-boot-starter-test
            
                
                    org.mockito
                    mockito-core
                
             
            test
        
        
        
        
            org.powermock
            powermock-api-mockito2
            ${powermock.version}
            
                
                    org.mockito
                    mockito-core
                
            
            test
        
        
        
            org.powermock
            powermock-module-junit4
            ${powermock.version}
            test
        
        
        
            org.powermock
            powermock-module-junit4-rule
            ${powermock.version}
            
                
                    org.mockito
                    mockito-core
                
            
            test
        
        
        
        
            org.mockito
            mockito-core
            ${mockito.version}
            test
        

参考:1.https://blog.csdn.net/icarusliu/article/details/80429257;
2.http://ju.outofmemory.cn/entry/100348;
3.https://stackoverflow.com/questions/43940857/after-upgrade-to-2-7-classnotfoundexception-org-mockito-exceptions-reporter-whe;
4.http://stackmirror.caup.cn/page/sksdmpy4cfg7;
5.https://github.com/powermock/powermock/wiki/Mockito#supported-versions;

你可能感兴趣的:(springboot静态方法mock)