Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Java 数据流单元测试_Java_Unit Testing_Google Cloud Dataflow_Apache Beam - Fatal编程技术网

Java 数据流单元测试

Java 数据流单元测试,java,unit-testing,google-cloud-dataflow,apache-beam,Java,Unit Testing,Google Cloud Dataflow,Apache Beam,我正在尝试为我的管道构建一个单元测试 该管道从pubsub读取数据,执行转换并将结果再次写入pubsub 为了进一步简化单元测试,直到它工作为止,单元测试将只接收一个字符串作为输入,并在输出是某个字符串时进行测试 代码如下所示: @RunWith(JUnit4.class) public class TesterPipeline { // Our static output data, which is the expected data that the final PColl

我正在尝试为我的管道构建一个单元测试

该管道从pubsub读取数据,执行转换并将结果再次写入pubsub

为了进一步简化单元测试,直到它工作为止,单元测试将只接收一个字符串作为输入,并在输出是某个字符串时进行测试

代码如下所示:

@RunWith(JUnit4.class)
public class TesterPipeline {   

    // Our static output data, which is the expected data that the final PCollection must match.
    static final String[] COUNTS_ARRAY = new String[] {"this"};

    @Rule public TestPipeline p = TestPipeline.create();

    // Example test that tests the pipeline's transforms.
    @Test
    @Category(ValidatesRunner.class)
    public void testPipeline() throws Exception {

      // Run the pipeline.
      PCollection<String> input= p.apply("input string", Create.of("This is just a message").withCoder(StringUtf8Coder.of()));
      PAssert.that(input).containsInAnyOrder(COUNTS_ARRAY);
      p.run();
    }
}
在查找有关此错误的信息时,我发现这与相关。在她看来,解决方案是:
hamcrest.jar需要在构建路径中位于Junit库之前。

我不确定我的配置中是否有错误,因为我认为我已经做了解决方案所说的,但我仍然会得到相同的错误

就像我是Java和Eclipse新手一样,也许我执行的单元测试是错误的,但是我没有在ApacheBeam的官方网站和google上找到更多信息

Junit配置:

生成路径配置:

pom文件:

<?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.xxx.pipeline</groupId>
  <artifactId>pipeline</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
    <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
    <slf4j.version>1.7.25</slf4j.version>
    <beam.version>2.16.0</beam.version>
    <apache-commons.version>2.5.0</apache-commons.version>
    <commons-cli.version>1.4</commons-cli.version>
  </properties>

  <repositories>
    <repository>
      <id>ossrh.snapshots</id>
      <name>Sonatype OSS Repository Hosting</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
      <releases>
        <enabled>false</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

  <build>
   <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler-plugin.version}</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>${exec-maven-plugin.version}</version>
          <configuration>
            <cleanupDaemonThreads>false</cleanupDaemonThreads>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <dependencies>
    <dependency>
      <groupId>com.google.cloud.dataflow</groupId>
      <artifactId>google-cloud-dataflow-java-sdk-all</artifactId>
      <version>2.5.0</version>
    </dependency>

    <dependency>
      <groupId>org.apache.beam</groupId>
      <artifactId>beam-runners-direct-java</artifactId>
      <version>${beam.version}</version>
      <scope>runtime</scope>
    </dependency>

    <dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
    <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.beam</groupId>
      <artifactId>beam-sdks-java-core</artifactId>
      <version>${beam.version}</version>
    </dependency>

    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
    </dependency>

    <dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache.beam</groupId>
      <artifactId>beam-sdks-java-io-google-cloud-platform</artifactId>
      <version>${beam.version}</version>
      <exclusions>
        <exclusion>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.apache.beam</groupId>
      <artifactId>beam-sdks-java-extensions-google-cloud-platform-core</artifactId>
      <version>${beam.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.beam</groupId>
      <artifactId>beam-sdks-java-extensions-protobuf</artifactId>
      <version>${beam.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.beam</groupId>
      <artifactId>beam-runners-google-cloud-dataflow-java</artifactId>
      <version>${beam.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>${apache-commons.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>${commons-cli.version}</version>
    </dependency>

    <!-- slf4j API frontend binding with JUL backend -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
  </dependencies>
</project>

4.0.0
com.xxx.pipeline
管道
0.0.1-快照
UTF-8
3.7.0
1.6.0
1.7.25
2.16.0
2.5.0
1.4
ossrh.snapshots
Sonatype OSS存储库托管
https://oss.sonatype.org/content/repositories/snapshots/
假的
真的
org.apache.maven.plugins
maven编译器插件
${maven编译器plugin.version}
1.8
1.8
org.codehaus.mojo
execmaven插件
${exec maven plugin.version}
假的
com.google.cloud.dataflow
google云数据流java sdk全部
2.5.0
org.apache.beam
直接java
${beam.version}
运行时
org.hamcrest
汉克雷斯特酒店
1.3
测试
org.apache.beam
beam SDK java核心
${beam.version}
公地io
公地io
2.6
com.googlecode.json-simple
简单json
1.1
org.apache.beam
beam SDK java io谷歌云平台
${beam.version}
朱尼特
朱尼特
org.apache.beam
beam SDK java扩展google云平台核心
${beam.version}
org.apache.beam
beam SDK java扩展协议
${beam.version}
org.apache.beam
谷歌云数据流java
${beam.version}
org.apache.commons
commons-dbcp2
${apache commons.version}
commons cli
commons cli
${commons cli.version}
org.slf4j
slf4j api
${slf4j.version}
org.slf4j
slf4j-jdk14
${slf4j.version}

首先,我删除了通过maven安装的JUnit库(属性->Java构建路径->库->添加库->JUnit->JUnit 3/4/5)

之后,在pom文件中,我添加了Junit依赖项(也是我之前添加的de hamcrest):


org.hamcrest
汉克雷斯特酒店
1.3
朱尼特
朱尼特
4.12
我从依赖项
test


之后,我可以在Eclipse中运行Junit测试,您需要根据构建要求在build.gradle或pom.xml中添加
hamcrest
库。 下面的代码

PAssert.that(input).containsInAnyOrder(COUNTS_ARRAY)
输入
pcollection
只有一个元素的值为
“这只是一条消息”
计数数组
是一个值为
的列表

因为这不匹配,所以它给出了一个断言错误


COUNTS\u数组
值设置为传入的
pcollection
&您的代码将正常工作。

我查看了您包含的链接。您是否尝试过按照中注释8中的说明进行操作?它说,将maven依赖项和Eclipse的JUnit4放在同一个依赖项列表中会导致问题,他们通过从该列表中删除JUnit4并通过maven pom导入来解决问题。是的。如果我这样做,测试将毫无问题地运行。我将把它作为一种解决办法。
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
PAssert.that(input).containsInAnyOrder(COUNTS_ARRAY)