<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> groovy testImplementation 'junit:junit:4.13' import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; public class CalculatorTest { private Calculator calculator; @Before public void setUp() { calculator = new Calculator(); } @Test public void testAdd() { int result = calculator.add(2, 3); assertEquals(5, result); } @Test public void testSubtract() { int result = calculator.subtract(5, 3); assertEquals(2, result); } @Test public void testMultiply() { int result = calculator.multiply(2, 3); assertEquals(6, result); } } <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.12.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>2.2</version> <scope>test</scope> </dependency> groovy testImplementation 'org.mockito:mockito-core:3.12.4' testImplementation 'org.hamcrest:hamcrest-all:2.2' import org.junit.Test; import static org.mockito.Mockito.*; public class AppIntegrationTest { @Test public void testSendEmailAndLog() { EmailSender emailSender = mock(EmailSender.class); Logger logger = mock(Logger.class); App app = new App(emailSender, logger); app.sendEmailAndLog("recipient@example.com", "Hello, World!"); verify(emailSender, times(1)).sendEmail(eq("recipient@example.com"), eq("Hello, World!")); verify(logger, times(1)).log(eq("Email sent: recipient@example.com")); } }


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