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

8 Answered Questions

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

32 Answered Questions

[SOLVED] How do you assert that a certain exception is thrown in JUnit 4 tests?

27 Answered Questions

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

17 Answered Questions

[SOLVED] How slow are Java exceptions?

2 Answered Questions

[SOLVED] Mockito test a void method throws an exception

60 Answered Questions

[SOLVED] Avoiding != null statements

45 Answered Questions

13 Answered Questions

[SOLVED] Google Gson - deserialize list<class> object? (generic type)

13 Answered Questions

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

32 Answered Questions

[SOLVED] The case against checked exceptions

Sponsored Content