BAEL 5932 - Callback Functions in Java (#13364)

This commit is contained in:
Olu 2023-02-01 08:04:40 +01:00 committed by GitHub
parent 226fa47606
commit c919c5e70c
2 changed files with 13 additions and 7 deletions

View File

@ -6,8 +6,7 @@ import com.baeldung.callbackfunctions.EventListener;
import com.baeldung.callbackfunctions.asynchronous.AsynchronousEventConsumer; import com.baeldung.callbackfunctions.asynchronous.AsynchronousEventConsumer;
import com.baeldung.callbackfunctions.asynchronous.AsynchronousEventListenerImpl; import com.baeldung.callbackfunctions.asynchronous.AsynchronousEventListenerImpl;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;
public class AsynchronousCallbackUnitTest { public class AsynchronousCallbackUnitTest {
@ -17,6 +16,6 @@ public class AsynchronousCallbackUnitTest {
AsynchronousEventConsumer asynchronousEventListenerConsumer = new AsynchronousEventConsumer(listener); AsynchronousEventConsumer asynchronousEventListenerConsumer = new AsynchronousEventConsumer(listener);
asynchronousEventListenerConsumer.doAsynchronousOperation(); asynchronousEventListenerConsumer.doAsynchronousOperation();
verify(listener, times(1)).onTrigger(); verify(listener, timeout(1000).times(1)).onTrigger();
} }
} }

View File

@ -3,18 +3,25 @@ package com.baeldung.callbackfunctions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.baeldung.callbackfunctions.ConsumerCallback; import com.baeldung.callbackfunctions.ConsumerCallback;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConsumerCallbackUnitTest { public class ConsumerCallbackUnitTest {
@Test @Test
public void whenIncreasingInitialAgeByGivenValueThroughCallback_shouldIncreaseAge(){ void whenIncreasingInitialAgeByGivenValueThroughCallback_shouldIncreaseAge(){
ConsumerCallback consumerCallback = new ConsumerCallback(); ConsumerCallback consumerCallback = new ConsumerCallback();
consumerCallback.getAge(20, (initialAge) -> {
int ageDifference = 10; int ageDifference = 10;
AtomicInteger newAge1 = new AtomicInteger();
int initialAge = 20;
consumerCallback.getAge(initialAge, (initialAge1) -> {
consumerCallback.increaseAge(initialAge, ageDifference, (newAge) -> { consumerCallback.increaseAge(initialAge, ageDifference, (newAge) -> {
assertEquals(initialAge + ageDifference, newAge); System.out.printf("New age ==> %s", newAge);
newAge1.set(newAge);
}); });
}); });
assertEquals(initialAge + ageDifference, newAge1.get());
} }
} }