BAEL-7128: Optional as a Record Parameter in Java (#15114)

* record class and test

* renaming test
This commit is contained in:
Pedro Lopes 2023-11-03 01:20:47 -03:00 committed by GitHub
parent 4c06d8e9f9
commit 6f618f5cf9
2 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package com.baeldung.optionalsasparameterrecords;
import java.util.Optional;
public record Product(String name, double price, Optional<String> description) {
}

View File

@ -0,0 +1,17 @@
package com.baeldung.optionalsasparameterrecords;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Optional;
public class OptionalAsRecordParameterUnitTest {
@Test
public void givenRecordCreationWithOptional_thenCreateItProperly() {
var emptyDescriptionProduct = new Product("television", 1699.99, Optional.empty());
Assertions.assertEquals("television", emptyDescriptionProduct.name());
Assertions.assertEquals(1699.99, emptyDescriptionProduct.price());
Assertions.assertNull(emptyDescriptionProduct.description().orElse(null));
}
}