静态方法的单元测试

静态方法的单元测试

在使用工具时,有些工具是静态方法实现,mock静态方法和普通方法不一样,我们使用powermockito来mock静态方法

文章目录

          • 静态方法的单元测试
            • 1.包含静态方法的工具类
            • 2.调用静态方法(简略版)
            • 3.单元测试代码
            • 参考文献:

1.包含静态方法的工具类
package com.compressfile.demo.utils;

import java.io.*;

public class FileByteArrayTransferUtil {
    public static byte[] getBytesByFile(String filePath) {
        try {
            File file=new File(filePath);
            //获取输入流
            FileInputStream fis = new FileInputStream(file);

            //新的 byte 数组输出流,缓冲区容量1024byte
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            //缓存
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                    bos.write(b, 0, n);
            }
            fis.close();
            //改变为byte[]
            byte[] data = bos.toByteArray();
            //
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            // 判断文件目录是否存在
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }
            file = new File(filePath + "\\" + fileName);

            //输出流
            fos = new FileOutputStream(file);

            //缓冲流
            bos = new BufferedOutputStream(fos);

            //将字节数组写出
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



}
2.调用静态方法(简略版)
package com.compressfile.demo.service;

import com.compressfile.demo.utils.FileByteArrayTransferUtil;
import org.springframework.stereotype.Service;

@Service
public class FileTransferService {
    public void callStaticFunction(String filePath){
        byte[] bytesByFile = FileByteArrayTransferUtil.getBytesByFile(filePath);
        System.out.println(bytesByFile.length);
    }
}
3.单元测试代码
package com.compressfile.demo.service;

import com.compressfile.demo.utils.FileByteArrayTransferUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(FileByteArrayTransferUtil.class)
public class FileTransferServiceTest {
    @InjectMocks
    FileTransferService fileTransferService;

    @Before
    public void setUp() throws Exception {
        PowerMockito.mockStatic(FileByteArrayTransferUtil.class);
    }

    @Test
    public void callStaticFunction() {
        byte[] bytes = new byte[10];
        PowerMockito.when(FileByteArrayTransferUtil.getBytesByFile(Mockito.any())).thenReturn(bytes);
        fileTransferService.callStaticFunction("abc");
    }
}

静态方法mock必要条件:

1.@PrepareForTest(静态方法所在类.class)

2.PowerMockito.mockStatic(静态方法所在类.class);

3.像普通方法一样when().thenreturn()


参考文献:

如何使用Powermock对静态方法进行mock

你可能感兴趣的:(spring,boot,单元测试汇总,单元测试,java)