Im struggeling to learn Mockito to unittest a application. Below is a example of the method im currently trying to test
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; } This method calls another method in the same class, methodThatCalculatAFormula, which I do not want to call when i unittest validateFormula.
To test this, I would like to see how this method behaves depending on what methodThatCalculatAFormula returns. Since it returns false when result is 0, and returns valid if it's any number but 0 I would like to simulate these returnvalues without running the actual methodThatCalculatAFormula method.
I have written the following:
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")); } However when I run the above code my assertTrue is false. Im guessing i've done something wrong in my mock setup. How would I test the above method by simulating the return value of methodThatCalculatAFormula without actually calling it.