SpringBoot——使用powermock进行静态方法的单元测试

关注:CodingTechWork,一起学习进步。
@[toc]

引言

  xxxService类引用了xxxUtils类的xxxstaticMethod()方法,当对xxxService类进行单元测试的时候,如何进行呢?

service层单测

无静态方法引用

依赖


   org.junit.platform
   junit-platform-launcher
   test


   org.springframework.boot
   spring-boot-starter-test

service层类

@Service
public class DemoServiceImpl implements DemoService {
   @Override
   public String aMethod(String key) {
       ... ...
   }
}

service层测试类

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoServiceImplTest{
    @Autowired
    private DemoService demoService;
    
    @Test
    public void testAMethod() {
        String key = "xxx";
        String res = "yyy";
        Assert.assertEquals(demoService.aMethod(key), res);
    }
    
}

有静态方法引用

依赖


   org.junit.platform
   junit-platform-launcher
   test


   org.springframework.boot
   spring-boot-starter-test


   org.powermock
   powermock-api-mockito2
   2.0.9


   org.powermock
   powermock-module-junit4
   2.0.9

service层类

@Service
public class DemoServiceImpl implements DemoService {
   @Override
   public String aMethod(String key) {
       ... ...
       try {
       String res0 = XXXUtils.staticBMethod(key);
       ... ...
       } catch (IOException) {
       ... ...
       }
       ... ...
   }
}

service层测试类

import xxx.xxx.XXXUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
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.PowerMockRunnerDeletegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@PrepareForTest(XXXUtil.class)
@PowerMockIgnore("javax.management.*")
public class DemoServiceImplTest{
    @Autowired
    private DemoService demoService;
    
    @Test
    public void testAMethodWithoutException() {
        String key = "xxx";
        String res0 = "yyy";
        String res = "zzz";
        PowerMockitio.mockStatic(XXXUtil.class);
        PowerMockito.when(XXXUtil.staticBMethod(Mockito.anyString())).thenReturn(res0);
        Assert.assertEquals(demoService.aMethod(key), res);
    }
    @Test
    public void testAMethodWithException() {
        String key = "xxx";
        String res0 = "yyy";
        String res = "zzz";
        PowerMockitio.mockStatic(XXXUtil.class);
        PowerMockito.when(XXXUtil.staticBMethod(Mockito.anyString())).thenThrow(IOException.class);
        Assert.assertEquals(demoService.aMethod(key), res);
    }
}

你可能感兴趣的:(SpringBoot——使用powermock进行静态方法的单元测试)