title: Why can't I mock service dependency of controller when trying to test it using mockMvc?
body: <p>I'm new to testing controller and web layer.
  I have Service which has methods to work with orders, and I have controller that has that service as dependency</p>
  <p>OrderService:</p>
  <pre><code> @Service
      public class OrderServiceImpl implements OrderService{
          private OrderRepository orderRepository;
          private Assembler&lt;Order, OrderDto&gt; orderDtoAssembler;
          @Autowired
          public OrderServiceImpl(OrderRepository orderRepository,Assembler&lt;Order, OrderDto&gt; orderDtoAssembler) {
              this.orderRepository = orderRepository;
              this.orderDtoAssembler = orderDtoAssembler;
          }
      
          @Override
          @Transactional
          public List&lt;OrderDto&gt; findFilSortOrders(Map&lt;String, String&gt; filters, String sortedColumn, boolean descending, int startRow, int rowsPerPage) {
              return orderRepository.findFilSortOrders(filters, sortedColumn, descending, startRow, rowsPerPage)
                      .stream().map(orderDtoAssembler::mergeAggregateIntoDto).collect(Collectors.toList());
          }
      }
  </code></pre>
  <p>And Controller has several mapping and the one I want to test.</p>
  <p>OrderController :</p>
  <pre><code>  @Controller
      public class OrderController {
          private static final Logger LOGGER = LogManager.getLogger(OrderController.class);
          private final CarCategoryService carCategoryService;
          private final TaxiServiceMakeOrder serviceMakeOrder;
          private final OrderService orderService;
          private final UserService userService;
      
          @Autowired
          public OrderController(CarCategoryService carCategoryService, TaxiServiceMakeOrder serviceMakeOrder, OrderService orderService, UserService userService) {
              this.carCategoryService = carCategoryService;
              this.serviceMakeOrder = serviceMakeOrder;
              this.orderService = orderService;
              this.userService = userService;
          }
      @GetMapping(&quot;/admin/ordersJson&quot;)
          @ResponseStatus(HttpStatus.OK)
          @ResponseBody
          public List&lt;OrderDto&gt; getOrders(@RequestParam String sortBy,
                                       @RequestParam String filter,
                                       @RequestParam boolean descending,
                                       @RequestParam int startRow,
                                       @RequestParam int rowsPerPage) throws JsonProcessingException {
              LOGGER.info(&quot;OrdersGetAction is invoked&quot;);
              ObjectMapper mapper = new ObjectMapper();
              Map&lt;String, String&gt; filters = null;
              if (!filter.equals(&quot;{}&quot;)) {
                  filters = mapper.readValue(filter, new TypeReference&lt;Map&lt;String, String&gt;&gt;() {
                  });
              }
              List&lt;OrderDto&gt; getOrders = orderService.findFilSortOrders(filters, sortBy, descending, startRow, rowsPerPage);
                  System.out.println(&quot;Controller :&quot;+getOrders.size());;//to check If it works
              LOGGER.info(&quot;Filtered and Sorted Order count is {}&quot;, getOrders.size());
              return getOrders;
          }
  
  }
  </code></pre>
  <p>My test util to generate dto :</p>
  <pre><code> public class OrderDtoUtil {
          public static List&lt;OrderDto&gt; generateOrderDto(int count) {
      
              return IntStream.range(0, count)
                      .mapToObj(i -&gt; {
                          OrderDto orderDto = new OrderDto();
                          orderDto.setOrderId((long) i);
                          orderDto.setOrderDate(LocalDateTime.now());
                          orderDto.setOrderCost(500);
                          orderDto.setUserDestination(&quot;UserDestination&quot;);
                          orderDto.setUserAddress(&quot;UserAddress&quot;);
                          orderDto.setUserId((long) i);
                          orderDto.setCarId((long) i);
                          return orderDto;
                      }).collect(Collectors.toList());
      
          }
  }
  </code></pre>
  <p>My test class for controller :</p>
  <pre><code>class OrderControllerTest {
      private CarCategoryService carCategoryService;
      private TaxiServiceMakeOrder serviceMakeOrder;
      private OrderService orderService;
      private UserService userService;
      private OrderController orderController;
      private MockMvc mockMvc;
  
      @BeforeEach
      public void setUp() {
          carCategoryService = mock(CarCategoryService.class);
          serviceMakeOrder = mock(TaxiServiceMakeOrder.class);
          orderService = mock(OrderService.class);
          userService = mock(UserService.class);
          orderController = new OrderController(carCategoryService, serviceMakeOrder, orderService, userService);
          mockMvc = standaloneSetup(orderController)
                  .defaultRequest(get(&quot;/&quot;)
                          .contextPath(&quot;/petProject_war_exploded&quot;)
                          .accept(MediaType.ALL)).build();
      }
  
      @Test
      void getOrders() throws Exception {
          List&lt;OrderDto&gt; orderDtos = OrderDtoUtil.generateOrderDto(3);
          System.out.println(&quot;TEST method :&quot;+orderDtos.size());
          when(orderService.findFilSortOrders(anyMap(), anyString(), anyBoolean(), anyInt(), anyInt())).thenReturn(orderDtos);
          mockMvc.perform(get(&quot;/petProject_war_exploded/admin/ordersJson&quot;)
                          .param(&quot;filter&quot;, &quot;{}&quot;)
                          .param(&quot;sortBy&quot;, &quot;orderId&quot;)
                          .param(&quot;descending&quot;, &quot;false&quot;)
                          .param(&quot;startRow&quot;, &quot;0&quot;)
                          .param(&quot;rowsPerPage&quot;, &quot;3&quot;))
                  .andExpect(status().isOk())
                  .andExpect(jsonPath(&quot;$&quot;, hasSize(3)))
                  .andExpect(jsonPath(&quot;$[0].orderId&quot;, is(orderDtos.get(0).getOrderId())))
                  .andExpect(jsonPath(&quot;$[1].orderId&quot;, is(orderDtos.get(1).getOrderId())))
                  .andExpect(jsonPath(&quot;$[2].orderId&quot;, is(orderDtos.get(2).getOrderId())));
  
      }
  }
  </code></pre>
  <p>And It fails on hasSize method because It shows that size is 0.
  Result :</p>
  <p><a href="https://i.stack.imgur.com/7lcDI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7lcDI.png" alt="enter image description here" /></a></p>
  <p>Can you explain why does it happen?</p>