CodecUtils的方法是静态类,使用@InjectMocks不能有用,因为这个注解只能用于非静态的对象。
想要为静态方法写单元测试,可以使用Mockito.mockStatic(Class classToMock)方法,它可以返回一个MockedStatic对象,用于模拟静态方法的调用。
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.18.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
2.单元测试文章来源:https://www.toymoban.com/news/detail-857833.html
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.文章来源地址https://www.toymoban.com/news/detail-857833.html
到了这里,关于静态方法 单元测试 springboot+mokito+junit5的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!