这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions testing-modules/mockito-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -46,4 +52,8 @@
</resources>
</build>

<properties>
<awaitility.version>4.2.1</awaitility.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.mockito.delayresponse;

public class PaymentService {

public String processPayment(){
// simulate processing payment and return completion status
return "SUCCESS";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.baeldung.mockito.delayresponse;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import java.util.concurrent.TimeUnit;

import org.awaitility.Awaitility;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.AdditionalAnswers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class PaymentServiceUnitTest {

@Mock
PaymentService paymentService;
private static final long DELAY = 1;

@Test
public void whenProcessingPayment_thenDelayResponseUsingThreadSleep(){
when(paymentService.processPayment()).thenAnswer(invocation -> {
Thread.sleep(DELAY); // Delay of 1 second
return "SUCCESS";
});

long startTime = System.currentTimeMillis();
String result = paymentService.processPayment();
long endTime = System.currentTimeMillis();

assertEquals("SUCCESS", result);
assertTrue((endTime - startTime) >= DELAY); // Verify the delay
}

@Test
public void whenProcessingPayment_thenDelayResponseUsingAnswersWithDelay() {

when(paymentService.processPayment()).thenAnswer(AdditionalAnswers.answersWithDelay(DELAY, invocation -> "SUCCESS"));

long startTime = System.currentTimeMillis();
String result = paymentService.processPayment();
long endTime = System.currentTimeMillis();

assertEquals("SUCCESS", result);
assertTrue((endTime - startTime) >= DELAY); // Verify the delay
}


@Test
public void whenProcessingPayment_thenDelayResponseUsingAwaitility() {

when(paymentService.processPayment()).thenAnswer(invocation -> {
Awaitility.await().pollDelay(DELAY, TimeUnit.MILLISECONDS).until(()->true);
return "SUCCESS";
});

long startTime = System.currentTimeMillis();
String result = paymentService.processPayment();
long endTime = System.currentTimeMillis();

assertEquals("SUCCESS", result);
assertTrue((endTime - startTime) >= DELAY); // Verify the delay
}
}