title: Unittest with Mockito - Ignore method call
body: <p>Im struggeling to learn Mockito to unittest a application. Below is a example of the method im currently trying to test</p>
  
  <pre><code>public boolean validateFormula(String formula) {
  
      boolean validFormula = true;
      double result = 0;
  
      try {
          result = methodThatCalculatAFormula(formula, 10, 10);
      } catch (Exception e) {
          validFormula = false;
      }
  
      if (result == 0)
          validFormula = false;
      return validFormula;
  }
  </code></pre>
  
  <p>This method calls another method in the same class, <code>methodThatCalculatAFormula</code>, which I do not want to call when i unittest <code>validateFormula</code>.</p>
  
  <p>To test this, I would like to see how this method behaves depending on what <code>methodThatCalculatAFormula</code> returns. Since it returns <code>false</code> when <code>result</code> is 0, and returns valid if it's any number but 0 I would like to simulate these returnvalues without running the actual <code>methodThatCalculatAFormula</code> method.</p>
  
  <p>I have written the following:</p>
  
  <pre><code>public class FormlaServiceImplTest {
      @Mock
  FormulaService formulaService;
  
  @Before
  public void beforeTest() {
     MockitoAnnotations.initMocks(this);
  }
  
  @Test
  public void testValidateFormula() {                 
  
  `//Valid since methodThatCalculatAFormula returns 3`    
  when(formulaService.methodThatCalculatAFormula(anyString(),anyDouble(),anyDouble(),anyBoolean())).thenReturn((double)3);        
          assertTrue(formulaService.validateFormula("Valid"));    
  
  
  
  //Not valid since methodThatCalculatAFormula returns 0
  when(formulaService.methodThatCalculatAFormula(anyString(),anyDouble(),anyDouble(),anyBoolean())).thenReturn((double)0);
      assertFalse(formulaService.validateFormula("Not Valid"));
  }
  </code></pre>
  
  <p>However when I run the above code my <code>assertTrue</code> is <code>false</code>. Im guessing i've done something wrong in my mock setup. How would I test the above method by simulating the return value of <code>methodThatCalculatAFormula</code> without actually calling it.</p>