我写了一个正在工作的java代码,但我必须为它编写一个Junit测试脚本,但我还没有经验。我尝试了几个小时,但我不明白它是如何工作的。所以你的帮助非常受欢迎。在此先感谢:)你有任何tipps给我吗? :)通过,如果你想测试你是否错误处理工程测试您有一定的预期输出,或不正确的输出编写的代码如何编写junit测试脚本?
import java.awt.*;
import java.awt.event.*;
class MailBox extends Frame {
private boolean request;
private String message;
TextField tf1;
public MailBox() {
Dimension screenDim = getToolkit().getScreenSize();
Dimension frameDim = getPreferredSize();
setLocation((screenDim.width-frameDim.width)/2, (screenDim.heightframeDim.height)/2); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}
Panel myPanel = new Panel();
myPanel.setLayout(new FlowLayout());
Label label1 = new Label("Message: ");
Button button1 = new Button("Send");
button1.addActionListener(new button1AL());
tf1 = new TextField("", 20);
myPanel.add(label1);
myPanel.add(tf1);
myPanel.add(button1);
add(myPanel, BorderLayout.CENTER);
setTitle("Mailbox");
pack();
show();
}
public synchronized void storeMessage(String message){
while(request==true){
try{
wait();
}
catch(InterruptedException e){
}
}
request = true;
this.message = message;
notify();
}
public synchronized String retrieveMessage(){
while(request==false){
try{
wait();
}
catch(InterruptedException e){
}
}
request=false;
notify();
return message;
}
public static void main(String args[]) {
System.out.println("Starting Mailbox...");
MailBox MyMailBox = new MailBox();
Consumer c1 = new Consumer(MyMailBox);
Thread t1 = new Thread(c1);
t1.start();
}
class button1AL implements ActionListener{
public void actionPerformed(ActionEvent ae){
storeMessage(tf1.getText());
tf1.setText("");
}
}
}
我的提示是正确缩进你的代码,这对于你自己和其他人在阅读代码时的好处都是合适的。 –