title: How to test api using Mockito? Do we need to mock all the class variables?
body: <p>I want to test an API which uses so many class variables, and also in the method there are so many others method calls, I don't know how to mock them all or do I need a different approach. </p>
  
  <p>Please help me fix this, I am providing the code below:</p>
  
  <pre><code>public class LoginController implements Initializable
  </code></pre>
  
  <p>{</p>
  
  <pre><code>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&lt;ActionEvent&gt;()
      {
          @Override
          public void handle(ActionEvent event)
          {
              String username = userId.getText().trim();
              String credentials = password.getText().trim();
              if (isValid(username) &amp;&amp; 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");
              }
          }
      });
  }
  </code></pre>
  
  <p>}</p>
  
  <p>While creating test for this method, I have written the following test method which initially gives NPE and doesn't proceeds further.</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Although I have stubbed some method calls, still confused whether this is the right way or not. Please help me. Thanks in advance.</p>