【问题标题】:How to check that method has been invoked in arquillian test如何检查该方法已在 arquillian 测试中被调用
【发布时间】:2016-09-04 12:55:43
【问题描述】:

我正在使用 arquillian 和 wilfdly 服务器测试 jms。这是一个简单的测试:

@RunWith(Arquillian.class)
public class PubSubMdbAsyncJmsTest {

    public static final String MESSAGE_1 = "Test message1";

    @Inject
    PubSubProducer pubSubProducer;

    final AtomicBoolean hasBeenInvoked = new AtomicBoolean(false);

    @Produces
    public Function<String, Void> messageConsumer() {
        return new Function<String, Void>() {
            @Override
            public Void apply(final String message) {
                Assert.assertEquals(MESSAGE_1, message);
                hasBeenInvoked.set(true);
                return null;
            }
        };
    }

    @Deployment
    public static JavaArchive createDeployment() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
                .addClass(PubSubProducer.class)
                .addClass(MdbConsumer.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        System.out.println(jar.toString(true));
        return jar;
    }

    /*
     */
    @Test
    public void testSendMessageAndConsumeItSuccessfully() throws InterruptedException {
        pubSubProducer.sendMessage(MESSAGE_1);
        TimeUnit.SECONDS.wait(5);
        Assert.assertTrue(hasBeenInvoked.get());
    }
}

测试是在 arquillian 下疯狂地运行的。 Assert.assertTrue(hasBeenInvoked.get()); 检查失败。如果将 Function 更改为(添加抛出异常):

   @Produces
    public Function<String, Void> messageConsumer() {
        return new Function<String, Void>() {
            @Override
            public Void apply(final String message) {
                Assert.assertEquals(MESSAGE_1, message);
                if(true) throw new RuntimeException(message);
                hasBeenInvoked.set(true);
                return null;
            }
        };
    }

您将看到带有正确消息 ("Test message1") 的 RuntimeException。这意味着apply 方法已被调用。不清楚,为什么hasBeenInvoked还是假的?

生成的Function用于MDB bean,我认为不需要这个bean的实现。正如我所说,只要你抛出一个异常,你就可以看到该方法被调用。问题是,为什么最后一个断言在测试中失败了?

作为替代方案,也许我可以使用 mockito 来检查 apply 方法是否已被调用,但在这种情况下我该如何使用它呢?

【问题讨论】:

    标签: java jakarta-ee junit jms jboss-arquillian


    【解决方案1】:

    您可能打算使用:

    TimeUnit.SECONDS.sleep(5);

    而不是

    TimeUnit.SECONDS.wait(5);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      相关资源
      最近更新 更多