0

I got following service bean that picks up connection and destination from JBoss standalone configuration file when deployed.

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

@Stateless
public class MessageSenderImpl {

    @Resource(lookup = "java:jboss/ConnectionFactory")
    private ConnectionFactory connectionFactory;

    @Resource(lookup = "java:jboss/to")
    private Destination destination;
    
    public void send(List<OutboundMessage> omms) {
        if (omms.isEmpty())
            return;
        try {
            Connection connection = connectionFactory.createConnection();
            try {
                Session session = connection.createSession(true, 0);
                try {
                    MessageProducer producer = session.createProducer(destination);
                    try {
                        for (OutboundMessage omm : omms) {
                            String text = omm.getText();
                            TextMessage message = session.createTextMessage(text);
                            producer.send(message);
                        }
                    } finally {
                        producer.close();
                    }
                } finally {
                    session.close();
                }
            } finally {
                connection.close();
            }
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
    }
}

How do I write test case so that it picks up the connection to the testscontainer?

@Testcontainers
class MQTests {

    private static final MessageSenderImpl messageSender = new MessageSenderImpl();

    @Container
    static GenericContainer<?> mqContainer = new GenericContainer<>(DockerImageName.parse("ibmcom/mq"))
            .withEnv("LICENSE", "accept")
            .withEnv("MQ_QMGR_NAME", "test")
            .withEnv("MQ_USER_NAME","admin")
            .withEnv("JMS_QUEUES", "To")
            .withEnv("JMS_QUEUES", "From")
            .withExposedPorts(1414);

    @Test
    void test1() {
        assertTrue(mqContainer.isRunning());
        messageSender.send(new OutboundMessage("test"));
    }
}

I got similar service bean implementation for mail.

@Stateless
public class MailServiceImpl implements MailService {

    @Resource(mappedName = "java:jboss/mail/Default")
    private Session mailSession;

    public SendResult send(String toEmail, String subject, String body) {
        ...
    }

}

Inside tests Session resource to the testscontainer has been provided like below:

@Testcontainers
public interface MailContainer {
    
    private MailService mailService = new MailServiceImpl();

    int SMTP_PORT = 3025;
    int IMAP_PORT = 3143;

    @Container
    GenericContainer mailServer = new GenericContainer(DockerImageName.parse("greenmail/standalone"))
            .withExposedPorts(SMTP_PORT, IMAP_PORT);
            
    @Test
    void testSendMail() throws Exception {
        // Set system properties or environment variables
        System.setProperty("mail.smtp.host", mailServer.getHost());
        System.setProperty("mail.smtp.port", String.valueOf(mailServer.getMappedPort(SMTP_PORT)));

        // Send the mail
        String toEmail = "test@localhost.com";
        String subject = "Test Subject";
        String body = "Test Body";
        MailService.SendResult result = mailService.send(toEmail, subject, body);

        // Assert the mail was sent successfully
        assertEquals(MailService.SendResult.OK, result);
}

And it works, is it possible to do the same for mq connection as shown with mail? Or I should I be looking to other aproaches?

Martin
  • 411
  • 8
  • 21
  • You can do exactly the same. Why the concern? Testcontainers is aim to help with any image available to run it as part of your tests. You can create networks so containers can communicate to each other using networkAliases. – Eddú Meléndez May 22 '23 at 17:48

0 Answers0