Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 2.66 KB

assert-api.md

File metadata and controls

27 lines (22 loc) · 2.66 KB

Assertion or Validation Java APIs

📌 Overview of some APIs we can use for assertions.

Production code

API Class Example Exception thrown Message
JDK - assert input != null; java.lang.AssertionError
JDK java.util.Objects Object value = Objects.requireNonNull(input); java.lang.NullPointerException 🚫
JDK java.util.Objects Object value = Objects.requireNonNull(input, "message"); java.lang.NullPointerException message
Spring org.springframework.util.Assert Assert.notNull(input); java.lang.IllegalArgumentException [Assertion failed] - the object argument must be null
Spring org.springframework.util.Assert Assert.notNull(input, "message"); java.lang.IllegalArgumentException message
Micronaut io.micronaut.core.util.ArgumentUtils ArgumentUtils.requireNonNull("argname", input); java.lang.NullPointerException Argument xxx cannot be null
Apache commons-lang3 org.apache.commons.lang3.Validate Object value = Validate.notNull(input); java.lang.NullPointerException The validated object is null
Apache commons-lang3 org.apache.commons.lang3.Validate Object value = Validate.notNull(input, "message"); java.lang.NullPointerException message
Google Guava com.google.common.base.Verify Verify.verifyNotNull(input); com.google.common.base.VerifyException expected a non-null reference

Test code

API Class Example Exception thrown
JUnit 4 junit.framework.Assert Assert.assertNotNull(input); junit.framework.AssertionFailedError
JUnit 5 org.junit.jupiter.api.Assertions Assertions.assertNotNull(input); org.opentest4j.AssertionFailedError
Hamcrest org.hamcrest.MatcherAssert MatcherAssert.asserthat(X, Y); java.lang.AssertionError
AssertJ org.assertj.core.api.Assertions Assertions.asserthat(X, Y); java.lang.AssertionError
TestNG org.testng.Assert Assert.assertNotNull(input); java.lang.AssertionError