📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
When to use assertNotNull() method or assertion
void org.junit.Assert.assertNotNull(Object object)
- object - Object to check or null
Assert.assertNotNull(Object object) Method Example
import static org.junit.Assert.assertNotNull;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import com.javaguides.strings.StringUtility;
public class AssertNotNullExample {
public static String[] toStringArray(final Collection<?> collection) {
if (collection == null) {
return null;
}
return collection.toArray(new String[collection.size()]);
}
@Test
public void toStringArrayTest() {
final String[] strArray = StringUtility.toStringArray(Arrays.asList("a", "b", "c"));
for (final String element : strArray) {
assertNotNull(element);
}
}
}
Output:
Related JUnit Examples
- JUnit Assert.assertArrayEquals() Method Example
- JUnit Assert.assertEquals() Method Example
- JUnit Assert.assertTrue() Method Example
- JUnit Assert.assertFalse() Method Example
- JUnit Assert.assertNull() Method Example
- JUnit Assert.assertNotNull() Method Example
- JUnit Assert.assertSame() Method Example
- JUnit Assert.assertNotSame() Method Example
- JUnit Assert.fail() Method Example
Comments
Post a Comment
Leave Comment