<dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.11.2</version> <scope>test</scope> </dependency> </dependencies> public class Calculator { public int add(int a, int b) { return a + b; } } import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAddition() { Calculator calculator = mock(Calculator.class); when(calculator.add(2, 3)).thenReturn(5); int result = calculator.add(2, 3); assertEquals(5, result); } } mvn test gradle test


上一篇:
下一篇:
切换中文