oops
oops

Reputation: 39

Mock test method with the return value

How to write a mock test for method with return value(a class instance)

userService

public User getUserInfo(String userId) {
        RestTemplate restTemplate = new RestTemplate();
        String url = http:127.0.0.1 + userId ;
        return restTemplate.getForObject(url, User.class);
}

public class TimUser {
    private String id = null;
    private String userName = null;
    **********GET & SET method***********;
}

 @Mock
private userService userService;
private String userId;


@Before
public void set_up(){
    MockitoAnnotations.initMocks(this);
    userId = "";
}

@Test
public void getUserInfo(){

    userService.getUserInfo(userId)

}

I don't know how to write a test case for this kind of method, can anyone provide some ideas?

Upvotes: 1

Views: 559

Answers (2)

Rowan
Rowan

Reputation: 56

Mockito.when(userService.getUserInfo(userId)).thenReturn(value);

Not exactly sure what you are trying to achieve here though. If the class you are testing is UserService then you should not be mocking it. You should be calling getUserInfo in your test and asserting the return value is the same as your expectation.

Upvotes: 2

Blokje5
Blokje5

Reputation: 5003

You can use when(mock.method).thenReturn(value) from mockito.

when(userservice.getUserInfo(eq(this.userId)).thenReturn(<your value here>)

Upvotes: 1

Related Questions