2 Replies Latest reply on Nov 1, 2013 7:36 PM by Michael Yakobi

    How to inject mocks in an Arquillian test?

    Stefan Miklosovic Novice

      Hi there,

       

      back in the day, I was trying to test some very basic EJB service which used DAO which in turn used some basic JPA entity. I used glassfish embedded server. Honestly, the inspiration was taken from this example: http://arquillian.org/guides/testing_java_persistence/ but I just created ShrinkWrap archive with all packages and modified somehow that project to my needs. Up to this point, all is good.

       

      The "normal" testing was smooth but I wanted to used Mockito - mock EJB service. So I added Mockito library to the ShrinkWrap archive by shrinkwrap resolver as posted here https://community.jboss.org/message/627546#627546 or something very similar.

       

      Then I wanted to mock the service. First of all, I can't use annotations as @Mock or @InjectMocks on my service and DAO in the test class because it is somehow useless since I guess I can not run the test with MockitoJUnitRunner ... hmm. So I was about to mock it manually. Something like the guy in the posted link did. He just did

       

      mock(List.class)

       

      But my case is another one. After mocking my service, which uses @EJB myDAO, I really guess dao will not be mocked as well, will be? (the @InjectMocks annotation does this automagically) So I have to mock it manually too. But how?

       

      Sorry if I am really missing something here. I would like to know what approach to take in order to make it right.

        • 1. Re: How to inject mocks in an Arquillian test?
          Bartosz Majsak Master

          I think this should help you to some extent https://github.com/arquillian/arquillian-showcase/tree/master/extensions/autodiscover

          With regards to injecting mocked dao to the service - how about using @Inject instead of @EJB and write your own CDI producer (could be within the test class itself) which will create such a mocked repository?

           

          As a side note I'm not really conviced if you really need mocks if you already have concrete implementations for your services and DAOs. This should be more for unit testing rather than integration testing where you anyway use container's infrastructure.

           

          Hope that helps.

           

          Cheers,

          Bartosz

          1 of 1 people found this helpful
          • 2. Re: How to inject mocks in an Arquillian test?
            Michael Yakobi Newbie

            Personally, I rather tests my EJBs as "units" before I get into integration-tests such that are enabled by Arquillian.

            Usually I mock all the dependencies of my EJB thus making it a real unit-test.

             

            This is why I've created testfun-JEE which allows you to inject EJBs directly into your test class and it doesn't require any container.

            testfun-JEE supports injection of EJBs, entity manager and some other common resources (such asJ DBC data source and context) - dependencies in injected EJBs are also being taken care by testfun-JEE.

             

            One of the cool features of testfun-JEE is that if you add a variable member, which is an interface of an EJB, to your test class and this variable is annotated with @Mock (from Mockito), than this mock will replace the EJB everywhere.

             

            See examples in https://github.com/michaelyaakoby/testfun