0

How should I correctly return element in my test method. I have problem in my test.

when(teamService.createTeam(teamDto)).thenReturn();

In this line i don't know how to wrote correctly return statement.What should be in bracket after thenReturn. My method cretate team look like this:

@Transactional
    public Team createTeam(TeamDto teamDto) {
        Assert.notNull(teamDto, "Object can't be null!");
        try {
            Assert.notNull(teamDto.getName());
            return teamRepository.save(modelMapper.map(teamDto, Team.class));
        } catch (Exception e) {
            throw new CreateEntityException(e);
        }
    }

And in this method i return Team object but when i add Team i have expression expected.

2 Answers2

3

depending on what you want to do, you could either do:

when(teamService.createTeam(teamDto)).thenReturn(new Team());

or

Team team = mock(Team.class);
when(teamService.createTeam(teamDto)).thenReturn(team);

PS: I am assuming that your class under test is NOT the one containing the method you have posted. I am assuming that you are mocking that service, so somewhere, before, you should have written something like:

TeamService teamService = mock(TeamService.class);
marco
  • 671
  • 6
  • 22
1

Since you're using a mocking framework to mock your TeamService class, you should create a mock Team object with dummy values and return it in the thenReturn() clause.

Adil B
  • 14,635
  • 11
  • 60
  • 78