Home / Class/ BindResultTests Class — spring-boot Architecture

BindResultTests Class — spring-boot Architecture

Architecture documentation for the BindResultTests class in BindResultTests.java from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindResultTests.java lines 43–207

@ExtendWith(MockitoExtension.class)
class BindResultTests {

	@Mock
	@SuppressWarnings("NullAway.Init")
	private Consumer<String> consumer;

	@Mock
	@SuppressWarnings("NullAway.Init")
	private Function<String, String> mapper;

	@Mock
	@SuppressWarnings("NullAway.Init")
	private Supplier<String> supplier;

	@Test
	void getWhenHasValueShouldReturnValue() {
		BindResult<String> result = BindResult.of("foo");
		assertThat(result.get()).isEqualTo("foo");
	}

	@Test
	void getWhenHasNoValueShouldThrowException() {
		BindResult<String> result = BindResult.of(null);
		assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(result::get)
			.withMessageContaining("No value bound");
	}

	@Test
	void isBoundWhenHasValueShouldReturnTrue() {
		BindResult<String> result = BindResult.of("foo");
		assertThat(result.isBound()).isTrue();
	}

	@Test
	void isBoundWhenHasNoValueShouldFalse() {
		BindResult<String> result = BindResult.of(null);
		assertThat(result.isBound()).isFalse();
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void ifBoundWhenConsumerIsNullShouldThrowException() {
		BindResult<String> result = BindResult.of("foo");
		assertThatIllegalArgumentException().isThrownBy(() -> result.ifBound(null))
			.withMessageContaining("'consumer' must not be null");
	}

	@Test
	void ifBoundWhenHasValueShouldCallConsumer() {
		BindResult<String> result = BindResult.of("foo");
		result.ifBound(this.consumer);
		then(this.consumer).should().accept("foo");
	}

	@Test
	void ifBoundWhenHasNoValueShouldNotCallConsumer() {
		BindResult<String> result = BindResult.of(null);
		result.ifBound(this.consumer);
		then(this.consumer).shouldHaveNoInteractions();
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void mapWhenMapperIsNullShouldThrowException() {
		BindResult<String> result = BindResult.of("foo");
		assertThatIllegalArgumentException().isThrownBy(() -> result.map(null))
			.withMessageContaining("'mapper' must not be null");
	}

	@Test
	void mapWhenHasValueShouldCallMapper() {
		BindResult<String> result = BindResult.of("foo");
		given(this.mapper.apply("foo")).willReturn("bar");
		assertThat(result.map(this.mapper).get()).isEqualTo("bar");
	}

	@Test
	void mapWhenHasNoValueShouldNotCallMapper() {
		BindResult<String> result = BindResult.of(null);
		result.map(this.mapper);
		then(this.mapper).shouldHaveNoInteractions();
	}

	@Test
	void orElseWhenHasValueShouldReturnValue() {
		BindResult<String> result = BindResult.of("foo");
		assertThat(result.orElse("bar")).isEqualTo("foo");
	}

	@Test
	void orElseWhenHasValueNoShouldReturnOther() {
		BindResult<String> result = BindResult.of(null);
		assertThat(result.orElse("bar")).isEqualTo("bar");
	}

	@Test
	void orElseGetWhenHasValueShouldReturnValue() {
		BindResult<String> result = BindResult.of("foo");
		assertThat(result.orElseGet(this.supplier)).isEqualTo("foo");
		then(this.supplier).shouldHaveNoInteractions();
	}

	@Test
	void orElseGetWhenHasValueNoShouldReturnOther() {
		BindResult<String> result = BindResult.of(null);
		given(this.supplier.get()).willReturn("bar");
		assertThat(result.orElseGet(this.supplier)).isEqualTo("bar");
	}

	@Test
	void orElseThrowWhenHasValueShouldReturnValue() throws Exception {
		BindResult<String> result = BindResult.of("foo");
		assertThat(result.orElseThrow(IOException::new)).isEqualTo("foo");
	}

	@Test
	void orElseThrowWhenHasNoValueShouldThrowException() {
		BindResult<String> result = BindResult.of(null);
		assertThatIOException().isThrownBy(() -> result.orElseThrow(IOException::new));
	}

	@Test
	void hashCodeAndEquals() {
		BindResult<?> result1 = BindResult.of("foo");
		BindResult<?> result2 = BindResult.of("foo");
		BindResult<?> result3 = BindResult.of("bar");
		BindResult<?> result4 = BindResult.of(null);
		assertThat(result1).hasSameHashCodeAs(result2);
		assertThat(result1).isEqualTo(result1).isEqualTo(result2).isNotEqualTo(result3).isNotEqualTo(result4);
	}

	@Test
	void ofWhenHasValueShouldReturnBoundResultOfValue() {
		BindResult<Object> result = BindResult.of("foo");
		assertThat(result.isBound()).isTrue();
		assertThat(result.get()).isEqualTo("foo");
	}

	@Test
	void ofWhenValueIsNullShouldReturnUnbound() {
		BindResult<Object> result = BindResult.of(null);
		assertThat(result.isBound()).isFalse();
		assertThat(result).isSameAs(BindResult.of(null));
	}

	static class ExampleBean {

		private final String value;

		ExampleBean() {
			this.value = "new";
		}

		ExampleBean(String value) {
			this.value = value;
		}

		String getValue() {
			return this.value;
		}

	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free