an example for a simple cdi-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
@RequestScoped | |
public class MyCdiBean | |
{ | |
private int count = 0; | |
public int getCount() | |
{ | |
return count; | |
} | |
public void increaseCount() | |
{ | |
count++; | |
} | |
} |
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
@RunWith(CdiTestRunner.class) | |
public class ManuallyMockedCdiBeanTest | |
{ | |
@Inject | |
private MyCdiBean myCdiBean; | |
@Inject | |
private DynamicMockContext mockContext; | |
@Test | |
public void manualMock() | |
{ | |
mockContext.addMock(new MyCdiBean() { | |
@Override | |
public int getCount() { | |
return 14; | |
} | |
}); | |
Assert.assertEquals(14, myCdiBean.getCount()); | |
myCdiBean.increaseCount(); //no effect due to the mock | |
Assert.assertEquals(14, myCdiBean.getCount()); | |
} | |
} |
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
@RunWith(CdiTestRunner.class) | |
public class MockitoMockedCdiBeanTest | |
{ | |
@Inject | |
private MyCdiBean myCdiBean; | |
@Inject | |
private DynamicMockContext mockContext; | |
@Test | |
public void mockitoMock() | |
{ | |
MyCdiBean mockedCdiBean = mock(MyCdiBean.class); | |
when(mockedCdiBean.getCount()).thenReturn(14); | |
mockContext.addMock(mockedCdiBean); | |
Assert.assertEquals(14, this.myCdiBean.getCount()); | |
myCdiBean.increaseCount(); //no effect due to the mock | |
Assert.assertEquals(14, this.myCdiBean.getCount()); | |
} | |
} |
the add-on is available here. the repository also contains several tests, which illustrate the supported use-cases. the tests use manual-mocks as well as mockito-mocks. however, you can provide any object as mock which is a subclass of the original implementation.