swing-test

Kotlin swing testing library

License

License

MIT
GroupId

GroupId

com.github.alexburlton
ArtifactId

ArtifactId

swing-test
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

swing-test
Kotlin swing testing library
Project URL

Project URL

https://github.com/alexburlton/swing-test
Source Code Management

Source Code Management

https://github.com/alexburlton/swing-test

Download swing-test

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.alexburlton/swing-test/ -->
<dependency>
    <groupId>com.github.alexburlton</groupId>
    <artifactId>swing-test</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.alexburlton/swing-test/
implementation 'com.github.alexburlton:swing-test:1.0.0'
// https://jarcasting.com/artifacts/com.github.alexburlton/swing-test/
implementation ("com.github.alexburlton:swing-test:1.0.0")
'com.github.alexburlton:swing-test:jar:1.0.0'
<dependency org="com.github.alexburlton" name="swing-test" rev="1.0.0">
  <artifact name="swing-test" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.alexburlton', module='swing-test', version='1.0.0')
)
libraryDependencies += "com.github.alexburlton" % "swing-test" % "1.0.0"
[com.github.alexburlton/swing-test "1.0.0"]

Dependencies

runtime (3)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.3.50
io.kotlintest : kotlintest-assertions jar 3.4.2
org.junit.jupiter : junit-jupiter-api jar 5.1.0

test (2)

Group / Artifact Type Version
io.mockk : mockk jar 1.9.3
org.mockito : mockito-core jar 3.3.3

Project Modules

There are no modules declared in this project.

swing-test

Build Status License: MIT

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:

image

  • Running with the system property updateSnapshots=true allows 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.png extension, to allow easy manual inspection.
  • Due to pixel differences caused by running on different operating systems, you may optionally specify a screenshotOs system 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);

Versions

Version
1.0.0