BAEL-5852: What does the Holder<T> do in Java? (#14477)
Co-authored-by: Vladyslav Chernov <vlad.chrv@gmail.com>
This commit is contained in:
parent
4b99565de3
commit
7ab8374bd9
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.holder;
|
||||
|
||||
public class Holder<T> {
|
||||
public T value;
|
||||
|
||||
public Holder(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.holder;
|
||||
|
||||
public class SupplierService {
|
||||
public void getSupplierByZipCode(String zip, Holder<Boolean> resultHolder) {
|
||||
// Let's pretend we did some work here to get the supplier
|
||||
// And let's say all zip codes starting with "9" are valid, just for this example
|
||||
if (zip.startsWith("9")) {
|
||||
resultHolder.value = true;
|
||||
} else {
|
||||
resultHolder.value = false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.holder;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SupplierServiceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidZipCode_whenGetSupplierByZipCode_thenTrue() {
|
||||
SupplierService service = new SupplierService();
|
||||
Holder<Boolean> resultHolder = new Holder<>(false);
|
||||
String zipCode = "98682";
|
||||
|
||||
service.getSupplierByZipCode(zipCode, resultHolder);
|
||||
|
||||
assertTrue(resultHolder.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidZipCode_whenGetSupplierByZipCode_thenFalse() {
|
||||
SupplierService service = new SupplierService();
|
||||
Holder<Boolean> resultHolder = new Holder<>(true);
|
||||
String zipCode = "12345";
|
||||
|
||||
service.getSupplierByZipCode(zipCode, resultHolder);
|
||||
|
||||
assertFalse(resultHolder.value);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue