Skip to content

Commit

Permalink
Apply reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Mar 22, 2023
1 parent 0602b39 commit e11b3bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,7 @@ interface ContextView {
* @param defaultObjectSupplier supplier for default object to return
* @param <T> value type
* @return object or default if not present
* @since 1.11.0
*/
default <T> T getOrDefault(Object key, Supplier<T> defaultObjectSupplier) {
T value = get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

/**
* Tests for {@link Observation.Context}.
Expand Down Expand Up @@ -68,6 +71,30 @@ void getOrDefaultShouldUseFallbackValue() {
assertThat(context.getOrDefault(Integer.class, 123)).isEqualTo(123);
}

@Test
void getOrDefaultSupplierWhenKeyIsPresent() {
context.put(String.class, "42");

@SuppressWarnings("unchecked")
Supplier<String> defaultSupplier = mock(Supplier.class);
when(defaultSupplier.get()).thenReturn("abc");

assertThat(context.getOrDefault(String.class, defaultSupplier)).isEqualTo("42");
verifyNoInteractions(defaultSupplier);
}

@Test
void getOrDefaultSupplierWhenKeyIsMissing() {
context.put(String.class, "42");

@SuppressWarnings("unchecked")
Supplier<Integer> defaultSupplier = mock(Supplier.class);
when(defaultSupplier.get()).thenReturn(123);

assertThat(context.getOrDefault(Integer.class, defaultSupplier)).isEqualTo(123);
verify(defaultSupplier, times(1)).get();
}

@Test
void getRequiredShouldFailIfThereIsNoValue() {
context.put(String.class, "42");
Expand Down

0 comments on commit e11b3bd

Please sign in to comment.