Arquillian:依赖注入不起作用

发布于 2025-02-11 13:57:49 字数 4683 浏览 0 评论 0原文

我正在尝试使用Arquillian在远程Wildfly服务器上运行测试。我不确定为什么依赖注入不起作用。这是POM的相关部分:

        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <version>1.7.0.Alpha10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.protocol</groupId>
            <artifactId>arquillian-protocol-servlet</artifactId>
            <version>1.6.0.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
<profile>
            <!-- Run with: mvn clean test -Parq-remote -->
            <id>arq-remote</id>
            <dependencies>
                <dependency>
                    <groupId>org.wildfly.arquillian</groupId>
                    <artifactId>wildfly-arquillian-container-remote</artifactId>
                    <version>5.0.0.Alpha3</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </profile>

我的测试类:

import javax.inject.Inject;
import javax.inject.Named;

import com.avm.service.ITestService;
import com.avm.service.TestService;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class OrderServiceTest {

        @Deployment
        public static Archive<?> createTestArchive() {
            return ShrinkWrap.create(WebArchive.class, "backend.war")
                    .addClass(ITestService.class)
                    .addClass(TestService.class)
                    //.addPackage(RepositoryManager.class.getPackage()).addAsResource("META-INF/persistence.xml")
                    .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
        }

        @Inject
        @Named("testService")
        TestService testService;

        @Test
        public void testRegister() throws Exception {
            Assert.assertEquals("backend", testService.getAppName());
        }
    }

服务界面:

public interface ITestService {
    String getAppName();
}

服务实现:

@Named("testService")
public class TestService implements ITestService{

    private String appName = "backend";
    private String version = "1.0.0";

    @Override
    public String getAppName() {
        return this.appName;
    }
}

最后,我的arquillian.xml:

<arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://jboss.org/schema/arquillian
    http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <!-- Force the use of the Servlet 3.0 protocol with all containers, as it is the most mature -->
    <defaultProtocol type="Servlet 3.0" />

    <!-- Uncomment to have test archives exported to the file system for inspection -->
    <engine>
        <property name="deploymentExportPath">target/</property>
    </engine>

    <!-- Example configuration for a managed WildFly / JBoss EAP instance -->
    <container qualifier="managed">
        <configuration>
            <property name="jbossHome">/git/Servers/wildfly-26.1.1.Final</property>
        </configuration>
    </container>

    <!-- Example configuration for a remote WildFly / JBoss EAP instance -->
    <container qualifier="remote">
        <!-- Arquillian will deploy to this WildFly server. -->
        <configuration>
            <property name="managementAddress">127.0.0.1</property>
            <property name="managementPort">9990</property>

        </configuration>
    </container>
</arquillian>

在寻找解决方案大约两天后,尝试了几乎所有我能做的一切,我觉得我真的很感激如果一个善良的灵魂现在看这个问题。

感谢您的阅读,感谢您的时间。

I am trying to use Arquillian to run tests on a remote Wildfly server. I am not sure why the Dependency Injection does not work. Here is the relevant section from POM:

        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <version>1.7.0.Alpha10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.protocol</groupId>
            <artifactId>arquillian-protocol-servlet</artifactId>
            <version>1.6.0.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
<profile>
            <!-- Run with: mvn clean test -Parq-remote -->
            <id>arq-remote</id>
            <dependencies>
                <dependency>
                    <groupId>org.wildfly.arquillian</groupId>
                    <artifactId>wildfly-arquillian-container-remote</artifactId>
                    <version>5.0.0.Alpha3</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </profile>

My test class:

import javax.inject.Inject;
import javax.inject.Named;

import com.avm.service.ITestService;
import com.avm.service.TestService;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class OrderServiceTest {

        @Deployment
        public static Archive<?> createTestArchive() {
            return ShrinkWrap.create(WebArchive.class, "backend.war")
                    .addClass(ITestService.class)
                    .addClass(TestService.class)
                    //.addPackage(RepositoryManager.class.getPackage()).addAsResource("META-INF/persistence.xml")
                    .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
        }

        @Inject
        @Named("testService")
        TestService testService;

        @Test
        public void testRegister() throws Exception {
            Assert.assertEquals("backend", testService.getAppName());
        }
    }

Service Interface:

public interface ITestService {
    String getAppName();
}

Service Implementation:

@Named("testService")
public class TestService implements ITestService{

    private String appName = "backend";
    private String version = "1.0.0";

    @Override
    public String getAppName() {
        return this.appName;
    }
}

Finally, my arquillian.xml:

<arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://jboss.org/schema/arquillian
    http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <!-- Force the use of the Servlet 3.0 protocol with all containers, as it is the most mature -->
    <defaultProtocol type="Servlet 3.0" />

    <!-- Uncomment to have test archives exported to the file system for inspection -->
    <engine>
        <property name="deploymentExportPath">target/</property>
    </engine>

    <!-- Example configuration for a managed WildFly / JBoss EAP instance -->
    <container qualifier="managed">
        <configuration>
            <property name="jbossHome">/git/Servers/wildfly-26.1.1.Final</property>
        </configuration>
    </container>

    <!-- Example configuration for a remote WildFly / JBoss EAP instance -->
    <container qualifier="remote">
        <!-- Arquillian will deploy to this WildFly server. -->
        <configuration>
            <property name="managementAddress">127.0.0.1</property>
            <property name="managementPort">9990</property>

        </configuration>
    </container>
</arquillian>

After hunting down a solution for about two days now and having tried almost anything which I could, I feel I would really appreciate if a kind soul looked at this problem now.

Thanks for reading, appreciate your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文