By Magdalena Rumakowicz


2018-06-13 14:15:01 8 Comments

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 comments

@marco 2018-06-13 14:21:08

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);

@Magdalena Rumakowicz 2018-06-13 14:23:09

Thank's taht what i Want!!

@Adil B 2018-06-13 14:19:42

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.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] How to determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1560570 View
  • 1892 Score
  • 25 Answer
  • Tags:   java arrays

17 Answered Questions

[SOLVED] How slow are Java exceptions?

60 Answered Questions

[SOLVED] Avoiding != null statements

32 Answered Questions

13 Answered Questions

[SOLVED] What could cause java.lang.reflect.InvocationTargetException?

7 Answered Questions

[SOLVED] What's the difference between faking, mocking, and stubbing?

44 Answered Questions

32 Answered Questions

[SOLVED] The case against checked exceptions

1 Answered Questions

[SOLVED] Mockito test a void method throws an exception

26 Answered Questions

[SOLVED] Should I test private methods or only public ones?

Sponsored Content