Unit Testing with Mocking and Java Config
I am trying to understand how one would use Mocking with Java Config to substitute the actual object.
@Configuration
public class MyConfig {
@Bean
public PaymentService paymentService() {
return new PaymentServiceImpl(auditService());
}
@Bean
public AuditService auditService() {
return new AuditServiceImpl();
}
}
In my unit test, I am hoping to mock out the AuditService like so:
class OrderServiceTest {
private AuditService auditServiceMock;
private ApplicationContext ctx;
@Before
public void setUp() {
auditServiceMock = Mockito.mock(AuditService.class);
ctx = new AnnotationApplicationContext(MyConfig.class);
// What I need to do is ctx.set(auditServiceMock);
}
@Test
public void ordersericeTest() {
Mockito.when(auditServiceMock.doSomething(..)).the nReturn(somethingElse);
OrderService s = ctx.get(OrderService.class);
s...
}
How would I tell the Context to use my mock object rather than the actual AuditServiceImpl. I do not want to use XML Config here but JavaConfig.
Thanks in advance for any help.