简体   繁体   中英

Arquillian EJB adding mockito

I am trying to add mockito to my arquillian tests (with ShrinkWrap), like so:

    @Deployment
    public static Archive<?> createDeployment() {
        JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addPackage(BeanClass.class.getPackage())
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
                ;

        Archive[] libs = Maven.resolver()
                .loadPomFromFile("pom.xml")
                .resolve(
                        "org.mockito:mockito-all"
                )
                .withTransitivity()
                .as(JavaArchive.class);

        for (Archive lib : libs) {
            archive = archive.merge(lib);
        }

        return archive;
    }

I am using Mockito to overwrite with @Alternative . But when I add the line archive = archive.merge(lib) I am getting the Exception:

Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Task

Or I will get

Caused by: java.lang.ClassNotFoundException: org.mockito.asm.signature.SignatureVisitor

Has anyone else experienced this too?

UPDATE : Some extra information, I am trying to test this with a wildfly embedded container: pom.xml

<dependencies>
                <dependency>
                    <groupId>org.wildfly</groupId>
                    <artifactId>wildfly-arquillian-container-embedded</artifactId>
                    <scope>test</scope>
                </dependency>

                <dependency>
                    <groupId>org.wildfly</groupId>
                    <artifactId>wildfly-embedded</artifactId>
                    <scope>test</scope>
                </dependency>

                <dependency>
                    <groupId>org.jboss.arquillian.extension</groupId>
                    <artifactId>arquillian-transaction-jta</artifactId>
                    <scope>test</scope>
                </dependency>
            </dependencies>

I finally found the solution that I have needed. I have found a solution by including ant dependency. The problems started when I needed to use other test libraries like cucumber. I am now testing with an EAR deployment which has resolved my problems:

    @Deployment
    public static Archive<?> createDeployment() {
        final JavaArchive ejbJar = ShrinkWrap
                .create(JavaArchive.class, "ejb-jar.jar")
                .addClass(NewSessionBean.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

        final WebArchive testWar = ShrinkWrap.create(WebArchive.class, "test.war")
                .addClass(NewSessionBeanTest.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")

                ;

        Archive[] libsArchives = Maven.resolver()
                .loadPomFromFile("pom.xml")
                .resolve("org.mockito:mockito-all")
                .withTransitivity()
                .as(JavaArchive.class);

        testWar.addAsLibraries(libsArchives);
        final EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class)
                .setApplicationXML("META-INF/test-application.xml")
                .addAsModule(ejbJar)
                .addAsModule(testWar);

        return ear;
    }

And my test-application.xml

<application>
    <display-name>ear</display-name>
    <module>
        <ejb>ejb-jar.jar</ejb>
    </module>
    <module>
        <web>
            <web-uri>test.war</web-uri>
            <context-root>/test</context-root>
        </web>
    </module>
</application>

You cannot use Mockito with statics or finals.

You will need to make use of PowerMock to do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM