Java 嵌入glassfish容器的JUnit测试错误

Java 嵌入glassfish容器的JUnit测试错误,java,jdbc,junit,glassfish,Java,Jdbc,Junit,Glassfish,我必须承认,到目前为止,JUnit考试一直是一场噩梦,我花了几天时间在互联网上搜索,但我尝试的一切都没有奏效。 我的错误如下: SCHWERWIEGEND: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method java.lang.RuntimeException: Invalid resource : jdbc/sample__pm jdbc/sample\uu pm不存

我必须承认,到目前为止,JUnit考试一直是一场噩梦,我花了几天时间在互联网上搜索,但我尝试的一切都没有奏效。 我的错误如下:

SCHWERWIEGEND: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
java.lang.RuntimeException: Invalid resource : jdbc/sample__pm
jdbc/sample\uu pm不存在,但persistence.xml中的jdbc/sample存在。 正如我所说,我已经寻找了这个问题,但我找不到解决这个问题的办法

我想问题出在pom.xml上,但我不知道如何修复它

谢谢你的帮助

pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maggioni</groupId>
<artifactId>SampleApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>4.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.10.2.0</version>    
    </dependency>

</dependencies>
<build>
    <finalName>SampleApp</finalName>
</build>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

源代码也可以找到

我也有同样的问题,在浪费了很多时间之后,我把它弄对了。我现在可以用连接池运行测试了。 这个问题是由Netbeans中一系列小型maven代码生成错误造成的

下面是一系列步骤供您检查

1-您的pom.xml必须为glassfish嵌入式静态shell配置路径、插件和依赖项,如下所示:

将属性添加到pom顶部glassfish安装文件夹的路径这里是我的: ${project.build.directory}/project UTF-8 C:/Program Files/glassfish-4.0/glassfish/lib/embedded/glassfish-embedded-static-shell.jar

在您的案例中,添加/检查插件定义BellowsSubstitute mysql per maven: `

<build>
...
  <plugins>
    ...
     <plugin>
        <groupId>org.glassfish.embedded</groupId>
        <artifactId>maven-embedded-glassfish-plugin</artifactId>
        <version>4.0</version>

        <dependencies>
            <dependency>
                <groupId>org.glassfish.main.common</groupId>
                <artifactId>simple-glassfish-api</artifactId>
                <version>4.1</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.main.extras</groupId>
                <artifactId>glassfish-embedded-static-shell</artifactId>
                <version>4.1</version>
                <scope>system</scope>
                <systemPath>${glassfish.embedded-static-shell.jar}</systemPath>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.28</version>
            </dependency>
        </dependencies>
        <configuration>
            <app>target/${project.artifactId}-${project.version}</app>
            <port>8282</port>
            <contextRoot>${project.artifactId}</contextRoot>
            <foo>bar</foo>
        </configuration>

    </plugin>
  ...
  </plugins>
 ...
</build>
`

<build>
...
  <plugins>
    ...
     <plugin>
        <groupId>org.glassfish.embedded</groupId>
        <artifactId>maven-embedded-glassfish-plugin</artifactId>
        <version>4.0</version>

        <dependencies>
            <dependency>
                <groupId>org.glassfish.main.common</groupId>
                <artifactId>simple-glassfish-api</artifactId>
                <version>4.1</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.main.extras</groupId>
                <artifactId>glassfish-embedded-static-shell</artifactId>
                <version>4.1</version>
                <scope>system</scope>
                <systemPath>${glassfish.embedded-static-shell.jar}</systemPath>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.28</version>
            </dependency>
        </dependencies>
        <configuration>
            <app>target/${project.artifactId}-${project.version}</app>
            <port>8282</port>
            <contextRoot>${project.artifactId}</contextRoot>
            <foo>bar</foo>
        </configuration>

    </plugin>
  ...
  </plugins>
 ...
</build>
2-您的玻璃鱼资源。。。这很愚蠢,但是jdbc资源的jndi名称必须具有前缀java:app/so,将其添加为示例bellow仅此资源,连接池可以具有任何名称: *只要记住glassfish中jdbc资源的名称,就不会有前缀java:app,例如:glassfish中我的jdbc资源只是一个精明的PCPool

第三,也是最愚蠢的一点,在部署嵌入式glassfish时,Netbeans自动生成的脚本在类路径中不包含/src/main/resources/setup,因此,简单的解决方案是复制glassfish-resources.xml para/src/main/resources/META-INF/或者如果您有足够的耐心,可以更改脚本

在运行测试之前,不要忘记清理和构建依赖项