import dev.nuaforma.image.comparison.kotlin.assertImagesMatch
import java.io.File
import javax.imageio.ImageIO
val expected = requireNotNull(ImageIO.read(File("reference.png")))
val actual = requireNotNull(ImageIO.read(File("actual.png")))
assertImagesMatch(expected, actual, "report preview") {
pixelTolerance = 0.1
allowedDifferentPixelsPercent = 0.0
}
A mismatch throws ImageComparisonAssertionError, a framework-independent AssertionError. Its comparisonResult exposes the result and difference image. Save diagnostic files explicitly in your own test code.
1. Add the Java core
The Java core has no external runtime dependencies and requires Java 17 or later.
import dev.nuaforma.image.comparison.ImageComparison;
import dev.nuaforma.image.comparison.model.ImageComparisonResult;
import dev.nuaforma.image.comparison.model.ImageComparisonState;
import java.io.File;
import javax.imageio.ImageIO;
var expected = ImageIO.read(new File("reference.png"));
var actual = ImageIO.read(new File("actual.png"));
ImageComparisonResult result = new ImageComparison(expected, actual)
.setPixelToleranceLevel(0.1)
.compareImages();
System.out.println(result.getImageComparisonState());
System.out.println(result.getDifferencePercent());
if (result.getImageComparisonState() != ImageComparisonState.MATCH) {
throw new AssertionError("Unexpected visual difference: "
+ result.getDifferencePercent() + "%");
}
The Java API returns a result. This explicit state check turns both MISMATCH and SIZE_MISMATCH into a failing test without requiring a particular test framework or the JVM's -ea flag.
3. Make the policy explicit
Tolerance and allowance
pixelTolerance describes how much a pixel's color or alpha may differ. allowedDifferentPixelsPercent describes how many non-excluded pixels may exceed that tolerance. They are different controls. Start with a deliberate policy; do not keep raising either value to hide a regression.
Known variable regions
Use excludeRectangle(x1, y1, x2, y2) for an intentional exclusion. Coordinates are inclusive and must fit inside the reference image. The excluded pixels leave both the numerator and denominator. Different image dimensions always fail the Kotlin assertion.
The library compares images. Render PDFs or capture screenshots separately, use fixed inputs and review golden images before accepting a new baseline. Bound image dimensions and concurrent work in your own application.