'MockMVC - Service method does not appeared to be called, works in postman, responding with null for test

I am trying to test the controller for creating a new league method, but it just returns null, and when I put a system.out.println in the service impl method, it doesn't print anything.

I have tried this with both LeagueService and LeagueServiceImpl, but neither works. It seems to me as if the league service is not being called.

LeagueControllerTest:

@AutoConfigureMockMvc
@WebMvcTest(controllers = LeaguesController.class)
public class LeagueControllerTests {


    @Autowired
    MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @MockBean
    LeagueServiceImpl leagueService;


    @Test
    public void addLeague() throws Exception {
        LeagueDTO leagueDTO = new LeagueDTO("The magnificent 7", LeagueType.BRONZE.leagueType(),7);
        String json = objectMapper.writeValueAsString(leagueDTO);

        System.out.println(leagueService.addLeagueFromJSON(leagueDTO));

        when(leagueService.addLeagueFromJSON(leagueDTO)).thenReturn("Success.");

        MvcResult mvcRes = mockMvc.perform(post("/leagues/add")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(json))
                .andExpect(status().isOk())
                .andReturn();

        assertEquals("{\"response\":\"Success\"}", mvcRes.getResponse().getContentAsString());
    }

LeagueController:

@RestController
@RequestMapping("/leagues")
public class LeaguesController {


    private final LeagueService leagueService;

    public LeaguesController(LeagueService leagueService) {
        this.leagueService = leagueService;
    }

    @PostMapping(value = "/add", consumes = "application/json", produces = "application/json")
    ResponseEntity<?> addLeague(@RequestBody final LeagueDTO leagueDTO) {
        String response = leagueService.addLeagueFromJSON(leagueDTO);
        System.out.println("MSG IN CONTROLLER: " + response);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }
}

LeagueDTO:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class LeagueDTO {

    private String leagueName;

    private String leagueType;

    private Integer playerLimit;

}

League:

@Entity
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "leagues")
public class League {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "leagueid", unique = true)
    private Long leagueID;

    @Column(name = "leaguename")
    private String leagueName;

    @Column(name = "leaguetype")
    private String leagueType;

    @Column(name = "playerlimit")
    private Integer playerLimit;

    @Column(name = "gamesToPlay")
    private Integer gamesToPlay;

    @Column(name = "playedGames")
    private Integer playedGames;

    @Column(name = "firstplaceuserid")
    private Long firstPlaceUserID;
}

LeagueServiceImpl:

@Override
    public String addLeagueFromJSON(LeagueDTO leagueDTO) {
        Integer playerLimit = leagueDTO.getPlayerLimit();
        String leagueName = leagueDTO.getLeagueName();

        if (leagueName != null && playerLimit != null && playerLimit > 4) {
            League league = new League(leagueName, leagueDTO.getLeagueType(), playerLimit);
            leagueRepository.save(league);
            return "Successfully created league.";
        }
        return "Failed to create league.";
    }

League Service:

public interface LeagueService {


    String addLeagueFromJSON(LeagueDTO leagueDTO);


}

Output from test:

null
MSG IN CONTROLLER: null

expected: <{"response":"Success"}> but was: <>
Expected :{"response":"Success"}
Actual   :

This works in Postman and says "Success" or "Failure" and I can see the changes in DB.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source