单元测试框架 powermock
单元测试框架 powermock
在 pom.xml 加入依赖包:
  <properties>
	    <powermock.version>2.0.9</powermock.version>
  </properties>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>javassist</artifactId>
            <groupId>org.javassist</groupId>
        </exclusion>
        <exclusion>
            <artifactId>objenesis</artifactId>
            <groupId>org.objenesis</groupId>
        </exclusion>
        <exclusion>
            <artifactId>mockito-core</artifactId>
            <groupId>org.mockito</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>${powermock.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>javassist</artifactId>
            <groupId>org.javassist</groupId>
        </exclusion>
        <exclusion>
            <artifactId>objenesis</artifactId>
            <groupId>org.objenesis</groupId>
        </exclusion>
    </exclusions>
</dependency>
在测试类中引入相关的依赖类:
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
在类头上加入注解:
指定运行器的注意:@RunWith ( 类名.class )
如:
@RunWith ( PowerMockRunner.class )
静态类预加载的注解:
    多个用英文状态下的逗号分隔
  @PrepareForTest ( value = { 静态类名.class, 静态类名.class, 静态类名.class } )
  如:
  @PrepareForTest ( value = { SpringContextAware.class, BeanUtil.class, IoUtil.class } )
注入静态类:  PowerMockito.mockStatic(静态类名.class);
  如:
  PowerMockito.mockStatic(IoUtil.class);
模拟静态方法:
when(IoUtil.readUtf8(file.getInputStream())).thenReturn("读取文件");
或
PowerMockito.when(IoUtil.readUtf8(file.getInputStream())).thenReturn("读取文件");
 
注入属性:
  @InjectMocks
      private RoleServiceImpl roleServiceImpl;
   @Mock
      private   RoleMapper roleMapper;
  @Before
      public void setUp () {
            MockitoAnnotations.initMocks(this);
     FieldSetter.setField(roleServiceImpl, Whitebox.getField(RoleServiceImpl.class, "roleMapper"), roleMapper);  // 解决多层依赖注入和双重依赖注入不是同一个实例的问题。
      }
模拟文件:
  import org.springframework.mock.web.MockMultipartFile;
  import org.springframework.web.multipart.MultipartFile;
// 文件的内容推荐至少 2 行,因为 Spring 读取文件时将首行默认为头(head),如果读取不到第二行认为没有内容。
MultipartFile multipartFile = new MockMultipartFile("user.csv", "1\n2".getBytes());
或
MultipartFile multipartFile = new MockMultipartFile("user.csv", "originalFilename", "contentType", "1\n2".getBytes());
或
MultipartFile multipartFile = PowerMockito.mock(MultipartFile.class);
  try {
                PowerMockito.when(multipartFile.getInputStream()).thenReturn(null);
                PowerMockito.when(multipartFile.getOriginalFilename()).thenReturn("originalFilename.csv");
          } catch (Exception e) {
          }
 
 
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号