java - JUnit testing a method with JOptionPane.showMessageDialog -
i have made game user , ai takes turns "rolling" dice. game automatically takes ai's turn , returns user's turn. used joptionpane.showmessagedialog popup dialog box notifying user turn. working when execute junit test class test hold() method popup comes up. there way suppress popup or automatically close window in junit test class?
public void hold() { this.swapwhoseturn(); this.setchanged(); this.notifyobservers(); if (this.getcurrentplayer().getismyturn() == this.getcomputerplayer().getismyturn()) { this.thecomputer.taketurn(); this.hold(); humanplayerpanel.turnalert(); }
the turnalert static method in class called humanplayerpanel. here code.
public static void turnalert() { joptionpane.showmessagedialog(null, "it turn"); }
i saw can call doclick method on ok_option button i'm not sure how find button. appreciated.
refactor if statement follows:
if (this.getcurrentplayer().getismyturn() == this.getcomputerplayer().getismyturn()) { this.thecomputer.taketurn(); this.hold(); humanplayerpanel.turnalert(); }
to this:
... if (this.getcurrentplayer().getismyturn() == this.getcomputerplayer().getismyturn()) { this.thecomputer.taketurn(); this.hold(); alerthumanplayer(); } ... protected void alerthumanplayer(){ humanplayerpanel.turnalert(); }
let's assume above class called game. in test code, extend game class follows:
... public class gamewithnohumanalert extends game{ @override protected void alerthumanplayer(){ // not show alert // humanplayerpanel.turnalert(); } } @test public void uitest(){ gamewithnohumanalert game = new gamewithnohumanalert(); ... // testing now. }
__update__
another, , imho better, idea use mock testing. have tried several of them , suggest take @ jmockit. trivial deal kind of troubles can focus on want / need test.
cheers.
Comments
Post a Comment