【发布时间】:2015-03-11 18:31:23
【问题描述】:
我有以下方法要求用户输入,直到输入有效的用户凭据。然后为该用户生成 id 并设置已注册 =TRUE。
1.如何检查单元测试中“注册”的局部变量的值?
2.如何在我的测试中断言 while 循环执行直到“注册”变为 TRUE?
private void register() {
boolean registered=false;
while(!registered){
try {
String uname =this.read("User Name : ");
char password[] = this.readPassword();
String serverURL = this.read("Server URL : ");
if(!uname.isEmpty() && password!=null && !serverURL.isEmpty()){
registered=this.getUID(uname,password,serverURL);
}
if(registered==false)
System.out.println("\nPlease verify your details and try again!\n");
} catch (UnsupportedEncodingException e) {}
catch(Exception e){}
}
System.out.println("Successful");
}
我遇到过使用 ArgumentCaptor 来捕获要测试的方法用来调用另一个方法的变量。
e.g verify(mockObj).intArgumentMethod(argument.capture());
但是我没有将变量“注册”传递给任何其他方法,否则我会捕获它。
【问题讨论】:
-
您可以使用reflections 进行验证。我认为为此您必须将其设为类级别变量而不是方法变量。不确定。
-
在这种情况下,我需要将其设为类变量,但这不是一个选项
标签: java unit-testing junit mocking mockito