Dubbo+SpringBoot单元测试mock静态方法

引入的maven依赖

        
            org.powermock
            powermock-api-mockito2
            2.0.9
            test
        
        
            org.powermock
            powermock-module-junit4
            2.0.9
            test
        
        
            org.junit.platform
            junit-platform-launcher
            1.10.1
            test
        

类头部注解与相关引入包


import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@ActiveProfiles("test")
@RunWith(PowerMockRunner.class)
@PrepareForTest(LoginInfoUtil.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore({"javax.net.ssl.*","javax.management.*", "javax.security.*", "javax.crypto.*"})
public class MockTest {


    @Autowired
    private ContractService contractService;

    @Test
    public void test(){
        PowerMockito.mockStatic(LoginInfoUtil.class);
        PowerMockito.when(LoginInfoUtil.getUserId()).thenReturn(10000015L);
        contractService.test();
    }

}

你可能感兴趣的:(Java,dubbo,spring,boot,数据库)