1

Is there any valid variant to test controller with the predicate in it?

@RestController
public class QueryLauncherController {
    private QueryLauncherService queryLauncherService;

    @Autowired
    public QueryLauncherController(QueryLauncherService queryLauncherService) {
        this.queryLauncherService = queryLauncherService;
    }

    @GetMapping("/queryLauncher/CMP")
    public List<QueryLauncherDto> getLaunchersCompany(RTAdmin admin) {
        return queryLauncherService.getLaunchersList(admin, QueryLauncher::getIsCompany);
    }
}

I've tried to solve this issue using any(Predicate.class) but IDE highlights it as Unchecked assignment and it looks pretty dumb. The code I've written for the test is:

    public void returnCompanyLinks_When_companyRequested() throws Exception {
        when(queryLauncherService.getLaunchersList(eq(user), any(Predicate.class))).thenReturn(queryLauncherDtos);

        mockMvc.perform(get(QUERY_LAUNCHER_URL + CMP).session(mockSession))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(content().json(new Gson().toJson(queryLauncherDtos)));
    }

The goal is to run method only in the case when QueryLauncher::getIsInvestor.

List<QueryLauncherDto> getLaunchersList(RTAdmin admin, Predicate<QueryLauncher> launcherType);

Are there any variants to do it?

3
  • 1
    Hi Oleksandr! I'm afraid that the only way to test it if your predicates returns different Objects.
    – Naya
    Commented Jan 28, 2019 at 14:27
  • Show the method signature for queryLauncherService.getLaunchersList
    – DwB
    Commented Jan 28, 2019 at 14:51
  • @DwB List<QueryLauncherDto> getLaunchersList(RTAdmin admin, Predicate<QueryLauncher> launcherType); Commented Jan 28, 2019 at 15:39

1 Answer 1

2

I believe this will do what you want:

doReturn(queryLauncherDtos).when(queryLauncherService).getLaunchersList(eq(user),
  ArgumentMatchers.<Predicate<QueryLauncher>>anyObject());

Some caveats:

  1. I prefer the doReturn(value).when(mock).method form when specifying mocked functionality, but it is easy enough to flip to the when(mock.method).thenReturn(value) form.
  2. anyObject is deprecated in the latest version of Mockito. It is still present, but it is deprecated.

Edit: I read the 2.23 release Mockito documentation, and it mentions that anyObject is just an alias for the any method.

The following should work and is not deprecated:

doReturn(queryLauncherDtos).when(queryLauncherService).
  getLaunchersList(eq(user),
  ArgumentMatchers.<Predicate<QueryLauncher>>any());
1
  • Thanks for the answer. I already found solution with anyObject() usage. But the problem is that I cannot use deprecated methods due to project restrictions. Commented Jan 29, 2019 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.