简体   繁体   English

如何指定没有输入的测试用例

[英]How to specify a test case that does not have an input

For example: I want to make a case test of the "Logout" function.. What can I put in the "input"? 例如:我想对“注销”功能进行案例测试。我可以在“输入”中添加什么? The click in the Logout button?, because the method does not receive any parameter.. The expected output would be going back to the Login page. 单击注销按钮?,因为该方法没有接收任何参数。。预期的输出将返回到“登录”页面。

public String logout() {
  FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
  return "/Login?faces-redirect=true";
}

This must be a very dumb question, but I'm not used to do test cases so I'm a little lost. 这肯定是一个非常愚蠢的问题,但是我不习惯做测试用例,所以我有点迷茫。

The FacesContext is a global variable. FacesContext是全局变量。 Ie you could inject a mock into it. 即,您可以向其中注入一个模拟。 I assume using Mockito: 我假设使用Mockito:

@Test
public testLogout()
{
   //setup
   FacesContext context = Mockito.mock(FacesContext.class);
   ExternalContext externalContext = Mockito.mock(ExternalContext.class);
   FacesContext.setCurrentInstance(context);
   when(context.getExternalContext()).thenReturn(externalContext);
   //act
   bean.logout();
   //verify
   verify(externalContext).invalidateSession();
}

This is not the best approach, but because of bad designed JSF, you should use the global variables and implicit dependencies. 这不是最佳方法,但是由于JSF设计不良,因此应使用全局变量和隐式依赖关系。 The proper solution would include IoC container and dependency injection. 适当的解决方案将包括IoC容器和依赖项注入。

From a more theoritical point of view, a test case is: 从更理论的角度来看,一个测试案例是:

  • initial situation/configuration (setup) 初始情况/配置(设置)
  • action (the one you want to test) with optional parameters (input) 具有可选参数(输入)的操作(您要测试的操作)
  • expected outcome/output 预期成果/产出

If you are testing "login", your situation is "I am logged out" and your input is "log/pwd" 如果您正在测试“登录”,则您的情况是“我已注销”,而您的输入是“ log / pwd”

If you are testing "logout", your situation is "I am logged in" and you have no input 如果您正在测试“注销”,则您的情况是“我已登录”,并且您没有输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM