# Chain AssertJ AssertThat Statements

# Description

AssertJ encourages writing fluent test cases by chaining the assertions that target the same object instead of invoking assertThat (opens new window) multiple times. This rule replaces consecutive AssertJ assertThat invocations targeting the same object with an assertion chain. Thus, eliminating some redundant code and increasing the readability of test cases.

# Benefits

Improves the readability of unit tests.

# Tags

# Code Changes

# Assertions on a String

Pre

String helloWorld = "Hello World!";
assertThat(helloWorld).isNotNull();
assertThat(helloWorld).isNotEmpty();
assertThat(helloWorld).startsWith("Hello ");
assertThat(helloWorld).contains(" ");
assertThat(helloWorld).contains("World");
assertThat(helloWorld).endsWith("!");
assertThat(helloWorld).doesNotStartWith("World");
assertThat(helloWorld).doesNotContain("--");
assertThat(helloWorld).doesNotEndWith("Hello");

Post

String helloWorld = "Hello World!";
assertThat(helloWorld)
	.isNotNull()
	.isNotEmpty()
	.startsWith("Hello ")
	.contains(" ")
	.contains("World")
	.endsWith("!")
	.doesNotStartWith("World")
	.doesNotContain("--")
	.doesNotEndWith("Hello");

# Assertions on a Primitive Int Value

Pre

int intValue = 10;
assertThat(intValue).isPositive();
assertThat(intValue).isEven();
assertThat(intValue).isNotEqualTo(-10);
assertThat(intValue).isGreaterThan(9);
assertThat(intValue).isEqualTo(10);
assertThat(intValue).isLessThan(11);		

Post

int intValue = 10;
assertThat(intValue)
	.isPositive()
	.isEven()
	.isNotEqualTo(-10)
	.isGreaterThan(9)
	.isEqualTo(10)
	.isLessThan(11);		

# Assertions on a String List

Pre

List<String> stringList = Arrays.asList("String-1", "String-2");
assertThat(stringList).isNotNull();
assertThat(stringList).hasSize(2);
assertThat(stringList).matches(s -> !s.isEmpty());
assertThat(stringList).contains("String-1", atIndex(0));
assertThat(stringList).contains("String-2", atIndex(1));

Post

List<String> stringList = Arrays.asList("String-1", "String-2");
assertThat(stringList)
	.isNotNull()
	.hasSize(2)
	.matches(s -> !s.isEmpty())
	.contains("String-1", atIndex(0))
	.contains("String-2", atIndex(1));

# Assertions on a File

Pre

File file = new File("pom.xml");
assertThat(file).isNotNull();
assertThat(file).exists();
assertThat(file).satisfies(File::isFile);
assertThat(file).hasFileName("pom.xml");
assertThat(file).canRead();

Post

File file = new File("pom.xml");
assertThat(file)
	.isNotNull()
	.exists()
	.satisfies(File::isFile)
	.hasFileName("pom.xml")
	.canRead();

# Assertions on String.class

Pre

assertThat(String.class).isFinal();
assertThat(String.class).isNotSameAs(Object.class);
assertThat(String.class).isNotInterface();
assertThat(String.class).hasMethods("startsWith", "endsWith");
assertThat(String.class).satisfies(new Condition<>(
		clazz -> !Modifier.isAbstract(clazz.getModifiers()), ""));

Post

assertThat(String.class)
	.isFinal()
	.isNotSameAs(Object.class)
	.isNotInterface()
	.hasMethods("startsWith", "endsWith")
	.satisfies(new Condition<>(
		clazz -> !Modifier.isAbstract(clazz.getModifiers()), ""));

# Assertions on a Predicate

Pre

Predicate<String> stringPredicate = s -> s.contains("-");
assertThat(stringPredicate).isNotNull();
assertThat(stringPredicate).rejects("a", "b");
assertThat(stringPredicate).rejectsAll(Arrays.asList("c", "d", "e"));
assertThat(stringPredicate).accepts("s-1", "s-2");
assertThat(stringPredicate).acceptsAll(Arrays.asList("s-3", "s-4", "s-5"));

Post

Predicate<String> stringPredicate = s -> s.contains("-");
assertThat(stringPredicate)
	.isNotNull()
	.rejects("a", "b")
	.rejectsAll(Arrays.asList("c", "d", "e"))
	.accepts("s-1", "s-2")
	.acceptsAll(Arrays.asList("s-3", "s-4", "s-5"));

# assertThatThrownBy on a ThrowingCallable

Pre

ThrowingCallable throwingCallable = () -> {
	throw new Exception("Unexpected exception!");
};
assertThatThrownBy(throwingCallable).isInstanceOf(Exception.class);
assertThatThrownBy(throwingCallable).hasMessageStartingWith("Unexpected");
assertThatThrownBy(throwingCallable).hasMessageContaining(" ", "exception");
assertThatThrownBy(throwingCallable).hasMessageEndingWith("!");

Post

ThrowingCallable throwingCallable = () -> {
	throw new Exception("Unexpected exception!");
};
assertThatThrownBy(throwingCallable)
	.isInstanceOf(Exception.class)
	.hasMessageStartingWith("Unexpected")
	.hasMessageContaining(" ", "exception")
	.hasMessageEndingWith("!");

🛠️ Auto-refactor Available

You can auto refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID ChainAssertJAssertThatStatements
First seen in jSparrow version 4.6.0
Minimum Java version 7
Remediation cost 5 min
Links