使用Java在Akka测试套件中测试流

时间:2019-11-21 12:02:40

标签: java akka akka-stream

https://doc.akka.io/docs/akka/current/stream/stream-testkit.html

我正在使用Java使用akka,谁能告诉我如何在代码中初始化系统

final Sink<Integer, CompletionStage<Integer>> sinkUnderTest =
Flow.of(Integer.class)
    .map(i -> i * 2)
    .toMat(Sink.fold(0, (agg, next) -> agg + next), Keep.right());

final CompletionStage<Integer> future =
Source.from(Arrays.asList(1, 2, 3, 4)).runWith(sinkUnderTest, system);
final Integer result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
assert (result == 20);

static ActorSystem system =ActorSystem.create()

中不起作用
Source.from(Arrays.asList(1, 2, 3, 4)).runWith(sinkUnderTest, system);

2 个答案:

答案 0 :(得分:0)

您是否检查了示例的源代码? source code

  static ActorSystem system;

  @BeforeClass
  public static void setup() {
    system = ActorSystem.create("StreamTestKitDocTest");
  }

答案 1 :(得分:0)

修改:

当您使用带有2.12 API的akka​​ 2.5时,您将必须按照匹配文档的快速入门部分中所述创建实例化器,请选中here

private ActorSystem system;
private Materializer materializer;

@BeforeEach
public void setup() {
    system = ActorSystem.create("StreamTestKitDocTest");
    materializer = ActorMaterializer.create(system);
}

// ...

@Test
public void test() throws InterruptedException, ExecutionException, TimeoutException {
    // ...
    Source.from(Arrays.asList(1, 2, 3, 4)).runWith(sinkUnderTest, materializer);
    // ...
}

正如您所说的那样,它甚至不适合您编译,我假设您的依赖项或导入都存在问题。一个常见的错误是,您意外导入了scala版本,而不是java dsl。

这里是我用来验证其正常工作的依赖项和代码(已通过Java 1.8测试):

依赖项:

<dependencies>
    <dependency>
      <groupId>com.typesafe.akka</groupId>
      <artifactId>akka-stream-testkit_2.13</artifactId>
      <version>2.6.0</version>
      <scope>test</scope>
    </dependency>
     <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

JUnit测试用例:

import java.util.Arrays;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import akka.actor.ActorSystem;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Keep;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;

public class AkkaTest {

    private ActorSystem system;

    @Before
    public void setup() {
        system = ActorSystem.create("StreamTestKitDocTest");
    }

    @After
    public void shutdown() {
        system.terminate();
    }

    @Test
    public void test() throws InterruptedException, ExecutionException, TimeoutException {

        final Sink<Integer, CompletionStage<Integer>> sinkUnderTest =
        Flow.of(Integer.class)
            .map(i -> i * 2)
            .toMat(Sink.fold(0, (agg, next) -> agg + next), Keep.right());

        final CompletionStage<Integer> future =
        Source.from(Arrays.asList(1, 2, 3, 4)).runWith(sinkUnderTest, system);
        final Integer result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);

        Assert.assertEquals(20, result.intValue());
    }
}