Arquillian bean 和 dao @Injection 不起作用
Arquillian bean and dao @Injection don't work
我正在编写 wildfly Java EE 应用程序,但我在测试用例中遇到 bean 注入问题。它在独立测试和部署的 ShrinkWrap 中不起作用。
import org.jboss.arquillian.junit.Arquillian;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.inject.Inject;
...
@RunWith(Arquillian.class)
public class InjectionTestCase {
@org.jboss.arquillian.core.api.annotation.Inject
private ProjectStatusDao proStatusDao;
@Inject
private ClientsCtrl clients;
@Test
public void groupSetter() {
ManageUser user = new ManageUser();
String g = "group";
user.setGroup(g);
Assert.assertEquals(g, user.getGroup());
}
}
简单测试,但两个注入的对象都是空的。我尝试从 org.jboss.arquillian.core.api.annotation.Inject 包注入但效果相同。
你能帮帮我吗?我不知道该怎么办。
src/test/resources/arquillian.xml
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://www.jboss.org/arquillian-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.org/arquillian-1.0
http://www.jboss.org/schema/arquillian/arquillian-1.0.xsd">
<defaultProtocol type="Servlet 3.0" />
<extension qualifier="webdriver">
<property name="browser">chrome</property>
</extension>
<container qualifier="widlfly-remote" default="true">
<configuration>
<property name="managementAddress">host</property>
<property name="managementPort">9990</property>
<property name="username">user</property>
<property name="password">password</property>
</configuration>
</container>
</arquillian>
执行日志
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.drone.webdriver.factory.remote.reusable.ReusedSessionPermanentFileStorage readStore
INFO: Reused session store is not available at C:\Users\......\.drone-webdriver-session-store, a new one will be created.
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
新日志。使用 ShrinkWrap:
public static WebArchive createDeployment() {
File[] files = Maven.resolver().loadPomFromFile("pom.xml")
.importRuntimeDependencies().resolve().withTransitivity().asFile();
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
// add classes
.addPackages(true, "my.package")
// add configuration
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource(new File("src/test/webapp/WEB-INF/jboss-web.xml"))
// add pages
.addAsWebResource(new File("src/main/webapp/403.xhtml"))
.addAsWebResource(new File("src/main/webapp/404.xhtml"))
.addAsWebResource(new File("src/main/webapp/error.xhtml"))
.addAsWebResource(new File("src/main/webapp/login.xhtml"))
/** OTHER PAGES ADDED **/
// add libraries
.addAsLibraries(files)
.setWebXML(new File("src/test/webapp/WEB-INF/web.xml"));
System.err.println(war.toString(true));
return war;
}
@Deployment
public static Archive<?> createDeployment() {
return createDeployment();
}
我发现了一个问题。测试正在尝试连接 0.0.0.0 而不是我的外部 wildfly 服务器 "host"
Caused by: java.lang.IllegalStateException: Error launching request at http://0.0.0.0:8080/test/ArquillianServletRunner?outputMode=serializedObject&className=my.package.InjectionTestCase&methodName=groupSetter. No result returned
在服务器上我看到应用程序部署成功
2016-04-15 11:20:55,986 INFO [org.jboss.as.server.deployment] (MSC service thread 1-14) JBAS015876: Starting deployment of "test.war" (runtime-name: "test.war")
......
2016-04-15 11:21:00,884 INFO [org.jboss.as.server] (management-handler-thread - 1) JBAS018559: Deployed "test.war" (runtime-name : "test.war")
你必须告诉 Arquillian,你想在服务器上部署你的测试用例:
简单示例(没有 addPackages() 等)
@Deployment
public static Archive<?> createDeployment() {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class).addAsManifestResource(EmptyAsset.INSTANCE,
"beans.xml");
return archive;
}
根据您的项目,您可能包括一些额外的部署操作:[=15=]
EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, "TestProject.ear")
.addAsModule(archive);
archive.addAsResource("resources/MANIFEST.MF", "META-INF/MANIFEST.MF");
archive.addAsResource("resources/beans.xml", "META-INF/beans.xml");
archive.addAsResource("resources/persistence.xml", "META-INF/persistence.xml");
然后在服务器上,整个依赖注入应该工作,或者至少测试部署应该失败并显示适当的消息
编辑:
关于
Error launching request at.....
错误:
据此:
https://docs.jboss.org/author/display/ARQ/Container+configuration
尝试添加
<property name="host">yourhost</property>
<property name="port">8181</property>
到你arquillian.xml
中的配置
确保导入正确的注解:
org.jboss.arquillian.container.test.api.Deployment
而不是这个:
org.jboss.arquillian.api.Deployment;
第二个不会被拾取,报同样的错误
there is no running deployment yet. Please use the annotation @RunAsClient
我正在编写 wildfly Java EE 应用程序,但我在测试用例中遇到 bean 注入问题。它在独立测试和部署的 ShrinkWrap 中不起作用。
import org.jboss.arquillian.junit.Arquillian;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.inject.Inject;
...
@RunWith(Arquillian.class)
public class InjectionTestCase {
@org.jboss.arquillian.core.api.annotation.Inject
private ProjectStatusDao proStatusDao;
@Inject
private ClientsCtrl clients;
@Test
public void groupSetter() {
ManageUser user = new ManageUser();
String g = "group";
user.setGroup(g);
Assert.assertEquals(g, user.getGroup());
}
}
简单测试,但两个注入的对象都是空的。我尝试从 org.jboss.arquillian.core.api.annotation.Inject 包注入但效果相同。
你能帮帮我吗?我不知道该怎么办。
src/test/resources/arquillian.xml
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://www.jboss.org/arquillian-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.org/arquillian-1.0
http://www.jboss.org/schema/arquillian/arquillian-1.0.xsd">
<defaultProtocol type="Servlet 3.0" />
<extension qualifier="webdriver">
<property name="browser">chrome</property>
</extension>
<container qualifier="widlfly-remote" default="true">
<configuration>
<property name="managementAddress">host</property>
<property name="managementPort">9990</property>
<property name="username">user</property>
<property name="password">password</property>
</configuration>
</container>
</arquillian>
执行日志
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.drone.webdriver.factory.remote.reusable.ReusedSessionPermanentFileStorage readStore
INFO: Reused session store is not available at C:\Users\......\.drone-webdriver-session-store, a new one will be created.
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
kwi 14, 2016 8:25:02 AM org.jboss.arquillian.container.test.impl.RunModeUtils isRunAsClient
WARNING: The test method "InjectionTestCase groupSetter" will run on the client side - there is no running deployment yet. Please use the annotation @RunAsClient
新日志。使用 ShrinkWrap:
public static WebArchive createDeployment() {
File[] files = Maven.resolver().loadPomFromFile("pom.xml")
.importRuntimeDependencies().resolve().withTransitivity().asFile();
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
// add classes
.addPackages(true, "my.package")
// add configuration
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource(new File("src/test/webapp/WEB-INF/jboss-web.xml"))
// add pages
.addAsWebResource(new File("src/main/webapp/403.xhtml"))
.addAsWebResource(new File("src/main/webapp/404.xhtml"))
.addAsWebResource(new File("src/main/webapp/error.xhtml"))
.addAsWebResource(new File("src/main/webapp/login.xhtml"))
/** OTHER PAGES ADDED **/
// add libraries
.addAsLibraries(files)
.setWebXML(new File("src/test/webapp/WEB-INF/web.xml"));
System.err.println(war.toString(true));
return war;
}
@Deployment
public static Archive<?> createDeployment() {
return createDeployment();
}
我发现了一个问题。测试正在尝试连接 0.0.0.0 而不是我的外部 wildfly 服务器 "host"
Caused by: java.lang.IllegalStateException: Error launching request at http://0.0.0.0:8080/test/ArquillianServletRunner?outputMode=serializedObject&className=my.package.InjectionTestCase&methodName=groupSetter. No result returned
在服务器上我看到应用程序部署成功
2016-04-15 11:20:55,986 INFO [org.jboss.as.server.deployment] (MSC service thread 1-14) JBAS015876: Starting deployment of "test.war" (runtime-name: "test.war")
......
2016-04-15 11:21:00,884 INFO [org.jboss.as.server] (management-handler-thread - 1) JBAS018559: Deployed "test.war" (runtime-name : "test.war")
你必须告诉 Arquillian,你想在服务器上部署你的测试用例:
简单示例(没有 addPackages() 等)
@Deployment
public static Archive<?> createDeployment() {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class).addAsManifestResource(EmptyAsset.INSTANCE,
"beans.xml");
return archive;
}
根据您的项目,您可能包括一些额外的部署操作:[=15=]
EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, "TestProject.ear")
.addAsModule(archive);
archive.addAsResource("resources/MANIFEST.MF", "META-INF/MANIFEST.MF");
archive.addAsResource("resources/beans.xml", "META-INF/beans.xml");
archive.addAsResource("resources/persistence.xml", "META-INF/persistence.xml");
然后在服务器上,整个依赖注入应该工作,或者至少测试部署应该失败并显示适当的消息
编辑:
关于
Error launching request at.....
错误:
据此:
https://docs.jboss.org/author/display/ARQ/Container+configuration
尝试添加
<property name="host">yourhost</property>
<property name="port">8181</property>
到你arquillian.xml
中的配置确保导入正确的注解:
org.jboss.arquillian.container.test.api.Deployment
而不是这个:
org.jboss.arquillian.api.Deployment;
第二个不会被拾取,报同样的错误
there is no running deployment yet. Please use the annotation @RunAsClient