美文网首页
SpringBoot——使用powermock进行静态方法的单元

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

作者: Hughman | 来源:发表于2022-08-24 14:19 被阅读0次

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

    引言

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

    service层单测

    无静态方法引用

    依赖

    <dependency>
       <groupId>org.junit.platform</groupId>
       <artifactId>junit-platform-launcher</artifactId>
       <scope>test</scope>
    </dependency>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    

    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);
        }
        
    }
    

    有静态方法引用

    依赖

    <dependency>
       <groupId>org.junit.platform</groupId>
       <artifactId>junit-platform-launcher</artifactId>
       <scope>test</scope>
    </dependency>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
       <groupId>org.powermock</groupId>
       <artifactId>powermock-api-mockito2</artifactId>
       <version>2.0.9</version>
    </dependency>
    <dependency>
       <groupId>org.powermock</groupId>
       <artifactId>powermock-module-junit4</artifactId>
       <version>2.0.9</version>
    </dependency>
    

    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进行静态方法的单元

          本文链接:https://www.haomeiwen.com/subject/mkqogrtx.html