Your test does seem wrong:
- Your unit test is about testing
ServiceLogic
why do you mock it then ?
- Also you don't have any expectations on any interaction with your
ServiceDAO
mock.
As the question is tagged Mockito, I propose the following solution (minus the imports) that you can adapt to your code :
@RunWith(MockitoJUnitRunner.class)
public class ServiceLogicTest {
@Mock ServiceDAO serviceDAO;
@InjectMocks ServiceLogic serviceLogic;
@Test
public void ensure_executeStatement_is_called_with_right_param() throws Exception {
// given
String input = "Some input";
// when
serviceLogic.getDataFrom(input);
// then
verify(serviceDAO).executeStatement("expected param");
}
}
When writing tests, I like to use the BDD (Behavior Driven Development) style to guide me to what I want to test. I encourage you to practice it, you can have look at the wiki page.
So for your question, you should take a look at the verify
line, it put the mock in a verification mode, so can actually verify that the method executeStatement
is actually called with the argument value "expected param"
.
If you have more complex parameters, you can use some matchers using the Hamcrest library:
verify(serviceDAO).executeStatement(argThat(hasProperty("propertyName")));
Or you can use a Mockito's ArgumentCaptor
in combination with the FEST-Assert library (usually my preferred approach):
ArgumentCaptor<ComplexArgument> argCaptor = ArgumentCaptor.forClass(ComplexArgument.class);
verify(serviceDAO).executeStatement(argCaptor.capture());
assertThat(argCaptor.getValue()).isNotNull().satisfies(myComplexArgumentCondition());
The main idea is to have understandable code, in production code and in test code.
For further reading have a look at the Mockito Javadoc.