简体   繁体   中英

How to check that method has been invoked in arquillian test

I'm testing jms with arquillian and wilfdly server. Here is a simple test:

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

Test is run under arquillian and wilfdly. The Assert.assertTrue(hasBeenInvoked.get()); check fails. If you change the Function to (add throwing an exception):

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

You'll see this RuntimeException with a correct message (" Test message1 "). Which means that the apply method has been invoked. It's not clear, why the hasBeenInvoked is still false?

The produced Function is used in MDB bean and, I think the implementation of this bean is not needed. As I said, as soon as you throw an exception, you can see that method is invoked. The question is, why the last assertion is failed in test?

As an alternative, maybe I could use mockito to check whether the apply method has been invoked or not, but how can I use it in this case?

You probably meant to use :

TimeUnit.SECONDS.sleep(5);

instead of

TimeUnit.SECONDS.wait(5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM