Test list inclusion relationships with AssertJ

List

Lists

The result is the expected value ... assertThat(actual).xxx(expected)
Same as (in no particular order) containsOnlyElementsOf
containsExactlyInAnyOrderElementsOf
Including all containsAll
All included in isSubsetOf
Including even one containsAnyElementsOf
Does not include any doesNotContainAnyElementsOf

Lists and elements

The result is the expected value ... assertThat(actual).xxx(expected)
include isIn
Not included in isNotIn
Including (all) contains
Does not include (no one) doesNotContain

Sample code

All last asserts fail.

src/test/java/ListTest.java


import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;

public class ListTest {

	@Test
	void equalityTest() {
		assertThat(Arrays.asList(2, 3, 5)).containsOnlyElementsOf(Arrays.asList(3, 5, 2));
		assertThat(Arrays.asList(2, 3, 5)).containsOnlyElementsOf(Arrays.asList(2, 4, 6));
	}

	@Test
	void equalityTest2() {
		assertThat(Arrays.asList(2, 3, 5)).containsExactlyInAnyOrderElementsOf(Arrays.asList(3, 5, 2));
		assertThat(Arrays.asList(2, 3, 5)).containsExactlyInAnyOrderElementsOf(Arrays.asList(2, 4, 6));
	}

	@Test
	void supersetTest() {
		assertThat(Arrays.asList(2, 3, 5)).containsAll(Arrays.asList(3, 5));
		assertThat(Arrays.asList(2, 3, 5)).containsAll(Arrays.asList(3, 5, 2));
		assertThat(Arrays.asList(2, 3, 5)).containsAll(Arrays.asList(1, 2, 3, 4, 5, 6));
	}

	@Test
	void subsetTest() {
		assertThat(Arrays.asList(2, 3, 5)).isSubsetOf(Arrays.asList(1, 2, 3, 4, 5, 6));
		assertThat(Arrays.asList(2, 3, 5)).isSubsetOf(Arrays.asList(3, 5, 2));
		assertThat(Arrays.asList(2, 3, 5)).isSubsetOf(Arrays.asList(3, 5));
	}

	@Test
	void intersectionTest() {
		assertThat(Arrays.asList(2, 3, 5)).containsAnyElementsOf(Arrays.asList(2, 4, 6));
		assertThat(Arrays.asList(2, 3, 5)).containsAnyElementsOf(Arrays.asList(3, 5, 2));
		assertThat(Arrays.asList(2, 3, 5)).containsAnyElementsOf(Arrays.asList(1, 4, 6));
	}

	@Test
	void disjointTest() {
		assertThat(Arrays.asList(2, 3, 5)).doesNotContainAnyElementsOf(Arrays.asList(1, 4, 6));
		assertThat(Arrays.asList(2, 3, 5)).doesNotContainAnyElementsOf(Arrays.asList(2, 4, 6));
	}


	@Test
	void includedTest() {
		assertThat(3).isIn(Arrays.asList(2, 3, 5));
		assertThat(3).isIn(Arrays.asList(2, 4, 6));
	}

	@Test
	void excludedTest() {
		assertThat(3).isNotIn(Arrays.asList(2, 4, 6));
		assertThat(3).isNotIn(Arrays.asList(2, 3, 5));
	}


	@Test
	void includingTest() {
		assertThat(Arrays.asList(2, 3, 5)).contains(3);
		assertThat(Arrays.asList(2, 3, 5)).contains(1, 2, 4, 5, 6);
	}

	@Test
	void excludingTest() {
		assertThat(Arrays.asList(2, 3, 5)).doesNotContain(4);
		assertThat(Arrays.asList(2, 3, 5)).doesNotContain(2, 4, 6);
	}
}

version

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>test</artifactId>
	<version>1.0-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>5.3.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.3.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.assertj</groupId>
			<artifactId>assertj-core</artifactId>
			<version>3.11.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.1</version>
			</plugin>
		</plugins>
	</build>
</project>

Recommended Posts

Test list inclusion relationships with AssertJ
Integration Test with Gradle
Engineer-like qualification test list ①
[Rails] Test with RSpec
Test Nokogiri with Rspec.
Automatically test with Gauge
Unit test with Junit.
Test Web API with junit
Test Active Strage with RSpec
Remote debugging with Gradle test
Test GraphQL resolver with rspec