2016-05-09 43 views
0

我正在用arquillian和wilfdly服务器测试jms。这里是一个简单的测试:如何检查在arquillian测试中调用该方法

@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和wilfdly下运行。 Assert.assertTrue(hasBeenInvoked.get());检查失败。如果你改变了功能,以(添加抛出一个异常):

@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; 
      } 
     }; 
    } 

你会看到这样的RuntimeException有一个正确的消息(“Test message1”)。这意味着apply方法已被调用。目前还不清楚,为什么hasBeenInvoked仍然是错误的?

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

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

回答

0

你可能想用的是:中

TimeUnit.SECONDS.sleep(5);

代替

TimeUnit.SECONDS.wait(5);

相关问题