A method can be tested either with mock object or without. I prefer the solution without mock when they are not necessary because:

1. They make tests more difficult to understand.
2. After refactoring it is a pain to fix junit tests if they have been implemented with mocks.

But I would like to ask your opinion. Here the method under test:

```
public class OndemandBuilder  {

    .... 
    private LinksBuilder linksBuilder;    
    ....

    public OndemandBuilder buildLink(String pid) {

        broadcastOfBuilder = new LinksBuilder(pipsBeanFactory);
        broadcastOfBuilder.type(XXX).pid(pid);
        return this;

    } 

```

Test with mocks:

```
@Test
public void testbuildLink() throws Exception {

    String type = "XXX";
    String pid = "test_pid";

    LinksBuilder linkBuilder = mock(LinksBuilder.class);
    given(linkBuilder.type(type)).willReturn(linkBuilder);

    //builderFactory replace the new call in order to mock it
    given(builderFactory.createLinksBuilder(pipsBeanFactory)).willReturn(linkBuilder);

    OndemandBuilder returnedBuilder = builder.buildLink(pid);

    assertEquals(builder, returnedBuilder); //they point to the same obj
    verify(linkBuilder, times(1)).type(type);
    verify(linkBuilder, times(1)).pid(pid);
    verifyNoMoreInteractions(linkBuilder);
}

```

The returnedBuilder obj within the method buildLink is 'this' that means that builder and returnedBuilder can't be different because they point to the same object in memory so the assertEquals is not really testing that it contains the expected field set by the method buildLink (which is the pid).

I have changed that test as below, without using mocks. The below test asserts what we want to test which is that the builder contains a LinkBuilder not null and the LinkBuilder pid is the one expected.

```
@Test
public void testbuildLink() throws Exception {
    String pid = "test_pid";

    OndemandBuilder returnedBuilder = builder.buildLink(pid);

    assertNotNull(returnedBuilder.getLinkBuilder());
    assertEquals(pid, returnedBuilder.getLinkBuilder().getPid());
}

```

I wouldn't use mock unless they are necessary, but I wonder if this makes sense or I misunderstand the mock way of testing.