It depends on what and how much you want your code to be tested. As you noted tdd , I suppose you wrote your test contracts before any actual production code.
So, in your contract, what do you want to verify using the actOn method:
- So that it returns the
SpecificResult value given by both a Context and Entity - That
add() , specificResult() interactions occur respectively Context and Entity - That
SpecificResult is the same instance returned by Result - and etc.
Depending on what you want to test, you will write the appropriate tests. Perhaps you should consider softening your approach to testing if this section of the code does not matter . And vice versa, if this section can cause the end of the world, as we know it.
Generally speaking, white-box tests are fragile , usually verbose and not expressive , but difficult to refactor . But they are well suited for critical sections, which should not change much and neophytes.
In your case, the layout returned by mock looks like a whitebox test. But then again, if you want to ensure this behavior in production code, that's fine. Mokito can help you with deep stubs.
Context context = mock(Context.class, RETURNS_DEEP_STUBS); given(context.add(any(Entity.class)).specificResult()).willReturn(someSpecificResult);
But don't get used to it, as this is usually considered bad practice and a trial smell.
Other comments:
Your test method name is not accurate enough testActOn tells the reader what behavior you are testing. Typically, tdd practitioners replace the method name with a contract proposal, such as returns_a_SpecificResult_given_both_a_Context_and_an_Entity , which is more readable and gives the practitioner what is being tested.
You create mock instances in a test with the syntax Mockito.mock() , if you have several such tests, I would recommend that you use MockitoJUnitRunner with @Mock annotations, this will slightly stretch your code and allow the reader to better see what is happening in this particular test .
Use BDD (Behavior Driven Dev) or AAA (Arrange Act Assert).
For instance:
@Test public void invoke_add_then_specificResult_on_call_actOn() {
In general, as Eric Evans said Context is king , you must make decisions based on this context. But you really should adhere to best practice as much as possible.
There are a lot of tests read here, Martin Fowler has very good articles about this, James Carr made a list to check anti-patterns , there are also many readings on how to use ridicule well (for example, don’t mock types that you don’t own mojo ), Nat Pryce is a co-author of A growing test-driven object-oriented software that, in my opinion, should be read, plus you have google;)