javajakarta-eemockitoejbjboss-arquillian

Why no more mocks in Arquillian?


If you go to Arquillian's home page it says: No more mocks., but if I have EJB 1 that injects EJB 2 and I want to test EJB 1, how do I simulate EJB 2 if I don't mock it? Is Arquillian used for integration and not unit testing? Why is it a bad practice to mock in Arquillian and what's the alternative?


Solution

  • Arquillian is indeed used for integration testing and not unit testing.

    It's somewhat of a bad practice to mock extensively in Arquillian, exactly for this reason. It largely defeats the purpose of using Arquillian in the first place.

    That said, mocking in Arquillian for the situation you described is reasonably easy. As the default mode in Arquillian is for you to provide micro archives build with shrinkwrap, you can simply replace EJB2 with a mock version when creating your archive.

    For instance:

    @RunWith(Arquillian.class)
    public class MyTest {
    
        @Deployment
        public static Archive<?> deployment() {
            return ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addClass(com.real.EJB1.class)
                .addClass(com.example.mock.EJB2.class)
            ;
        }
    
        // ..
    }
    

    Since EJB1 would not depend on the actual class type of EJB2 but only on its (simple) name or interface, you can just swap it out when creating your test.