The most common unit testing tool in Java is junit. Fest-swing is a GUI testing framework that can be integrated with JUnit. With Fest-swing, you can perform a series of tests on swing more easily. Let's talk about how to test using fest-swing. First to[Url]http://code.google.com/p/fest/downloads/list[/url]Download the latest fest-swing file above, the general file name should be fest-swing-x.x.zip. After the download is complete, the directory structure is: Note that you need to add all the files under Fest-swing-1.0.jar and Lib to the project path, and you need to add junit support. Here we use JUNIT4, and after adding it we can use fest-swing. Create a new test file below with the following code:
1 Importjava.awt.BorderLayout;2 ImportJava.awt.Container;3 Importjava.awt.event.ActionEvent;4 ImportJava.awt.event.ActionListener;5 6 ImportJavax.swing.Box;7 Importjavax.swing.BoxLayout;8 ImportJavax.swing.JButton;9 ImportJavax.swing.JFrame;Ten ImportJavax.swing.JLabel; One ImportJavax.swing.JTextField; A - Public classMyFrameextendsJFrame { - PublicMyFrame () { theSettitle ("My Frame"); -SetSize (200, 100); - setdefaultcloseoperation (jframe.exit_on_close); - +Container content =Getcontentpane (); -Box VBox =NewBox (boxlayout.y_axis); + Content.add (VBox, borderlayout.center); A at FinalJLabel Showtextlabel =NewJLabel (""); -Showtextlabel.setname ("Show"); - Vbox.add (Showtextlabel); - FinalJTextField input =NewJTextField (); -Input.setname ("Input"); - vbox.add (input); inJButton button =NewJButton ("Copy"); -Button.setname ("Copy"); toButton.addactionlistener (NewActionListener () { + - @Override the Public voidactionperformed (ActionEvent e) { * Showtextlabel.settext (Input.gettext ()); $ } Panax Notoginseng - }); the vbox.add (button); + } A}
Here we do not elaborate on the meaning of the Code, the general function is to click on the button, you can JTextField input text displayed on the JLabel above. It is important to note that fest-swing is using the component's name value to get the component, so the invocation of this setname method is necessary. Create a new JUNIT4 Test case below. You first need to have a property of the Framefixture object. Here we can understand framefixture as the object being tested, because we want to test a jframe, so we use framefixture. In fest-swing, these classes are roughly the same as the name of the swing component, but one more fixture behind. For example, the corresponding class of JButton is Jbuttonfixture. It is then instantiated in the @before method:
1 Privateframefixture frame;2 3 @Before4 Public voidsetUp () {5frame =NewFramefixture (NewMyFrame ()); 6Frame.show ();//show the frame.7 }8 9 //clean it up in the @after method:Ten @After One Public voidTearDown () { A Frame.cleanup (); - } - the //then write the @test method: - @Test - Public voidTestcopytexttolabel () { -Frame.textbox ("Input"). Entertext ("Hello world!"); +Frame.button ("Copy"). Click (); -Frame.label ("Show"). Requiretext ("Hello world!"); +}
Although there is no comment, I think this code is very clear: first get the frame above the input, enter Hello world! in it, and then click the Copy button, then show the text request is Hello world! This is a self-explanatory code, very clear. Here is the name value set by the component that is used to get the component, as mentioned earlier. This completes the completion of a test case, the entire code is as follows:
1 Importorg.fest.swing.fixture.FrameFixture;2 ImportOrg.junit.After;3 ImportOrg.junit.Before;4 Importorg.junit.Test;5 6 Public classMyframetest {7 8 Privateframefixture frame;9 Ten @Before One Public voidsetUp () { Aframe =NewFramefixture (NewMyFrame ()); - frame.show (); - } the - @After - Public voidTearDown () { - Frame.cleanup (); + } - + @Test A Public voidTestcopytexttolabel () { atFrame.textbox ("Input"). Entertext ("Hello world!"); -Frame.button ("Copy"). Click (); -Frame.label ("Show"). Requiretext ("Hello world!"); - } -}
Run the test case below and you can see the performance ... Looks like there is animation effect, very beautiful. Hehe ~ ~ Actually here also just according to the official website above getting started explained fest-swing simple usage, more specific use method please refer to the website above the user manual.
This article is from the "Bean Space" blog, make sure to keep this source http://devbean.blog.51cto.com/448512/126828
Testing the GUI with fest-swing