---
author: Jan Brennenstuhl
canonical_url: https://www.janbrennenstuhl.eu/assertj-null-not-blank/
description: 'This AssertJ bug could lead to severe issues: In Java, a blank String is a CharSequence that is empty, null or whitespace only – except when you use AssertJ!'
id: jb-assertj-null-not-blank
kind: essay
license:
  name: CC BY 4.0
  scope: Original text; credited third-party media retain their own rights.
  url: https://creativecommons.org/licenses/by/4.0/
markdown_url: https://www.janbrennenstuhl.eu/assertj-null-not-blank/index.md
modified: '2025-07-06T00:00:00+02:00'
published: '2017-09-05T00:00:00+02:00'
review_basis: publication
review_status: reviewed
reviewed: '2017-09-05'
series: ''
status: published
title: 'Learning AssertJ: Null ain''t Blank'
topics:
- engineering-practice
---

# Learning AssertJ: Null ain't Blank

Popular assertions framework AssertJ surprises Java engineers with handling NULL-values different from what is commonly expected


As programmers, we probably use strings more than any other type of data structure. The cross-language String environment is characterized by shared vocabulary and well-established concepts. And it is especially important to follow these established naming conventions when it comes to [null strings](https://web.archive.org/web/20200809121851/https://josjong.com/2017/10/16/null-vs-empty-strings-why-oracle-was-right-and-apple-is-not/) vs empty strings (and blank strings).

![Toilet rolls representing blank and null states](https://www.janbrennenstuhl.eu/assertj-null-not-blank/toilet-rolls.webp "AssertJ – Blank vs Null Strings")

With [Apache Commons StringUtils](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isBlank-java.lang.CharSequence-) and [Hibernate Validator Constraints](https://docs.jboss.org/hibernate/validator/5.4/api/org/hibernate/validator/constraints/NotBlank.html), two popular and [widely adapted](https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/usages) projects established a de facto definition for a blank string:

> “A blank String is a CharSequence that is empty (""), null or whitespace only.”

While recently investigating a bug in one of our systems, I stumbled upon a slight divergent and unexpected string handling in AssertJ, a fluent assertions framework for Java. This [AssertJ misbehaviour](https://github.com/assertj/assertj/issues/1069) has the potential to lead to severe troubles – which is why I consider it to be a bug. As it turns out …  

```java
String s = null;
assertThat(s).isNotBlank();
```

… passes. This is definitely not expected behaviour, since (following the common definition above) a null string is considered to be a blank string and vice versa. So if you're used to Apache Commons StringUtils and Hibernate Validators behaviour, you want to make sure that also your tests expect the correct behaviour.

Do you know further libraries that follow a different blank string definition or if you have a different opinion about all of this, drop me a message [via Mastodon](https://infosec.exchange/@jbspeakr)!

---

Cite as: Jan Brennenstuhl. "Learning AssertJ: Null ain't Blank." Brennenstuhl on Security, 5 September 2017. https://www.janbrennenstuhl.eu/assertj-null-not-blank/
License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/) for the original text; credited third-party media keep their own rights.
