Last active
May 30, 2018 15:44
-
-
Save odrotbohm/979d42f161123af7d3fca2d7bcc4a335 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class MockPostProcessor implements BeanPostProcessor { | |
/* | |
* (non-Javadoc) | |
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String) | |
*/ | |
@Override | |
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | |
if (!SimpleUserRepository.class.isInstance(bean)) { | |
return bean; | |
} | |
return Mockito.mock(SimpleUserRepository.class, AdditionalAnswers.delegatesTo(bean)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
@Test | |
public void testname() { | |
// Calls real method | |
repository.save(new User()); | |
// Stubs method call | |
when(repository.findAll()).thenReturn(Arrays.asList()); | |
// Assert on stubbed method | |
assertThat(repository.findAll()).isEmpty(); | |
// Verify method called | |
verify(repository).save(any()); | |
verify(repository).findAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment