Unittest with Mockito - call Ignore method

I try my best to learn Mockito in order to remove the application. The following is an example of the im method that is currently trying to verify

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 a value if it is some number but 0, I would like to simulate these return values ​​without running the actual methodThatCalculatAFormula method.

I wrote 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 . I guess I did something wrong in my layout setup. How would I test the above method by simulating the return value of methodThatCalculatAFormula without actually calling it.

+4
source share
2 answers

What you are trying to do is not a layout, but a spy (partial layout). You do not want to mock the object, but only one method.

It works:

 public class FormulaService { 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; } public double methodThatCalculatAFormula(String formula, int i, int j){ return 0; } } 

and

 public class FormulaServiceImplTest { FormulaService formulaService; @Test public void testValidateFormula() { formulaService = spy(new FormulaService()); // Valid since methodThatCalculatAFormula returns 3` doReturn((double) 3).when( formulaService).methodThatCalculatAFormula(anyString(), anyInt(), anyInt()); assertTrue(formulaService.validateFormula("Valid")); // Not valid since methodThatCalculatAFormula returns 0 doReturn((double)0).when( formulaService).methodThatCalculatAFormula(anyString(), anyInt(), anyInt()); assertFalse(formulaService.validateFormula("Not Valid")); } } 

But you should not use a spy. You must redo the class in two so that you can test one against the layout of the other.

+4
source

You cannot test code in Mocked classes. If you just mimic it, all methods will be stubs.

Instead, you should spy. Read the Mockito documentation on how to use Spy.

0
source

Source: https://habr.com/ru/post/1469019/


All Articles