您好我的"Wanted but not invoked"
测试出现testRegLogin()
错误,但其余测试都已通过。
我的测试如下。此代码测试Login.java
代码,该代码检查输入到JTextField
的详细信息,然后根据数据库进行检查。如果正确登录,则会调用Controller
类。
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import com.jogamp.newt.event.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import javax.swing.*;
public class TestLoginJunit {
Login log = new Login();
String username = "edwardc5";
String password = "case4";
Robot r;
JButton[] buttons = log.sendButton();
//correct login for program expected outcome = new controller invoked
@Test
public void testCorrectLogin() throws AWTException {
r=new Robot();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection(username);
clipboard.setContents(stringSelection, stringSelection);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
r.delay(500);
stringSelection= new StringSelection(password);
clipboard.setContents(stringSelection, stringSelection);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
r.delay(500);
buttons[0].doClick();
r.delay(100);
Controller controller = Mockito.spy(new Controller());
verify(controller, times(1));
}
//testing wrong username outcome= error displayed and controller not invoked
@Test
public void testWrongUsernameLogin() throws AWTException {
Login login = mock(Login.class);
r=new Robot();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection("ed");
clipboard.setContents(stringSelection, stringSelection);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
r.delay(500);
stringSelection= new StringSelection(password);
clipboard.setContents(stringSelection, stringSelection);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
r.delay(500);
buttons[0].doClick();
r.delay(100);
verify(login, never()).invokeController();
}
//testing cancel button outcome= window closes
@Test
public void testCancelLogin() throws AWTException {
Login login = mock(Login.class);
r=new Robot();
r.delay(500);
buttons[2].doClick();
r.delay(100);
verify(login, never()).invokeController();
}
//testing registration button outcome = register is invoked
@Test
public void testRegLogin() throws AWTException {
Login login = mock(Login.class);
r=new Robot();
buttons[1].doClick();
r.delay(500);
verify(login, times(1)).register();
}
}
答案 0 :(得分:0)
Mockito用testRegLogin()
方法告诉你,你希望login.register()
可以召唤一次,但是Mockito会告诉你它从未被调用过。您可以通过更改
verify(login, times(1)).register();
到
verify(login, never()).register();