Re: [easytesting] Parameterized tests: how to keep or relaunch application window?

91 views
Skip to first unread message

Alex Ruiz

unread,
Feb 20, 2013, 6:58:14 AM2/20/13
to easytesting
'onSetup' gets called before every test method is executed. If you'd like to have the app shown once, try this: create a static public method with return type 'void'. Annotate the method with @BeforeClass. This method will be called once, before running the first test method.

I don't know how this change will affect executing the whole test suite. Please give it a try and let us know how it goes.

Cheers,
-Alex


On Tue, Feb 19, 2013 at 8:09 AM, bbg <bukky....@gmail.com> wrote:
Hi


I am trying to write a parameterized FEST test. And I encountered a problem: the first run of my test pass successfully, but the second and all next runs fails with stacktrace like this:

org.fest.swing.exception.ComponentLookupException: Unable to find component using matcher org.fest.swing.core.TypeMatcher[type=fest_test.GuiForParameterizedTest, requireShowing=true].

Component hierarchy:
javax.swing.SwingUtilities$SharedOwnerFrame[name='frame1', title='', enabled=true, visible=false, showing=false]

at org.fest.swing.core.BasicComponentFinder.componentNotFound(BasicComponentFinder.java:271)
at org.fest.swing.core.BasicComponentFinder.find(BasicComponentFinder.java:260)
at org.fest.swing.core.BasicComponentFinder.find(BasicComponentFinder.java:179)
<...>

A similar (or possibly the same) problem appears when I try to create more than one @Test method in the test.

As I understood from thread https://groups.google.com/forum/?fromgroups#!searchin/easytesting/Having$20trouble$20running$20multiple$20tests$20together/easytesting/qfMTQINnjmg/QM5vQfnQ4VAJ , a problem like this appers when trying to reuse a window once opened for many separate tests: the window that was initialized before the first test started is inevitably disposed when this test ends, and the second test has nothing to work with. The proposed solution is to run the window initialization and cleanup in the methods annoteded @BeforeClass and @AfterClass correspondingly. But:
- in this case I can't access to a Robot instance (@BeforeClass and @AfterClass methods should be static, and robot() method is not), and without it I can' t create a fixture for my application frame
- I tried to launch my applicaton separately for each test method run (in onSetUp() method and even in the start of @Test method itself), but the problem persisted: after the try of application launch I received the same stacktrace.

What I am doing wrong?


Code of sample application:

public class GuiForParameterizedTest extends JFrame {
private static final long serialVersionUID = -1484892685385521277L;
private static GuiForParameterizedTest instance;
private GuiForParameterizedTest() {
setSize(100, 100);
JTextArea textArea = new JTextArea();
textArea.setName("textArea");
add(textArea);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static GuiForParameterizedTest getInstance() {
if (instance == null) {
instance = new GuiForParameterizedTest();
}
return instance;
}
public static void main(String[] args) {
getInstance();
}
}

Code of parameterized test:

@RunWith(Parameterized.class)
public class ParameterizedTest extends FestSwingJUnitTestCase {

private FrameFixture guiWindow;
private String text;
@Parameters
public static Collection<String[]> prepareText() {
ArrayList<String[]> texts = new ArrayList<String[]>();
texts.add(new String[] {"text1"});
texts.add(new String[] {"text2"});
return texts;
}
public ParameterizedTest(String text) {
this.text = text;
}
@Override
protected void onSetUp() {
GuiForParameterizedTest gui = GuiActionRunner.execute(new GuiQuery<GuiForParameterizedTest>() {
@Override
protected GuiForParameterizedTest executeInEDT() throws Throwable {
ApplicationLauncher.application(GuiForParameterizedTest.class).start();
GuiForParameterizedTest gui = robot().finder().findByType(GuiForParameterizedTest.class);
return gui;
}
});
guiWindow = new FrameFixture(robot(), gui);
guiWindow.show();
}
@Test
public void checkTextContent() {
guiWindow.textBox("textArea").enterText(text);
guiWindow.textBox("textArea").requireText(text);
}
}

Code of non-parameterized modification of the same test with the same problem:

public class ParameterizedTest extends FestSwingJUnitTestCase {

private FrameFixture guiWindow;
private String text = "text";
@Override
protected void onSetUp() {
GuiForParameterizedTest gui = GuiActionRunner.execute(new GuiQuery<GuiForParameterizedTest>() {
@Override
protected GuiForParameterizedTest executeInEDT() throws Throwable {
ApplicationLauncher.application(GuiForParameterizedTest.class).start();
GuiForParameterizedTest gui = robot().finder().findByType(GuiForParameterizedTest.class);
return gui;
}
});
guiWindow = new FrameFixture(robot(), gui);
guiWindow.show();
}
@Test
public void checkTextContent1() {
guiWindow.textBox("textArea").deleteText();
guiWindow.textBox("textArea").enterText(text);
guiWindow.textBox("textArea").requireText(text);
}
@Test
public void checkTextContent2() {
guiWindow.textBox("textArea").deleteText();
guiWindow.textBox("textArea").enterText(text);
guiWindow.textBox("textArea").requireText(text);
}
}

Sorry if the answer is evident - I managed not to find it myself in the project documentation :(
And sorry for my poor English. It is not my natural language :)

--
You received this message because you are subscribed to the Google Groups "easytesting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to easytesting...@googlegroups.com.
To post to this group, send email to easyt...@googlegroups.com.
Visit this group at http://groups.google.com/group/easytesting?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted
Message has been deleted

bbg

unread,
Feb 22, 2013, 2:14:54 AM2/22/13
to easyt...@googlegroups.com
I have modified test code like this:

@RunWith(Parameterized.class)
public class ParameterizedTest extends FestSwingJUnitTestCase {

private static FrameFixture guiWindow;
private String text;
@BeforeClass
public static void prepare() {
final Robot robot = BasicRobot.robotWithCurrentAwtHierarchy();
GuiForParameterizedTest gui = GuiActionRunner.execute(new GuiQuery<GuiForParameterizedTest>() {
@Override
protected GuiForParameterizedTest executeInEDT() throws Throwable {
ApplicationLauncher.application(GuiForParameterizedTest.class).start();
GuiForParameterizedTest gui = robot.finder().findByType(GuiForParameterizedTest.class);
return gui;
}
});
guiWindow = new FrameFixture(robot, gui);
guiWindow.show();
}
@Parameters
public static Collection<String[]> prepareText() {
ArrayList<String[]> texts = new ArrayList<String[]>();
texts.add(new String[] {"text1"});
texts.add(new String[] {"text2"});
return texts;
}
public ParameterizedTest(String text) {
this.text = text;
}
@Override
protected void onSetUp() {

}
@Test
public void checkTextContent() {
guiWindow.textBox("textArea").enterText(text);
guiWindow.textBox("textArea").requireText(text);
}
}

Now, when I try to execute the test, it just don't run the @Test-annotated method (and onSetUp() method too). But @BeforeClass- and @Parameters-annotated methods are being executed. The window of my application is drawn, but no any action take place with it. The JUnit view in Eclipse remains looking like if the test is being running.
Probably the reason is final Robot robot = BasicRobot.robotWithCurrentAwtHierarchy(); , but I don't know how to receive Robot instance by other way in the static method and can' t manage without it (I need it to create the FrameFixture of my application window). Anyway, the idea to relaunch  the application afresh each run of @Test-annotated method can also be suitable, but my attempt to do it was not successful also (it is described in first message of this thread).
Reply all
Reply to author
Forward
0 new messages