Yes, absolutely, it is allowed to use a variety of test doubles including mocks, fakes, and stubs, all within the same test. As ndrone mentioned in the comments, you may want to pick a naming system or convention to help you identify which of your dependencies are mocks or fakes, particularly to make it easier to help diagnose whether a test is failing due to a missing mock expectation or due to an actual system failure.
You may also consider using real implementations; after all, the decision to isolate your unit-under-test can be taken to excess, as one would hope you could trust the JRE's implementations or Mockito's implementations, and you shouldn't be tempted to mock or replace those. I tend to use real implementations when they are fast, deterministic, and well-tested—and when they are straightforward enough not to require deep dependency graphs themselves. (This takes a bit of judgment; I'd be tempted to use a real EmailAddressParser, for example, but maybe not a real FullStackRpcServer.)
One word of caution, though: You may want to extract the return value to a variable.
Result result = stringGrid.writeCell(rowNum,colNum,value);
when(computationHelper.someMethod()).thenReturn(someMethodResult);
This is for two reasons:
For newbies to Mockito, your original syntax may look like writeCell
is called every time someMethod
is called, where in reality that doesn't happen unless you write your own Answer. Extracting it to a variable makes it clear that Mockito will evaluate writeCell
exactly once when you're setting up your when
statement.
Though your syntax works fine when using a fake or a real implementation, if stringGrid
were a mock, similar code may not work. This is because Mockito relies heavily on side-effects and execution order, and so calling a mock in the middle of stubbing a method may cause hard-to-diagnose exceptions. As a rule, it's safer to ensure that every parameter within a call to when
or verify
is a local variable, constant, or literal (possibly within a Mockito matcher, as applicable); extracting here makes it easier to hold to that rule.