Sebelumnya saya dan teman saya telah mencari-cari di google cara test JUnit di JFrame namun ga nemu-nemu, tapi ada satu artikel yang teman saya temukan yang bersisi tentang Melakukan Pengujian Java Swing Dengan FEST, di artikel tersebut menggunakan MigLayout jdi untuk mendisainnya lumayan sulit..
Nah langsung aja cara test JFrame dengan JUnit okay :
pertama anda download fest-swing-1.2.zip terlebih dahulu, Lalu file Jar fest-swing-1.2.jar dan semua file Jar yang berada di folder lib di Add terlebih dahulu ke projek anda.
Anda buat tampilan jframe terlebih dalu, disini saya buat tampilan login :
Login.java :
package cobaTJf;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Login extends JFrame {
private JPanel contentPane;
private JTextField textUser;
private JPasswordField passwordUser;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login() {
this.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 228, 155);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(10, 14, 95, 14);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(10, 39, 95, 14);
contentPane.add(lblPassword);
textUser = new JTextField();
textUser.setBounds(104, 11, 86, 20);
contentPane.add(textUser);
textUser.setColumns(10);
passwordUser = new JPasswordField();
passwordUser.setBounds(104, 39, 86, 20);
contentPane.add(passwordUser);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (textUser.getText().equalsIgnoreCase("Admin") &&
String.valueOf(passwordUser.getPassword()).equalsIgnoreCase("123")) {
JOptionPane.showMessageDialog(null, "Login sukses!");
} else {
JOptionPane.showMessageDialog(null, "Password / Username Salah!");
}
}
});
btnLogin.setBounds(104, 72, 89, 23);
contentPane.add(btnLogin);
textUser.setName("user");
passwordUser.setName("pass");
btnLogin.setName("login");
}
}
Untuk pengujiannya saya membuat test JUnit seperti ini :
pertama anda download fest-swing-1.2.zip terlebih dahulu, Lalu file Jar fest-swing-1.2.jar dan semua file Jar yang berada di folder lib di Add terlebih dahulu ke projek anda.
Anda buat tampilan jframe terlebih dalu, disini saya buat tampilan login :
Login.java :
package cobaTJf;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Login extends JFrame {
private JPanel contentPane;
private JTextField textUser;
private JPasswordField passwordUser;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login() {
this.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 228, 155);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(10, 14, 95, 14);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(10, 39, 95, 14);
contentPane.add(lblPassword);
textUser = new JTextField();
textUser.setBounds(104, 11, 86, 20);
contentPane.add(textUser);
textUser.setColumns(10);
passwordUser = new JPasswordField();
passwordUser.setBounds(104, 39, 86, 20);
contentPane.add(passwordUser);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (textUser.getText().equalsIgnoreCase("Admin") &&
String.valueOf(passwordUser.getPassword()).equalsIgnoreCase("123")) {
JOptionPane.showMessageDialog(null, "Login sukses!");
} else {
JOptionPane.showMessageDialog(null, "Password / Username Salah!");
}
}
});
btnLogin.setBounds(104, 72, 89, 23);
contentPane.add(btnLogin);
textUser.setName("user");
passwordUser.setName("pass");
btnLogin.setName("login");
}
}
Untuk pengujiannya saya membuat test JUnit seperti ini :
LoginTest.java :
package cobaTJf;
import static org.junit.Assert.*;
import java.awt.Dimension;
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.fixture.FrameFixture;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class LoginTest {
private FrameFixture window;
@Before
public void before() {
Login Login = new Login();
window = new FrameFixture(Login);
Dimension size = Login.getSize();
window.show(size);
}
@After
public void after() {
window.cleanUp();
}
@Test
public void testTampilanAwal() {
window.textBox("user").requireEditable().requireEmpty();
window.textBox("pass").requireEditable().requireEmpty();
window.button("login").requireVisible();
}
@Test
public void testLoginSukses() {
window.textBox("user").enterText("admin");
window.textBox("pass").enterText("123");
window.button("login").click();
window.optionPane().requireMessage("Login sukses!");
}
@Test
public void testLoginGagal() {
window.textBox("user").enterText("Bos");
window.textBox("pass").enterText("123");
window.button("login").click();
window.optionPane().requireMessage("Faild!");
}
}
Untuk yang ingin mendownload Java Project Eclipse : DISINI atau DISINI
Kereenn bro...
ReplyDelete