静态方法 单元测试 springboot+mokito+junit5

CodecUtils的方法是静态类,使用@InjectMocks不能有用,因为这个注解只能用于非静态的对象。

想要为静态方法写单元测试,可以使用Mockito.mockStatic(Class classToMock)方法,它可以返回一个MockedStatic对象,用于模拟静态方法的调用。

1.导入依赖

<dependency>
   <groupId>org.springframework.bootgroupId>  
    <artifactId>spring-boot-starter-testartifactId>
    <version>2.1.18.RELEASEversion>
    <scope>testscope>
    <exclusions>
        <exclusion>
            <groupId>org.mockitogroupId>
            <artifactId>mockito-coreartifactId>
        exclusion>
    exclusions>
dependency>


<dependency> 
    <groupId>org.mockitogroupId>     
    <artifactId>mockito-junit-jupiterartifactId> 
    <version>5.3.1version> 
    <scope>testscope>
 dependency> 


<dependency>
    <groupId>org.mockitogroupId>
    <artifactId>mockito-coreartifactId>
    <version>4.1.0version>
    <scope>testscope>
dependency>


<dependency>
    <groupId>org.mockitogroupId>
    <artifactId>mockito-inlineartifactId>
    <version>4.1.0version>
    <scope>testscope>
dependency>
           

2.单元测试

import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class CodecUtilsTest {

    @Test
    void testBytesToBinary() {
        byte[] bytes = {1, 2, 3};
        String expected = "000000010000001000000011";
        try (MockedStatic<CodecUtils> mocked = Mockito.mockStatic(CodecUtils.class)) {
            mocked.when(() -> CodecUtils.bytesToBinary(bytes)).thenReturn(expected);
            String actual = CodecUtils.bytesToBinary(bytes);
            assertEquals(expected, actual);
            mocked.verify(() -> CodecUtils.bytesToBinary(bytes));
        }
    }

    @Test
    void testBinaryToBytes() {
        String binary = "000000010000001000000011";
        byte[] expected = {1, 2, 3};
        try (MockedStatic<CodecUtils> mocked = Mockito.mockStatic(CodecUtils.class)) {
            mocked.when(() -> CodecUtils.binaryToBytes(binary)).thenReturn(expected);
            byte[] actual = CodecUtils.binaryToBytes(binary);
            assertArrayEquals(expected, actual);
            mocked.verify(() -> CodecUtils.binaryToBytes(binary));
        }
    }

    @Test
    void testBytesToHex() {
        byte[] bytes = {1, 2, 3};
        String expected = "010203";
        try (MockedStatic<CodecUtils> mocked = Mockito.mockStatic(CodecUtils.class)) {
            mocked.when(() -> CodecUtils.bytesToHex(bytes)).thenReturn(expected);
            String actual = CodecUtils.bytesToHex(bytes);
            assertEquals(expected, actual);
            mocked.verify(() -> CodecUtils.bytesToHex(bytes));
        }
    }

    @Test
    void testHexToBytes() throws Exception {
        String hex = "010203";
        byte[] expected = {1, 2, 3};
        try (MockedStatic<CodecUtils> mocked = Mockito.mockStatic(CodecUtils.class)) {
            mocked.when(() -> CodecUtils.hexToBytes(hex)).thenReturn(expected);
            byte[] actual = CodecUtils.hexToBytes(hex);
            assertArrayEquals(expected, actual);
            mocked.verify(() -> CodecUtils.hexToBytes(hex));
        }
    }

    @Test
    void testBytesToDecimal() {
        byte[] bytes = {1, 2, 3};
        String expected = "66051";
        try (MockedStatic<CodecUtils> mocked = Mockito.mockStatic(CodecUtils.class)) {
            mocked.when(() -> CodecUtils.bytesToDecimal(bytes)).thenReturn(expected);
            String actual = CodecUtils.bytesToDecimal(bytes);
            assertEquals(expected, actual);
            mocked.verify(() -> CodecUtils.bytesToDecimal(bytes));
        }
    }

    @Test
    void testDecimalToBytes() {
        String decimal = "66051";
        byte[] expected = {1, 2, 3};
        try (MockedStatic<CodecUtils> mocked = Mockito.mockStatic(CodecUtils.class)) {
            mocked.when(() -> CodecUtils.decimalToBytes(decimal)).thenReturn(expected);
            byte[] actual = CodecUtils.decimalToBytes(decimal);
            assertArrayEquals(expected, actual);
            mocked.verify(() -> CodecUtils.decimalToBytes(decimal));
        }
    }

    @Test
    void testBytesToAscii() {
        byte[] bytes = {65, 66, 67};
        String expected = "ABC";
        try (MockedStatic<CodecUtils> mocked = Mockito.mockStatic(CodecUtils.class)) {
            mocked.when(() -> CodecUtils.bytesToAscii(bytes)).thenReturn(expected);
            String actual = CodecUtils.bytesToAscii(bytes);
            assertEquals(expected, actual);
            mocked.verify(() -> CodecUtils.bytesToAscii(bytes));
        }
    }

    @Test
    void testAsciiToBytes() {
        String ascii = "ABC";
        byte[] expected = {65, 66, 67};
        try (MockedStatic<CodecUtils> mocked = Mockito.mockStatic(CodecUtils.class)) {
            mocked.when(() -> CodecUtils.asciiToBytes(ascii)).thenReturn(expected);
            byte[] actual = CodecUtils.asciiToBytes(ascii);
            assertArrayEquals(expected, actual);
            mocked.verify(() -> CodecUtils.asciiToBytes(ascii));
        }
    }
}

可以参考如下地址,了解如何使用Mockito.mockStatic方法。
① Mocking Static Methods With Mockito | Baeldung.
另外,可以使用PowerMockito工具来模拟静态方法,但是它需要额外的配置和依赖。可以查看如下回答。
② java - Mockito InjectMocks into static object - Stack Overflow.
③ unit testing - Java Mockito inject a static class - Stack Overflow.

你可能感兴趣的:(Java笔记心得,java,单元测试)