Record vs Final class in Java (#13455)
* Added code for record vs final. * Fixed a small mistake.
This commit is contained in:
parent
7a65d453ed
commit
e48229dc25
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.java14.recordvsfinal;
|
||||||
|
|
||||||
|
public record USCitizen(String firstName, String lastName, String address) {
|
||||||
|
static int countryCode;
|
||||||
|
|
||||||
|
// static initializer
|
||||||
|
static {
|
||||||
|
countryCode = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getCountryCode() {
|
||||||
|
return countryCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFullName() {
|
||||||
|
return firstName + " " + lastName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.java14.recordvsfinal;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class USCitizenUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenName_whenGetNameAndCode_thenExpectedValuesReturned() {
|
||||||
|
|
||||||
|
String firstName = "Joan";
|
||||||
|
String lastName = "Winn";
|
||||||
|
String address = "1892 Black Stallion Road";
|
||||||
|
int countryCode = 1;
|
||||||
|
|
||||||
|
USCitizen citizen = new USCitizen(firstName, lastName, address);
|
||||||
|
|
||||||
|
assertEquals(firstName + " " + lastName, citizen.getFullName());
|
||||||
|
assertEquals(countryCode, USCitizen.getCountryCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user