Why does JPanelFixture.comboBox().pressAndReleaseKeys() work with FEST, but not with AssertJ?Does a finally block always get executed in Java?How does the Java 'for each' loop work?What is a serialVersionUID and why should I use it?Why does Java have transient fields?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why does this code using random strings print “hello world”?Why is printing “B” dramatically slower than printing “#”?
Examples of subgroups where it's nontrivial to show closure under multiplication?
If a warlock with the Repelling Blast invocation casts Eldritch Blast and hits, must the targets always be pushed back?
How to get a plain text file version of a CP/M .BAS (M-BASIC) program?
Is the 5 MB static resource size limit 5,242,880 bytes or 5,000,000 bytes?
What is the difference between `command a[bc]d` and `command `ab,cd`
Is there any limitation with Arduino Nano serial communication distance?
What does the "ep" capability mean?
Reducing vertical space in stackrel
What does KSP mean?
What language was spoken in East Asia before Proto-Turkic?
Is there really no use for MD5 anymore?
Why do Computer Science majors learn Calculus?
Does Gita support doctrine of eternal samsara?
How to reduce LED flash rate (frequency)
What's the polite way to say "I need to urinate"?
How can the Zone of Truth spell be defeated without the caster knowing?
Rivers without rain
Don’t seats that recline flat defeat the purpose of having seatbelts?
Can someone publish a story that happened to you?
Combinable filters
how to sum variables from file in bash
French for 'It must be my imagination'?
Unexpected email from Yorkshire Bank
What are the potential pitfalls when using metals as a currency?
Why does JPanelFixture.comboBox().pressAndReleaseKeys() work with FEST, but not with AssertJ?
Does a finally block always get executed in Java?How does the Java 'for each' loop work?What is a serialVersionUID and why should I use it?Why does Java have transient fields?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why does this code using random strings print “hello world”?Why is printing “B” dramatically slower than printing “#”?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
When trying to simulate input using AssertJ's pressAndReleaseKeys() for unit testing a JComboBox in a Java Swing program, I am not seeing the expected behavior. The program will most often hang on the pressAndReleaseKeys line and then fail, or occasionally will delete all the text currently in the JComboBox being tested, causing later assertions to fail (i.e. requireSelection()). The stack trace I receive for the provided example program (see below) when it hangs is as follows:
Focus change to javax.swing.JComboBox[name='combob', selectedItem='Bean', contents=["Pork", "Beans", "Rice"], editable=true, enabled=true, visible=true, showing=true] failed focus owner: javax.swing.plaf.metal.MetalComboBoxEditor$1(javax.swing.JTextField)[name=null, text='Bean', enabled=true, visible=true, showing=true]
org.assertj.swing.exception.ActionFailedException
at org.assertj.swing.exception.ActionFailedException.actionFailure(ActionFailedException.java:33)
at org.assertj.swing.core.BasicRobot.focus(BasicRobot.java:301)
at org.assertj.swing.core.BasicRobot.focusAndWaitForFocusGain(BasicRobot.java:270)
at org.assertj.swing.driver.ComponentDriver.focusAndWaitForFocusGain(ComponentDriver.java:419)
at org.assertj.swing.driver.ComponentDriver.pressAndReleaseKeys(ComponentDriver.java:315)
at org.assertj.swing.fixture.AbstractComponentFixture.pressAndReleaseKeys(AbstractComponentFixture.java:293)
at javapractice.ComboBoxSampleTest.testMain(ComboBoxSampleTest.java:59)
I have been using FEST and am hoping to migrate my tests to AssertJ since it is being actively maintained, whereas FEST hasn't been updated for years. I used Joel Costigliola's migration from Fest to AssertJ guide, but am having trouble when simulating keyboard input by using pressAndReleaseKeys(). I am able to simulate input when using a JTextComponentFixture i.e.
window.textBox("textB").pressAndReleaseKeys(KeyEvent.VK_LEFT);
(where window is a FrameFixture, a container in both AssertJ and FEST), but I am unable to simulate input when using a JComboBoxFixture i.e.
window.comboBox("comboB").pressAndReleaseKeys(KeyEvent.VK_LEFT);
This obstacle can usually be avoided, since most "key presses" can be simulated by using enterText i.e.
window.comboBox("comboB").enterText("n"); //to press the enter key
window.comboBox("comboB").enterText("b"); //to press the backspace key
but I would like to be able to use the arrow keys, control key, and other keys where I can't simulate the key press using enterText(). Is this failure due to an issue with my environment*, an issue with the way I'm using it, or is the API itself flawed?
I tried using pressKey() and then releaseKey() as a workaround, but that doesn't work with JComboBox either, and my program instead hangs on pressKey(). That being said, I am not able to use pressKey() and releaseKey() to test a JComboBox with FEST either.
*Environment details:
Language version: java version "1.8.0_131"
Platform version (e.g. .NET 3.5; note that this isn’t always implicit from the language version, or vice versa)
Operating system: Red Hat Release 6.10 (Santiago)
IDE: Netbeans 8.0.2
Sample GUI application:
package javapractice;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ComboBoxSample extends JFrame implements ItemListener
JPanel jp;
JComboBox jcb;
JLabel result;
JLabel title;
JTextField jtc;
public static void main(String[] args)
ComboBoxSample frame = new ComboBoxSample();
ComboBoxSample()
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("Testing AssertJ");
this.setLayout(new FlowLayout());
jp = new JPanel();
jcb = new JComboBox(new String[] "Pork", "Beans", "Rice");
jcb.setEditable(true);
jcb.setName("combob");
jtc = new JTextField();
jtc.setEditable(true);
jtc.setPreferredSize(new Dimension(150, 25));
jtc.setName("textb");
title = new JLabel("Food: ");
result = new JLabel("No food");
jp.add(title);
jp.add(jcb);
jp.add(result);
jp.add(jtc);
this.add(jp);
this.setLocationRelativeTo(null);
jcb.addItemListener(this);
this.pack();
this.repaint();
@Override
public void itemStateChanged(ItemEvent e)
if(e.getSource() == jcb)
result.setText("I'm eating " + jcb.getSelectedItem());
this.pack();
public void cleanUp()
jcb = null;
result = null;
jtc = null;
jp = null;
title = null;
Test File for Fest:
package javapractice;
import com.sun.glass.events.KeyEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Fest imports.
*/
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.fixture.FrameFixture;
public class ComboBoxSampleTest
private FrameFixture window;
private ComboBoxSample frame;
@BeforeClass
public static void setUpClass()
FailOnThreadViolationRepaintManager.install();
@AfterClass
public static void tearDownClass()
@Before
public void setUp()
frame = GuiActionRunner.execute(new GuiQuery<ComboBoxSample>()
@Override
protected ComboBoxSample executeInEDT()
return new ComboBoxSample();
);
window = new FrameFixture(frame);
window.show();
@After
public void tearDown()
window.cleanUp();
frame.cleanUp();
/**
* Test of main method, of class ComboBoxSample.
*/
@Test
public void testMain()
//Delay so that we can see what's going on
try
Thread.sleep(2000);
catch (InterruptedException ie)
window.textBox("textb").enterText("hi there");
window.textBox("textb").pressAndReleaseKeys(KeyEvent.VK_BACKSPACE);
window.comboBox().replaceText("Bean");
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_ENTER);
Test File for AssertJ:
package javapractice;
import com.sun.glass.events.KeyEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* AssertJ imports.
*/
import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
public class ComboBoxSampleTest
private FrameFixture window;
private ComboBoxSample frame;
@BeforeClass
public static void setUpClass()
FailOnThreadViolationRepaintManager.install();
@AfterClass
public static void tearDownClass()
@Before
public void setUp()
frame = GuiActionRunner.execute(() -> new ComboBoxSample());
window = new FrameFixture(frame);
window.show();
@After
public void tearDown()
window.cleanUp();
frame.cleanUp();
/**
* Test of main method, of class ComboBoxSample.
*/
@Test
public void testMain()
//Delay so that we can see what's going on
try
Thread.sleep(2000);
catch (InterruptedException ie)
window.textBox("textb").enterText("hi there");
window.textBox("textb").pressAndReleaseKeys(KeyEvent.VK_BACKSPACE);
window.comboBox().replaceText("Bean");
//the above line is the last one to execute
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_ENTER);
java swing junit assertj fest
add a comment |
When trying to simulate input using AssertJ's pressAndReleaseKeys() for unit testing a JComboBox in a Java Swing program, I am not seeing the expected behavior. The program will most often hang on the pressAndReleaseKeys line and then fail, or occasionally will delete all the text currently in the JComboBox being tested, causing later assertions to fail (i.e. requireSelection()). The stack trace I receive for the provided example program (see below) when it hangs is as follows:
Focus change to javax.swing.JComboBox[name='combob', selectedItem='Bean', contents=["Pork", "Beans", "Rice"], editable=true, enabled=true, visible=true, showing=true] failed focus owner: javax.swing.plaf.metal.MetalComboBoxEditor$1(javax.swing.JTextField)[name=null, text='Bean', enabled=true, visible=true, showing=true]
org.assertj.swing.exception.ActionFailedException
at org.assertj.swing.exception.ActionFailedException.actionFailure(ActionFailedException.java:33)
at org.assertj.swing.core.BasicRobot.focus(BasicRobot.java:301)
at org.assertj.swing.core.BasicRobot.focusAndWaitForFocusGain(BasicRobot.java:270)
at org.assertj.swing.driver.ComponentDriver.focusAndWaitForFocusGain(ComponentDriver.java:419)
at org.assertj.swing.driver.ComponentDriver.pressAndReleaseKeys(ComponentDriver.java:315)
at org.assertj.swing.fixture.AbstractComponentFixture.pressAndReleaseKeys(AbstractComponentFixture.java:293)
at javapractice.ComboBoxSampleTest.testMain(ComboBoxSampleTest.java:59)
I have been using FEST and am hoping to migrate my tests to AssertJ since it is being actively maintained, whereas FEST hasn't been updated for years. I used Joel Costigliola's migration from Fest to AssertJ guide, but am having trouble when simulating keyboard input by using pressAndReleaseKeys(). I am able to simulate input when using a JTextComponentFixture i.e.
window.textBox("textB").pressAndReleaseKeys(KeyEvent.VK_LEFT);
(where window is a FrameFixture, a container in both AssertJ and FEST), but I am unable to simulate input when using a JComboBoxFixture i.e.
window.comboBox("comboB").pressAndReleaseKeys(KeyEvent.VK_LEFT);
This obstacle can usually be avoided, since most "key presses" can be simulated by using enterText i.e.
window.comboBox("comboB").enterText("n"); //to press the enter key
window.comboBox("comboB").enterText("b"); //to press the backspace key
but I would like to be able to use the arrow keys, control key, and other keys where I can't simulate the key press using enterText(). Is this failure due to an issue with my environment*, an issue with the way I'm using it, or is the API itself flawed?
I tried using pressKey() and then releaseKey() as a workaround, but that doesn't work with JComboBox either, and my program instead hangs on pressKey(). That being said, I am not able to use pressKey() and releaseKey() to test a JComboBox with FEST either.
*Environment details:
Language version: java version "1.8.0_131"
Platform version (e.g. .NET 3.5; note that this isn’t always implicit from the language version, or vice versa)
Operating system: Red Hat Release 6.10 (Santiago)
IDE: Netbeans 8.0.2
Sample GUI application:
package javapractice;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ComboBoxSample extends JFrame implements ItemListener
JPanel jp;
JComboBox jcb;
JLabel result;
JLabel title;
JTextField jtc;
public static void main(String[] args)
ComboBoxSample frame = new ComboBoxSample();
ComboBoxSample()
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("Testing AssertJ");
this.setLayout(new FlowLayout());
jp = new JPanel();
jcb = new JComboBox(new String[] "Pork", "Beans", "Rice");
jcb.setEditable(true);
jcb.setName("combob");
jtc = new JTextField();
jtc.setEditable(true);
jtc.setPreferredSize(new Dimension(150, 25));
jtc.setName("textb");
title = new JLabel("Food: ");
result = new JLabel("No food");
jp.add(title);
jp.add(jcb);
jp.add(result);
jp.add(jtc);
this.add(jp);
this.setLocationRelativeTo(null);
jcb.addItemListener(this);
this.pack();
this.repaint();
@Override
public void itemStateChanged(ItemEvent e)
if(e.getSource() == jcb)
result.setText("I'm eating " + jcb.getSelectedItem());
this.pack();
public void cleanUp()
jcb = null;
result = null;
jtc = null;
jp = null;
title = null;
Test File for Fest:
package javapractice;
import com.sun.glass.events.KeyEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Fest imports.
*/
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.fixture.FrameFixture;
public class ComboBoxSampleTest
private FrameFixture window;
private ComboBoxSample frame;
@BeforeClass
public static void setUpClass()
FailOnThreadViolationRepaintManager.install();
@AfterClass
public static void tearDownClass()
@Before
public void setUp()
frame = GuiActionRunner.execute(new GuiQuery<ComboBoxSample>()
@Override
protected ComboBoxSample executeInEDT()
return new ComboBoxSample();
);
window = new FrameFixture(frame);
window.show();
@After
public void tearDown()
window.cleanUp();
frame.cleanUp();
/**
* Test of main method, of class ComboBoxSample.
*/
@Test
public void testMain()
//Delay so that we can see what's going on
try
Thread.sleep(2000);
catch (InterruptedException ie)
window.textBox("textb").enterText("hi there");
window.textBox("textb").pressAndReleaseKeys(KeyEvent.VK_BACKSPACE);
window.comboBox().replaceText("Bean");
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_ENTER);
Test File for AssertJ:
package javapractice;
import com.sun.glass.events.KeyEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* AssertJ imports.
*/
import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
public class ComboBoxSampleTest
private FrameFixture window;
private ComboBoxSample frame;
@BeforeClass
public static void setUpClass()
FailOnThreadViolationRepaintManager.install();
@AfterClass
public static void tearDownClass()
@Before
public void setUp()
frame = GuiActionRunner.execute(() -> new ComboBoxSample());
window = new FrameFixture(frame);
window.show();
@After
public void tearDown()
window.cleanUp();
frame.cleanUp();
/**
* Test of main method, of class ComboBoxSample.
*/
@Test
public void testMain()
//Delay so that we can see what's going on
try
Thread.sleep(2000);
catch (InterruptedException ie)
window.textBox("textb").enterText("hi there");
window.textBox("textb").pressAndReleaseKeys(KeyEvent.VK_BACKSPACE);
window.comboBox().replaceText("Bean");
//the above line is the last one to execute
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_ENTER);
java swing junit assertj fest
add a comment |
When trying to simulate input using AssertJ's pressAndReleaseKeys() for unit testing a JComboBox in a Java Swing program, I am not seeing the expected behavior. The program will most often hang on the pressAndReleaseKeys line and then fail, or occasionally will delete all the text currently in the JComboBox being tested, causing later assertions to fail (i.e. requireSelection()). The stack trace I receive for the provided example program (see below) when it hangs is as follows:
Focus change to javax.swing.JComboBox[name='combob', selectedItem='Bean', contents=["Pork", "Beans", "Rice"], editable=true, enabled=true, visible=true, showing=true] failed focus owner: javax.swing.plaf.metal.MetalComboBoxEditor$1(javax.swing.JTextField)[name=null, text='Bean', enabled=true, visible=true, showing=true]
org.assertj.swing.exception.ActionFailedException
at org.assertj.swing.exception.ActionFailedException.actionFailure(ActionFailedException.java:33)
at org.assertj.swing.core.BasicRobot.focus(BasicRobot.java:301)
at org.assertj.swing.core.BasicRobot.focusAndWaitForFocusGain(BasicRobot.java:270)
at org.assertj.swing.driver.ComponentDriver.focusAndWaitForFocusGain(ComponentDriver.java:419)
at org.assertj.swing.driver.ComponentDriver.pressAndReleaseKeys(ComponentDriver.java:315)
at org.assertj.swing.fixture.AbstractComponentFixture.pressAndReleaseKeys(AbstractComponentFixture.java:293)
at javapractice.ComboBoxSampleTest.testMain(ComboBoxSampleTest.java:59)
I have been using FEST and am hoping to migrate my tests to AssertJ since it is being actively maintained, whereas FEST hasn't been updated for years. I used Joel Costigliola's migration from Fest to AssertJ guide, but am having trouble when simulating keyboard input by using pressAndReleaseKeys(). I am able to simulate input when using a JTextComponentFixture i.e.
window.textBox("textB").pressAndReleaseKeys(KeyEvent.VK_LEFT);
(where window is a FrameFixture, a container in both AssertJ and FEST), but I am unable to simulate input when using a JComboBoxFixture i.e.
window.comboBox("comboB").pressAndReleaseKeys(KeyEvent.VK_LEFT);
This obstacle can usually be avoided, since most "key presses" can be simulated by using enterText i.e.
window.comboBox("comboB").enterText("n"); //to press the enter key
window.comboBox("comboB").enterText("b"); //to press the backspace key
but I would like to be able to use the arrow keys, control key, and other keys where I can't simulate the key press using enterText(). Is this failure due to an issue with my environment*, an issue with the way I'm using it, or is the API itself flawed?
I tried using pressKey() and then releaseKey() as a workaround, but that doesn't work with JComboBox either, and my program instead hangs on pressKey(). That being said, I am not able to use pressKey() and releaseKey() to test a JComboBox with FEST either.
*Environment details:
Language version: java version "1.8.0_131"
Platform version (e.g. .NET 3.5; note that this isn’t always implicit from the language version, or vice versa)
Operating system: Red Hat Release 6.10 (Santiago)
IDE: Netbeans 8.0.2
Sample GUI application:
package javapractice;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ComboBoxSample extends JFrame implements ItemListener
JPanel jp;
JComboBox jcb;
JLabel result;
JLabel title;
JTextField jtc;
public static void main(String[] args)
ComboBoxSample frame = new ComboBoxSample();
ComboBoxSample()
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("Testing AssertJ");
this.setLayout(new FlowLayout());
jp = new JPanel();
jcb = new JComboBox(new String[] "Pork", "Beans", "Rice");
jcb.setEditable(true);
jcb.setName("combob");
jtc = new JTextField();
jtc.setEditable(true);
jtc.setPreferredSize(new Dimension(150, 25));
jtc.setName("textb");
title = new JLabel("Food: ");
result = new JLabel("No food");
jp.add(title);
jp.add(jcb);
jp.add(result);
jp.add(jtc);
this.add(jp);
this.setLocationRelativeTo(null);
jcb.addItemListener(this);
this.pack();
this.repaint();
@Override
public void itemStateChanged(ItemEvent e)
if(e.getSource() == jcb)
result.setText("I'm eating " + jcb.getSelectedItem());
this.pack();
public void cleanUp()
jcb = null;
result = null;
jtc = null;
jp = null;
title = null;
Test File for Fest:
package javapractice;
import com.sun.glass.events.KeyEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Fest imports.
*/
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.fixture.FrameFixture;
public class ComboBoxSampleTest
private FrameFixture window;
private ComboBoxSample frame;
@BeforeClass
public static void setUpClass()
FailOnThreadViolationRepaintManager.install();
@AfterClass
public static void tearDownClass()
@Before
public void setUp()
frame = GuiActionRunner.execute(new GuiQuery<ComboBoxSample>()
@Override
protected ComboBoxSample executeInEDT()
return new ComboBoxSample();
);
window = new FrameFixture(frame);
window.show();
@After
public void tearDown()
window.cleanUp();
frame.cleanUp();
/**
* Test of main method, of class ComboBoxSample.
*/
@Test
public void testMain()
//Delay so that we can see what's going on
try
Thread.sleep(2000);
catch (InterruptedException ie)
window.textBox("textb").enterText("hi there");
window.textBox("textb").pressAndReleaseKeys(KeyEvent.VK_BACKSPACE);
window.comboBox().replaceText("Bean");
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_ENTER);
Test File for AssertJ:
package javapractice;
import com.sun.glass.events.KeyEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* AssertJ imports.
*/
import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
public class ComboBoxSampleTest
private FrameFixture window;
private ComboBoxSample frame;
@BeforeClass
public static void setUpClass()
FailOnThreadViolationRepaintManager.install();
@AfterClass
public static void tearDownClass()
@Before
public void setUp()
frame = GuiActionRunner.execute(() -> new ComboBoxSample());
window = new FrameFixture(frame);
window.show();
@After
public void tearDown()
window.cleanUp();
frame.cleanUp();
/**
* Test of main method, of class ComboBoxSample.
*/
@Test
public void testMain()
//Delay so that we can see what's going on
try
Thread.sleep(2000);
catch (InterruptedException ie)
window.textBox("textb").enterText("hi there");
window.textBox("textb").pressAndReleaseKeys(KeyEvent.VK_BACKSPACE);
window.comboBox().replaceText("Bean");
//the above line is the last one to execute
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_ENTER);
java swing junit assertj fest
When trying to simulate input using AssertJ's pressAndReleaseKeys() for unit testing a JComboBox in a Java Swing program, I am not seeing the expected behavior. The program will most often hang on the pressAndReleaseKeys line and then fail, or occasionally will delete all the text currently in the JComboBox being tested, causing later assertions to fail (i.e. requireSelection()). The stack trace I receive for the provided example program (see below) when it hangs is as follows:
Focus change to javax.swing.JComboBox[name='combob', selectedItem='Bean', contents=["Pork", "Beans", "Rice"], editable=true, enabled=true, visible=true, showing=true] failed focus owner: javax.swing.plaf.metal.MetalComboBoxEditor$1(javax.swing.JTextField)[name=null, text='Bean', enabled=true, visible=true, showing=true]
org.assertj.swing.exception.ActionFailedException
at org.assertj.swing.exception.ActionFailedException.actionFailure(ActionFailedException.java:33)
at org.assertj.swing.core.BasicRobot.focus(BasicRobot.java:301)
at org.assertj.swing.core.BasicRobot.focusAndWaitForFocusGain(BasicRobot.java:270)
at org.assertj.swing.driver.ComponentDriver.focusAndWaitForFocusGain(ComponentDriver.java:419)
at org.assertj.swing.driver.ComponentDriver.pressAndReleaseKeys(ComponentDriver.java:315)
at org.assertj.swing.fixture.AbstractComponentFixture.pressAndReleaseKeys(AbstractComponentFixture.java:293)
at javapractice.ComboBoxSampleTest.testMain(ComboBoxSampleTest.java:59)
I have been using FEST and am hoping to migrate my tests to AssertJ since it is being actively maintained, whereas FEST hasn't been updated for years. I used Joel Costigliola's migration from Fest to AssertJ guide, but am having trouble when simulating keyboard input by using pressAndReleaseKeys(). I am able to simulate input when using a JTextComponentFixture i.e.
window.textBox("textB").pressAndReleaseKeys(KeyEvent.VK_LEFT);
(where window is a FrameFixture, a container in both AssertJ and FEST), but I am unable to simulate input when using a JComboBoxFixture i.e.
window.comboBox("comboB").pressAndReleaseKeys(KeyEvent.VK_LEFT);
This obstacle can usually be avoided, since most "key presses" can be simulated by using enterText i.e.
window.comboBox("comboB").enterText("n"); //to press the enter key
window.comboBox("comboB").enterText("b"); //to press the backspace key
but I would like to be able to use the arrow keys, control key, and other keys where I can't simulate the key press using enterText(). Is this failure due to an issue with my environment*, an issue with the way I'm using it, or is the API itself flawed?
I tried using pressKey() and then releaseKey() as a workaround, but that doesn't work with JComboBox either, and my program instead hangs on pressKey(). That being said, I am not able to use pressKey() and releaseKey() to test a JComboBox with FEST either.
*Environment details:
Language version: java version "1.8.0_131"
Platform version (e.g. .NET 3.5; note that this isn’t always implicit from the language version, or vice versa)
Operating system: Red Hat Release 6.10 (Santiago)
IDE: Netbeans 8.0.2
Sample GUI application:
package javapractice;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ComboBoxSample extends JFrame implements ItemListener
JPanel jp;
JComboBox jcb;
JLabel result;
JLabel title;
JTextField jtc;
public static void main(String[] args)
ComboBoxSample frame = new ComboBoxSample();
ComboBoxSample()
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("Testing AssertJ");
this.setLayout(new FlowLayout());
jp = new JPanel();
jcb = new JComboBox(new String[] "Pork", "Beans", "Rice");
jcb.setEditable(true);
jcb.setName("combob");
jtc = new JTextField();
jtc.setEditable(true);
jtc.setPreferredSize(new Dimension(150, 25));
jtc.setName("textb");
title = new JLabel("Food: ");
result = new JLabel("No food");
jp.add(title);
jp.add(jcb);
jp.add(result);
jp.add(jtc);
this.add(jp);
this.setLocationRelativeTo(null);
jcb.addItemListener(this);
this.pack();
this.repaint();
@Override
public void itemStateChanged(ItemEvent e)
if(e.getSource() == jcb)
result.setText("I'm eating " + jcb.getSelectedItem());
this.pack();
public void cleanUp()
jcb = null;
result = null;
jtc = null;
jp = null;
title = null;
Test File for Fest:
package javapractice;
import com.sun.glass.events.KeyEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Fest imports.
*/
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.fixture.FrameFixture;
public class ComboBoxSampleTest
private FrameFixture window;
private ComboBoxSample frame;
@BeforeClass
public static void setUpClass()
FailOnThreadViolationRepaintManager.install();
@AfterClass
public static void tearDownClass()
@Before
public void setUp()
frame = GuiActionRunner.execute(new GuiQuery<ComboBoxSample>()
@Override
protected ComboBoxSample executeInEDT()
return new ComboBoxSample();
);
window = new FrameFixture(frame);
window.show();
@After
public void tearDown()
window.cleanUp();
frame.cleanUp();
/**
* Test of main method, of class ComboBoxSample.
*/
@Test
public void testMain()
//Delay so that we can see what's going on
try
Thread.sleep(2000);
catch (InterruptedException ie)
window.textBox("textb").enterText("hi there");
window.textBox("textb").pressAndReleaseKeys(KeyEvent.VK_BACKSPACE);
window.comboBox().replaceText("Bean");
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_ENTER);
Test File for AssertJ:
package javapractice;
import com.sun.glass.events.KeyEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* AssertJ imports.
*/
import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
public class ComboBoxSampleTest
private FrameFixture window;
private ComboBoxSample frame;
@BeforeClass
public static void setUpClass()
FailOnThreadViolationRepaintManager.install();
@AfterClass
public static void tearDownClass()
@Before
public void setUp()
frame = GuiActionRunner.execute(() -> new ComboBoxSample());
window = new FrameFixture(frame);
window.show();
@After
public void tearDown()
window.cleanUp();
frame.cleanUp();
/**
* Test of main method, of class ComboBoxSample.
*/
@Test
public void testMain()
//Delay so that we can see what's going on
try
Thread.sleep(2000);
catch (InterruptedException ie)
window.textBox("textb").enterText("hi there");
window.textBox("textb").pressAndReleaseKeys(KeyEvent.VK_BACKSPACE);
window.comboBox().replaceText("Bean");
//the above line is the last one to execute
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_DOWN);
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_ENTER);
java swing junit assertj fest
java swing junit assertj fest
asked Mar 22 at 18:22
NashirNashir
15
15
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is not an answer to the question, but a workaround that allows the desired behavior. This issue can be mitigated by invoking robot() for the comboBox().
Instead of
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
try doing
window.comboBox().robot().pressAndReleaseKeys(KeyEvent.VK_S);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305725%2fwhy-does-jpanelfixture-combobox-pressandreleasekeys-work-with-fest-but-not%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is not an answer to the question, but a workaround that allows the desired behavior. This issue can be mitigated by invoking robot() for the comboBox().
Instead of
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
try doing
window.comboBox().robot().pressAndReleaseKeys(KeyEvent.VK_S);
add a comment |
This is not an answer to the question, but a workaround that allows the desired behavior. This issue can be mitigated by invoking robot() for the comboBox().
Instead of
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
try doing
window.comboBox().robot().pressAndReleaseKeys(KeyEvent.VK_S);
add a comment |
This is not an answer to the question, but a workaround that allows the desired behavior. This issue can be mitigated by invoking robot() for the comboBox().
Instead of
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
try doing
window.comboBox().robot().pressAndReleaseKeys(KeyEvent.VK_S);
This is not an answer to the question, but a workaround that allows the desired behavior. This issue can be mitigated by invoking robot() for the comboBox().
Instead of
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
try doing
window.comboBox().robot().pressAndReleaseKeys(KeyEvent.VK_S);
answered Mar 25 at 15:14
NashirNashir
15
15
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305725%2fwhy-does-jpanelfixture-combobox-pressandreleasekeys-work-with-fest-but-not%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown