BAEL-5852: What does the Holder<T> do in Java?
This commit is contained in:
parent
a4d39d14dd
commit
fa06d77df8
@ -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…
x
Reference in New Issue
Block a user