如何使用Mockito测试api?我们需要模拟所有类变量吗?

时间:2014-11-10 11:44:07

标签: java unit-testing testing mockito

我想测试一个使用这么多类变量的API,而且在方法中还有很多其他方法调用,我不知道如何模拟它们或者我需要一个不同的方法。

请帮我解决这个问题,我提供以下代码:

public class LoginController implements Initializable

{

protected static BorderPane choose;

protected static VBox menu;

@FXML
private BorderPane loginPane;

@FXML
private Label errorText;

@FXML
private PasswordField password;

@FXML
private Label databaseName;

@FXML
private Label host;

@FXML
private Button login;

@FXML
private TextField userId;

BaseFrame mainController = BaseFrame.getMainController();

@Override
public void initialize(URL arg0, ResourceBundle arg1)
{
    assert login != null : "fx:id=\"login\" was not injected: check your FXML file 'Login.fxml'.";
    OSSDatabase ossDatabase = (OSSDatabase) OSSConfigurationTool.getContext().getBean("ossDatabase");
    this.databaseName.setText(ossDatabase.getName());
    this.host.setText(ossDatabase.getHost());
    login.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent event)
        {
            String username = userId.getText().trim();
            String credentials = password.getText().trim();
            if (isValid(username) && isValid(credentials))
            {
                errorText.setText("");
                UserManager userManager = (UserManager) OSSConfigurationTool.getContext().getBean("UserManager");
                try
                {
                    userManager.login(username, credentials, true);
                    FXMLLoader menuLoader = new FXMLLoader(LoginController.class.getResource("/com/capsilon/oss/configuration/tool/view/mainMenu.fxml"));
                    try
                    {
                        mainController.getMenuPane().setVisible(true);
                        mainController.getMenuPane().add((VBox) menuLoader.load(), 0, 1);
                    } catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (OSSException e)
                {
                    e.printStackTrace();
                    errorText.setText(e.getErrorCode().name());
                    errorText.setTooltip(new Tooltip(e.getMessage()));
                }
            }
            else
            {
                errorText.setText("Username or Password is empty");
            }
        }
    });
}

}

在为此方法创建测试时,我编写了以下测试方法,该方法最初给出NPE并且不会进一步进行。

public class LoginControllerTest 
{
@Mock
OSSDatabase ossDatabase;
@Mock
private TextField userId;
@Mock
private PasswordField password;

LoginController controller = new LoginController();

@Before
public void setUp() throws Exception 
{
}

@Test
public final void testInitialize() 
{
    URL url = PowerMockito.mock(URL.class);
    ResourceBundle bundle = Mockito.mock(ResourceBundle.class);

    Mockito.when((OSSDatabase) OSSConfigurationTool.getContext().getBean("ossDatabase")).thenReturn(ossDatabase);
    Mockito.when(ossDatabase.getName()).thenReturn("SomeName");
    Mockito.when(ossDatabase.getHost()).thenReturn("SomeHost");
    Mockito.when(userId.getText().trim()).thenReturn("userName");

    controller.initialize(url, bundle);
}

}

虽然我已经存在一些方法调用,但仍然混淆这是否是正确的方法。请帮我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你不知道你的NPE在哪里......

我认为它就在这里:

((OSSDatabase) OSSConfigurationTool.getContext()

当你得到上下文没有?

如果是,则必须模拟OSSConfigurationTool.getContext()以返回上下文;)

如果OSSConfigurationTool是您必须使用的静态调用(在junit&gt; 4中)

@RunWith(PowerMockRunner.class)
@PrepareForTest( OSSConfigurationTool.class )   
public class LoginControllerTest {
...

如果需要,也可以添加此行

PowerMockito.mockStatic(OSSConfigurationTool.class);

尝试所有这些并让我知道它的工作是否

感谢