Whenever I want to test a class which uses resource injection I end up including a constructor that will only be used within the test:
public class A {
According to this article (Mockito and Dependency Injection), Mockito has support for injecting mocked resources.
public class ATest
{
@InjectMocks
private A a; //this is your class under test into which the mocks will be injected.
@Mock
private B b; //this is the EJB to be injected.
@Before
public void setUp()
{
MockitoAnnotations.initMocks(this);
}
}
You can also inject multiple mocks. Just declare them in the same way as we did for B b. The initMocks part can also be done in each test or in a BeforeClass setup method depending on your needs.