How to only mock a method of a bean in Spring using Mockito?

Viewed 3528

I use @Primary and @Profile to mock a bean in Spring test:

@Profile("test")
@Configuration
public class TestBeanConf {

@Bean
@Primary
public UserService userService() {
    UserService userService = Mockito.mock(UserService.class);
    TokenValidationUrl validation = new TokenValidationUrl();
    validation.setValid(true);
    validation.setUid("123456789");
    Mockito.when(userService.tokenValidation("23456")).thenReturn(validation);
    return userService;
}

But other methods of UserService bean return null, how can i spy the real created bean and only mock tokenValidation method?

1 Answers
Related