Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BAEL-8927] Guide to Objects.requireNonNull() in Java #18099

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.baeldung.requirenonnull;

import java.time.Instant;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class RequireNonNullUnitTest {

private static final Logger logger = LoggerFactory.getLogger(RequireNonNullUnitTest.class);

void greet(String name) {
Objects.requireNonNull(name, "Name cannot be null");
logger.info("Hello, {}!", name);
}

@Test
void givenObject_whenGreet_thenNoException() {
Assertions.assertDoesNotThrow(() -> greet("Baeldung"));
}

@Test
void givenNull_whenGreet_thenException() {
Assertions.assertThrows(NullPointerException.class, () -> greet(null));
}

static class User {

private final String username;
private final String password;

public User(String username, String password) {
this.username = Objects.requireNonNull(username, "Username is null!");
this.password = Objects.requireNonNull(password, "Password is null!");
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}
}

@Test
void givenValidInput_whenNewUser_thenNoException() {
Assertions.assertDoesNotThrow(() -> new User("Baeldung", "Secret"));
}

@Test
void givenNull_whenNewUser_thenException() {
Assertions.assertThrows(NullPointerException.class, () -> new User(null, "Secret"));
Assertions.assertThrows(NullPointerException.class, () -> new User("Baeldung", null));
}

void processOrder(UUID orderId) {
Objects.requireNonNull(orderId, () -> {
String message = "Order ID cannot be null! Current timestamp: " + getProcessTimestamp();
message = message.concat("Total number of invalid orders: " + getOrderAmount());
message = message.concat("Please provide a valid order.");
return message;
});
logger.info("Processing order with id: {}", orderId);
}

private static int getOrderAmount() {
return new Random().nextInt(100_000);
}

private static Instant getProcessTimestamp() {
return Instant.now();
}

@Test
void givenObject_whenProcessOrder_thenNoException() {
Assertions.assertDoesNotThrow(() -> processOrder(UUID.randomUUID()));
}

@Test
void givenNull_whenProcessOrder_thenException() {
Assertions.assertThrows(NullPointerException.class, () -> processOrder(null));
}
}