Add code examples for Spring Boot nullsafety blog (#137)
* Add code examples for Spring Boot nullsafety blog * Add required files as per checklist * Add review comments * Update build-all.sh Co-authored-by: Tom Hombergs <tom.hombergs@gmail.com>
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package io.reflectoring.nullsafety;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package io.reflectoring.nullsafety;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
class Employee {
|
||||
|
||||
@NonNull
|
||||
String id;
|
||||
String name;
|
||||
LocalDate joiningDate;
|
||||
@Nullable
|
||||
String pastEmployment;
|
||||
|
||||
// constructor
|
||||
|
||||
public Employee(String id, String name, LocalDate joiningDate) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.joiningDate = joiningDate;
|
||||
}
|
||||
|
||||
// standard getters-setters
|
||||
|
||||
String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
LocalDate getJoiningDate() {
|
||||
return joiningDate;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
String getPastEmployment() {
|
||||
return pastEmployment;
|
||||
}
|
||||
|
||||
void setPastEmployment(@Nullable String pastEmployment) {
|
||||
this.pastEmployment = pastEmployment;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
@NonNullApi
|
||||
package io.reflectoring.nullsafety;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.reflectoring.nullsafety;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package io.reflectoring.nullsafety;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class EmployeeTest {
|
||||
|
||||
@Test
|
||||
void employeeShouldHaveValidDetails() {
|
||||
final Employee employee = new Employee("ID001", "Jane Dia", LocalDate.of(2019, 12, 31));
|
||||
employee.setPastEmployment(null);
|
||||
assertAll(() -> {
|
||||
assertNotNull(employee.getId());
|
||||
assertNotNull(employee.getName());
|
||||
assertNotNull(employee.getJoiningDate());
|
||||
assertNull(employee.getPastEmployment());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user