如何在Storm中测试Spouts

时间:2017-04-02 20:05:03

标签: java apache-storm

我正在努力完成对我的Apache Storm spout进行单元测试的任务,该实际上从队列中读取并发送消息作为元组。我想使用嵌入式队列,将消息放入队列并将资产发送给我的spout。任何人都可以帮助用来实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

你可能有很多方法可以做到这一点,但这里有一个使用mockito和testng(\ src \ test \ java \ com \ example \ storm \ spout \ DummySpoutTest.java)进行简单测试的例子:

package com.example.storm.spout;

import static org.mockito.Matchers.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.testng.annotations.Test;

import backtype.storm.spout.SpoutOutputCollector;

import com.example.storm.spout.DummySpout;

public class DummySpoutTest {

    @SuppressWarnings("unchecked")
    @Test
    public void shouldCreateDummyMessage() {
        // given
        DummySpout spout = new DummySpout();
        SpoutOutputCollector collector = mock(SpoutOutputCollector.class);
        spout.open(null, null, collector);

        // when
        spout.nextTuple();

        // then
        verify(collector).emit(anyList());
    }

}