title: Spring Boot Controller Test - Mockito when thenReturn list not working
body: <p>I'm mocking my <code>MemberServiceImpl</code> class.  In particular the following <code>getMembers()</code> method which returns <code>List&lt;Golfer&gt;</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  public class MemberServiceImpl implements MemberService {
  
      private Logger logger = LoggerFactory.getLogger(this.getClass());
  
      @Autowired
      MemberRepository repository;
  
      @Override
      public List&lt;Golfer&gt; getMembers() {
          List&lt;Golfer&gt; members = repository.findAll();
          return members;
      }
  </code></pre>
  
  <p>In my <code>MemberControllerTest</code> class I create a list of golfers and the use the when <code>thenReturn(members)</code> before sending my <code>GET</code> request - which always returns an empty list.  Any ideas what I am doing wrong:</p>
  
  <pre class="lang-java prettyprint-override"><code>@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
  class MemberControllerTest {
  
      @Mock
      MemberService memberService;
  
      @InjectMocks
      MemberController memberController = new MemberController();
  
      @Autowired
      private TestRestTemplate restTemplate;
  
      //bind RANDOM_PORT
      @LocalServerPort
      private int port;
      private String name = "David";
      private Golfer member = new Golfer("David");
      private HttpHeaders headers = new HttpHeaders();
      private String baseUrl = "http://localhost:";
  
      @Test
      void testGetAllMembers() throws Exception{
          restTemplate = new TestRestTemplate();
          HttpHeaders headers = new HttpHeaders();
  
          Golfer member1 = new Golfer("Harry");
          Golfer member2 = new Golfer("Maureen");
          List&lt;Golfer&gt; members = new ArrayList&lt;Golfer&gt;();
          members.add(member1);
          members.add(member2);
  
          when(memberService.getMembers()).thenReturn(members);
  
          ResponseEntity&lt;List&lt;Golfer&gt;&gt; response = restTemplate.exchange("http://localhost:"+port, HttpMethod.GET, null, new ParameterizedTypeReference&lt;List&lt;Golfer&gt;&gt;() {});
  
          assertEquals(members,response.getBody());
      }   
  }
  </code></pre>