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:
Saikat Sengupta
2022-02-01 01:13:37 +05:30
committed by GitHub
parent bbb40758ff
commit 9d6f34dd98
14 changed files with 702 additions and 1 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,4 @@
@NonNullApi
package io.reflectoring.nullsafety;
import org.springframework.lang.NonNullApi;

View File

@@ -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() {
}
}

View File

@@ -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());
});
}
}