Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何使用嵌入式企业bean容器(GlassFish)过滤(替换)ebj-jar.xml中的env条目值以进行单元测试?_Unit Testing_Maven_Ejb 3.1_Glassfish Embedded - Fatal编程技术网

Unit testing 如何使用嵌入式企业bean容器(GlassFish)过滤(替换)ebj-jar.xml中的env条目值以进行单元测试?

Unit testing 如何使用嵌入式企业bean容器(GlassFish)过滤(替换)ebj-jar.xml中的env条目值以进行单元测试?,unit-testing,maven,ejb-3.1,glassfish-embedded,Unit Testing,Maven,Ejb 3.1,Glassfish Embedded,我需要几个配置文件进行部署。在Maven POM中,我定义了一个概要文件“dev”和一个属性“theHost”(作为localhost): 最后,在ejb-jar.xml中,我引用${theHost}来获得@Resource属性“host”所需的特定于概要文件的值: 有人知道为什么在运行单元测试时替换不起作用吗?还有什么我必须特别为单元测试定义的,以便对ejb-jar.xml进行过滤吗?或者如果存在不同的概要文件,那么使用更好的方法对EJB进行单元测试?理想情况下,您可以为env条目指定一个外部

我需要几个配置文件进行部署。在Maven POM中,我定义了一个概要文件“dev”和一个属性“theHost”(作为localhost):

最后,在ejb-jar.xml中,我引用
${theHost}
来获得@Resource属性“host”所需的特定于概要文件的值:


有人知道为什么在运行单元测试时替换不起作用吗?还有什么我必须特别为单元测试定义的,以便对ejb-jar.xml进行过滤吗?或者如果存在不同的概要文件,那么使用更好的方法对EJB进行单元测试?

理想情况下,您可以为env条目指定一个外部“绑定”。我知道WebSphereApplicationServer(via)可以做到这一点,但我不知道Glassfish是否可以做到这一点

作为一种解决方法,您可以声明要注入的env条目,然后在构造后检查容器是否注入了任何值(即,在部署到服务器之前不要指定env条目值)。如果您仅使用JNDI,那么可以使用try/catch(NameNotFoundException)执行相同的操作


基于bkail建议的解决方法:仅为单元测试设置系统属性,并在构造后发现:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14.1</version>
            <configuration>
                <skip>false</skip>
                <argLine>-Xmx1g -XX:MaxPermSize=128m</argLine>
                <reuseForks>false</reuseForks> <!-- with reuse the EJB timer service would fail -->
                <systemPropertyVariables>
                    <is.unittest>true</is.unittest>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    // Override values that were not substituted in ejb-jar.xml
    if (Boolean.getBoolean("is.unittest")) {
        host = "localhost";
        port = "27017";
        authenticationRequired = false;
    }

谢谢你的建议。我尽量避免容器的专有机制,因为我们将来可能会切换。但我通过使用Maven插件Maven surefire插件设置系统属性来实现您的解决方案。由于评论中的空间限制,答案中的详细信息。
<session>
  <ejb-name>MongoDao</ejb-name>
  <ejb-class>com.coolcorp.MongoDao</ejb-class>
  <session-type>Stateless</session-type>
  <env-entry>
    <env-entry-name>host</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>${theHost}</env-entry-value>
  </env-entry>
...
mvn.bat -Pdev test
@Resource(name="host")
private String host;

@PostConstruct
public void postConstruct() {
  if (host == null) {
    // Not configured at deployment time.
    host = System.getProperty("test.host");
  }
}
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14.1</version>
            <configuration>
                <skip>false</skip>
                <argLine>-Xmx1g -XX:MaxPermSize=128m</argLine>
                <reuseForks>false</reuseForks> <!-- with reuse the EJB timer service would fail -->
                <systemPropertyVariables>
                    <is.unittest>true</is.unittest>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    // Override values that were not substituted in ejb-jar.xml
    if (Boolean.getBoolean("is.unittest")) {
        host = "localhost";
        port = "27017";
        authenticationRequired = false;
    }