Arquillian with Mockito and CDI

雨燕双飞 提交于 2019-12-06 09:45:06

问题


Is it possible to create spy(mock) object in testing class?

Here is tested class.

@Stateless
@Slf4j
public class UserDao {

    @Inject
    private TestBean testBean;

    public String mock() {
        return testBean.mock();
    }

    public String notMock() {
        return testBean.notMock();
    }
}

TestBean code

@Stateless
@Slf4j
public class TestBean {
    public String notMock() {
        return "NOT MOCK";
    }

    public String mock() {
        return "IMPLEMENTED MOCK";
    }
}

Here's my test

@RunWith(Arquillian.class)
public class UserDataTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Inject
    private UserDao userDao;

    @Deployment
    protected static Archive createWar() {

        File[] dependencies = Maven.configureResolver()
                .withRemoteRepo("nexus-remote", "http://maven.wideup.net/nexus/content/groups/public/", "default")
                .withRemoteRepo("nexus-release", "http://maven.wideup.net/nexus/content/repositories/releases/", "default")
                .resolve(
                        "org.slf4j:slf4j-simple:1.7.7",
                        "eu.bitwalker:UserAgentUtils:1.15",
                        "org.mockito:mockito-all:1.10.8"
                ).withoutTransitivity().asFile();

        return ShrinkWrap
                .create(WebArchive.class, "pass.jpa.war")
                .addAsWebInfResource("jbossas-ds.xml")
                .addAsWebInfResource("jboss-deployment-structure.xml")
                .addAsLibraries(
                        PassApiDeployments.createDefaultDeployment(),
                        PassUtilLibrary.createDefaultDeployment(),
                        PassJpaDeployments.createDefaultDeployment()
                ).addAsLibraries(dependencies);
    }

    @Test
    public void testMock() {
        assertEquals("MOCK", userDao.mock());
    }

    @Test
    public void testNotMock() {
        assertEquals("NOT MOCK", userDao.notMock());
    }
}

I'd like to create a spy object on TestBean to change result on method test() of this bean.

So is it possible to create TestBean spy in UserDao.

I solve some problems through producer like this.

@Singleton
public class MockFactory {
    @Produces
    @ArquillianAlternative
    public TestBean getTestBean() {
        return when(mock(TestBean.class).mock()).thenReturn("MOCK").getMock();
    }
}

But in this example I need create on Bean completely on my own. And if it is bean with additional dependencies and thus i will manage all dependencies.


回答1:


As far as I know, its not possible to use a mocking framework in combination with arquillian ...




回答2:


I haven't used it myself, but this Arquillian extension seems to be specifically designed to support Mockito Spy objects in an Arquillian test: https://github.com/topikachu/arquillian-extension-mockito/



来源:https://stackoverflow.com/questions/31267717/arquillian-with-mockito-and-cdi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!