CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-google-truth--truth

Fluent assertion framework for Java that provides clear, readable test assertions and informative failure messages

Pending
Overview
Eval results
Files

map-assertions.mddocs/

Map Assertions

Specialized assertions for maps including key-value pair validation, entry containment, and size verification.

Capabilities

Map Subject Creation

/**
 * Creates a MapSubject for asserting about Map instances.
 * @param actual the Map under test
 */
public static MapSubject assertThat(Map<?, ?> actual);

Size and Emptiness Assertions

/**
 * Fails if the map is not empty.
 */
public void isEmpty();

/**
 * Fails if the map is empty.
 */
public void isNotEmpty();

/**
 * Fails if the map does not have the specified size.
 * @param expectedSize the expected number of entries
 */
public void hasSize(int expectedSize);

Usage Examples:

Map<String, Integer> emptyMap = new HashMap<>();
Map<String, Integer> scores = Map.of("Alice", 95, "Bob", 87, "Charlie", 92);

assertThat(emptyMap).isEmpty();
assertThat(scores).isNotEmpty();
assertThat(scores).hasSize(3);

Key Assertions

/**
 * Fails if the map does not contain the specified key.
 * @param key the key that should be present
 */
public void containsKey(Object key);

/**
 * Fails if the map contains the specified key.
 * @param key the key that should not be present
 */
public void doesNotContainKey(Object key);

Usage Examples:

Map<String, String> config = Map.of(
    "database.host", "localhost",
    "database.port", "5432",
    "app.name", "MyApp"
);

assertThat(config).containsKey("database.host");
assertThat(config).containsKey("app.name");
assertThat(config).doesNotContainKey("nonexistent.key");

Entry Assertions

/**
 * Fails if the map does not contain the specified key-value entry.
 * @param key the expected key
 * @param value the expected value for that key
 */
public void containsEntry(Object key, Object value);

/**
 * Fails if the map contains the specified key-value entry.
 * @param key the key to check
 * @param value the value that should not be associated with the key
 */
public void doesNotContainEntry(Object key, Object value);

Usage Examples:

Map<String, Integer> inventory = Map.of(
    "apples", 50,
    "bananas", 30,
    "oranges", 25
);

assertThat(inventory).containsEntry("apples", 50);
assertThat(inventory).containsEntry("bananas", 30);
assertThat(inventory).doesNotContainEntry("apples", 25);
assertThat(inventory).doesNotContainEntry("pears", 10);

Exact Content Assertions

/**
 * Fails if the map is not empty.
 */
public Ordered containsExactly();

/**
 * Fails if the map does not contain exactly the specified entries.
 * Arguments are key-value pairs: containsExactly(key1, value1, key2, value2, ...)
 * @param entries alternating keys and values
 */
public Ordered containsExactly(Object... entries);

/**
 * Fails if the map does not contain exactly the entries in the specified map.
 * @param expectedMap the map containing expected entries
 */
public Ordered containsExactlyEntriesIn(Map<?, ?> expectedMap);

Usage Examples:

Map<String, Integer> empty = new HashMap<>();
Map<String, Integer> grades = Map.of("Math", 95, "Science", 87);
Map<String, Integer> expected = Map.of("Science", 87, "Math", 95);

// Empty map
assertThat(empty).containsExactly();

// Exact entries (order doesn't matter by default)
assertThat(grades).containsExactly("Math", 95, "Science", 87);
assertThat(grades).containsExactly("Science", 87, "Math", 95);
assertThat(grades).containsExactlyEntriesIn(expected);

// With ordering (for ordered maps like LinkedHashMap)
Map<String, Integer> ordered = new LinkedHashMap<>();
ordered.put("first", 1);
ordered.put("second", 2);
assertThat(ordered).containsExactly("first", 1, "second", 2).inOrder();

At Least Assertions

/**
 * Fails if the map does not contain at least the specified entries.
 * Arguments are key-value pairs: containsAtLeast(key1, value1, key2, value2, ...)
 * @param entries alternating keys and values that must be present
 */
public void containsAtLeast(Object... entries);

/**
 * Fails if the map does not contain at least the entries in the specified map.
 * @param expectedEntries the map containing entries that must be present
 */
public void containsAtLeastEntriesIn(Map<?, ?> expectedEntries);

Usage Examples:

Map<String, String> fullConfig = Map.of(
    "host", "localhost",
    "port", "8080", 
    "protocol", "https",
    "timeout", "30000",
    "retries", "3"
);

Map<String, String> minimalConfig = Map.of(
    "host", "localhost",
    "port", "8080"
);

// Must contain at least these entries (can have more)
assertThat(fullConfig).containsAtLeast("host", "localhost", "port", "8080");
assertThat(fullConfig).containsAtLeastEntriesIn(minimalConfig);

Custom Value Comparison

/**
 * Starts a method chain for a check in which the actual values are compared to expected 
 * values using the given Correspondence.
 * @param correspondence the correspondence to use for value comparison
 */
public <A, E> UsingCorrespondence<A, E> comparingValuesUsing(Correspondence<? super A, ? super E> correspondence);

Usage Examples:

import com.google.common.truth.Correspondence;

// Compare string values ignoring case
Correspondence<String, String> CASE_INSENSITIVE = 
    Correspondence.from((actual, expected) -> actual.equalsIgnoreCase(expected), "ignoring case");

Map<String, String> actualSettings = Map.of(
    "MODE", "Production",
    "LEVEL", "Debug"
);

Map<String, String> expectedSettings = Map.of(
    "MODE", "production", 
    "LEVEL", "debug"
);

assertThat(actualSettings)
    .comparingValuesUsing(CASE_INSENSITIVE)
    .containsExactlyEntriesIn(expectedSettings);

// Compare numeric values with tolerance
Correspondence<Double, Double> TOLERANCE = Correspondence.tolerance(0.1);

Map<String, Double> measurements = Map.of("temp", 20.05, "humidity", 45.2);
Map<String, Double> expected = Map.of("temp", 20.0, "humidity", 45.0);

assertThat(measurements)
    .comparingValuesUsing(TOLERANCE)
    .containsAtLeastEntriesIn(expected);

Multimap Assertions

/**
 * Creates a MultimapSubject for asserting about Multimap instances.
 * @param actual the Multimap under test
 */
public static MultimapSubject assertThat(Multimap<?, ?> actual);

Multimap Usage Examples:

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

Multimap<String, String> tags = ArrayListMultimap.create();
tags.put("color", "red");
tags.put("color", "blue");
tags.put("size", "large");

assertThat(tags).isNotEmpty();
assertThat(tags).hasSize(3);  // Total number of key-value pairs
assertThat(tags).containsKey("color");
assertThat(tags).containsEntry("color", "red");
assertThat(tags).containsEntry("color", "blue");

Advanced Map Testing Examples

// Testing configuration maps
Map<String, String> config = loadConfiguration();
assertThat(config).containsKey("database.url");
assertThat(config).containsKey("api.key");
assertThat(config).doesNotContainKey("password"); // Should not store plaintext passwords

// Testing transformation results
Map<String, List<Order>> ordersByCustomer = orders.stream()
    .collect(groupingBy(Order::getCustomerId));

assertThat(ordersByCustomer).isNotEmpty();
assertThat(ordersByCustomer).containsKey("customer123");
assertThat(ordersByCustomer.get("customer123")).hasSize(expectedOrderCount);

// Testing cache contents
Map<String, CachedValue> cache = getCache();
assertThat(cache).hasSize(expectedCacheSize);
assertThat(cache).containsKey(frequentlyAccessedKey);

// Testing builder results
Map<String, Object> result = new MapBuilder()
    .put("name", "John")
    .put("age", 30)
    .put("active", true)
    .build();

assertThat(result).containsExactly(
    "name", "John",
    "age", 30,
    "active", true
);

Types

/**
 * Subject class for making assertions about Map instances.
 */
public class MapSubject extends Subject {
    /**
     * Constructor for MapSubject.
     * @param metadata failure metadata for context
     * @param actual the Map under test
     */
    protected MapSubject(FailureMetadata metadata, Map<?, ?> actual);
}

/**
 * Subject class for making assertions about Multimap instances.
 */
public class MultimapSubject extends Subject {
    /**
     * Constructor for MultimapSubject.
     * @param metadata failure metadata for context
     * @param actual the Multimap under test
     */
    protected MultimapSubject(FailureMetadata metadata, Multimap<?, ?> actual);
}

/**
 * Interface returned by containsExactly methods to enforce ordering constraints.
 */
public interface Ordered {
    /**
     * Enforces that the entries appear in the specified order (for ordered map types).
     */
    void inOrder();
}

/**
 * Result of comparingValuesUsing() providing custom value comparison methods.
 */
public class UsingCorrespondence<A, E> {
    /**
     * Fails if the map does not contain exactly the specified entries using the correspondence.
     * @param entries alternating keys and expected values
     */
    public Ordered containsExactly(Object... entries);
    
    /**
     * Fails if the map does not contain exactly the entries in the given map using the correspondence.
     * @param expectedMap the map containing expected entries
     */
    public Ordered containsExactlyEntriesIn(Map<?, ? extends E> expectedMap);
    
    /**
     * Fails if the map does not contain at least the specified entries using the correspondence.
     * @param entries alternating keys and expected values
     */
    public void containsAtLeast(Object... entries);
    
    /**
     * Fails if the map does not contain at least the entries in the given map using the correspondence.
     * @param expectedEntries the map containing required entries
     */
    public void containsAtLeastEntriesIn(Map<?, ? extends E> expectedEntries);
}

Install with Tessl CLI

npx tessl i tessl/maven-com-google-truth--truth

docs

array-assertions.md

collection-assertions.md

core-assertions.md

custom-assertions.md

exception-assertions.md

index.md

java8-assertions.md

map-assertions.md

numeric-assertions.md

string-assertions.md

testing-utilities.md

tile.json