Garmaine Staff asked 2 years ago

I have a JavaEE application running on Wildfly. The application has Stateless EJBs that I use for the business logic and database access.

For example, I have EjbDAO injected in EjbBusiness, like so:

@Stateless
@LocalBean
public class EjbBusiness {

    @Inject
    private EjbDAO dao;

    // ..........
}

Now, I want to test EjbBusiness with JUnit/Mockito. The problem is that the EjbDAO is null when I run the test. So I tried to add to the test class:

@InjectMocks
private EjbDAO dao = mock(EjbDAO.class);  

And this doesn't work either. It doesn't seem right to declare with @InjectMocks a class that I actually don't want to mock, as I want it to work as the real class, but I couldn't find another way to declare EjbDAO in the test.

Question is: why does EjbDAO injection return null? Do I need to tell my tests class that EjbDAO is injected in EjbBusiness is I don't want to change its behavior?