Skip navigation links

Package org.assertj.swing.fixture

Public API, source of AssertJ-Swing's power and flexibility.

See: Description

Package org.assertj.swing.fixture Description

Public API, source of AssertJ-Swing's power and flexibility. Although you can use the BasicRobot directly, it is too low-level and requires, in our opinion, too much code. AssertJ-Swing fixtures can simplify creation and maintenance of functional GUI tests by:

  1. providing reliable lookup of GUI components (by component name or using custom criteria)
  2. simulating user events on GUI components
  3. providing assertion methods about the state of GUI components

The following example shows how easy is to use AssertJ-Swing fixtures. The test verifies that an error message is displayed if the user enters her username but forgets to enter her password.

   private FrameFixture window;

   @Before
   public void setUp() {
     window = new FrameFixture(new LoginWindow());
     window.show();
   }

   @After
   public void tearDown() {
     window.cleanUp();
   }

   @Test
   public void shouldCopyTextInLabelWhenClickingButton() {
     window.textBox("username").enterText("some.user");
     window.button("login").click();
     window.optionPane().requireErrorMessage().requireMessage("Please enter your password");
   }
 

The test uses a FrameFixture to launch the GUI to test (LoginWindow) and find the GUI components in such window. This is the recommended way to use AssertJ-Swing. We could also use individual fixtures to simulate user events, but it would result in more code to write and maintain:

   private BasicRobot robot;

   @Before
   public void setUp() {
     robot = BasicRobot.robotWithNewAwtHierarchy();
     robot.showWindow(new LoginWindow());
   }

   @After
   public void tearDown() {
     robot.cleanUp();
   }

   @Test
   public void shouldCopyTextInLabelWhenClickingButton() {
     new JTextComponentFixture(robot, "username").enterText("some.user");
     new JButtonFixture(robot, "login").click();
     new JOptionPaneFixture(robot).requireErrorMessage().requireMessage("Please enter your password");
   }
 

Note: It is very important to clean up resources used by AssertJ-Swing (keyboard, mouse and opened windows) after each test; otherwise, the AssertJ-Swing robot will keep control of them and can make your computer pretty much unusable. To clean up resources call the method 'cleanUp' from BasicRobot, FrameFixture or DialogFixture.

Each fixture has the name of the GUI component it can control plus the word "Fixture" at the end. For example, JButtonFixture can simulate user events on JButtons.

Author:
Alex Ruiz
Skip navigation links

Copyright © 2014–2019 AssertJ. All rights reserved.