本文介绍了Arquillian与Mockito和CDI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在测试类中创建spy(mock)对象?

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代码

TestBean code

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

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

这是我的考试

@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());
    }
}

我想在TestBean上创建一个间谍对象,以更改此bean的方法test()的结果.

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

因此可以在UserDao中创建TestBean间谍.

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();
    }
}

但是在这个例子中,我需要完全在Bean上自行创建.如果它是具有其他依赖项的bean,那么我将管理所有依赖项.

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.

推荐答案

我自己没有使用过,但是这个Arquillian扩展似乎专门设计用于支持Arquillian测试中的Mockito Spy对象:"> https://github.com/topikachu/arquillian-extension-mockito/

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/

这篇关于Arquillian与Mockito和CDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 04:51