Home

notNullValue

notNullValue is a matcher in the Hamcrest library used for unit testing in Java. It represents the concept of a value that is not null and is commonly used in assertions to ensure that a given expression produces a non-null result.

There are two forms of the matcher. notNullValue() matches any non-null value, while notNullValue(Class<T> type) matches

Typical usage involves static imports from Hamcrest’s core matchers and assertion utilities, for example:

import static org.hamcrest.CoreMatchers.notNullValue;

import static org.hamcrest.MatcherAssert.assertThat;

assertThat(foo, notNullValue());

assertThat(bar, notNullValue(String.class));

In these examples, the assertion passes if foo is not null, and bar is not null and

See also: null value concepts, other Hamcrest matchers such as is, not, and type-safe matchers that help

a
non-null
value
that
is
also
of
the
specified
type.
The
type
variant
helps
enforce
both
non-nullness
and
a
expected
type,
providing
stronger
assertions
in
typed
tests.
is
an
instance
of
String.
notNullValue
is
part
of
Hamcrest’s
core
matchers
and
is
a
basic
tool
for
null-safety
checks.
It
is
often
used
in
combination
with
other
matchers
to
express
more
complex
expectations
about
values,
such
as
ensuring
a
value
is
non-null
before
performing
further
validations.
build
expressive
assertions.