swing-test
swing-test is an idiomatic testing library for Java Swing components, developed in Kotlin
Test components with ease
Write simple assertions for Swing components, using the StringSpec style.
@Test
fun `Should enable Ok button once terms have been read`() {
val form = MyForm()
form.getChild<JButton>("Ok").shouldBeDisabled()
form.clickChild<JCheckBox>("I have read the terms and conditions")
form.getChild<JButton>("Ok").shouldBeEnabled()
}
Easily interact with whole layouts
Use in-built finders to interact with child components without having to expose them directly. Out-the-box support for narrowing by class, text and toolTipText, plus the ability to specify your own lambda for more complex cases:
val myContainer = MyContainer()
myContainer.findChild<JButton>(text = "Cancel").shouldBeNull()
myContainer.getChild<JLabel>(toolTipText = "Avatar").shouldBeVisible()
// Custom example
myContainer.clickChild<JRadioButton> { it.text.contains("foo") }
Simulate common interactions once you have a reference to the component you're after:
val label = myContainer.getChild<JLabel>()
label.doHover() //fires mouseEntered on listeners
label.doHoverAway() //fires mouseExited
label.doubleClick() //simulates mouseClicked/mouseReleased, with clickCount = 2
val table = myContainer.getChild<JTable>()
table.simulateKeyPress(KeyEvent.VK_ENTER) //Simulate the enter key being pressed
Snapshot Testing
📸
swing-test provides a simple one-line approach for verifying that components match a generated png snapshot file. This is particularly useful for testing components with custom painting logic, which can otherwise be hard to verify:
@Test
fun `Should match snapshot - locked`() {
val achievement = AchievementMedal(AchievementStatus.LOCKED)
achievement.shouldMatchImage("locked")
}
@Test
fun `Should match snapshot - red`() {
val achievement = AchievementMedal(AchievementStatus.RED)
medal.shouldMatchImage("red")
}
Snapshot images are automatically written to src/test/resources/__snapshots__/your/package/structure/test-class/imageName.png, for example:
- Running with the system property
updateSnapshots=trueallows the image files to be created for the first time, or updated locally in the event of a deliberate change. - When a snapshot comparison fails, the failed image file is written out with the same name and a
failed.pngextension, to allow easy manual inspection. - Due to pixel differences caused by running on different operating systems, you may optionally specify a
screenshotOssystem property, e.g.screenshotOs=linux. This will cause any screenshot tests to be skipped when run on a different operating system.
Fully interoperable with Java
Although swing-test is developed with Kotlin in mind, it fully supports raw Java projects too:
Component myComponent = new CustomComponent();
SwingSnapshotsKt.shouldMatchImage(myComponent,"Default");
List<JButton> buttons=ComponentFindersKt.findAll(myComponent,JButton.class);
ComponentInteractionsKt.doHover(myComponent);
