[JAVA] Tester les relations d'inclusion de liste avec AssertJ

liste

Listes

Le résultat est la valeur attendue ... assertThat(actual).xxx(expected)
Identique à (sans ordre particulier) containsOnlyElementsOf
containsExactlyInAnyOrderElementsOf
Y compris tous containsAll
Tout inclus dans isSubsetOf
Y compris même un containsAnyElementsOf
N'inclut aucun doesNotContainAnyElementsOf

Listes et éléments

Le résultat est la valeur attendue ... assertThat(actual).xxx(expected)
comprendre isIn
Non inclus dans isNotIn
Y compris (tous) contains
N'inclut pas (personne) doesNotContain

Exemple de code

Toute la dernière affirmation échoue.

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

Tester les relations d'inclusion de liste avec AssertJ
Test d'intégration avec Gradle
Liste de tests de qualification de type ingénieur ①
[Rails] Test avec RSpec
Testez Nokogiri avec Rspec.
Tester automatiquement avec la jauge
Faites un test unitaire avec Junit.
Tester l'API Web avec junit
Tester Active Strage avec RSpec
Tester le résolveur GraphQL avec rspec