weld-junit5 4.0.0.CR1 namespace confusion

47 views Asked by At

I'm a bit confused about the namespace usage for @Inject, @Produces etc. with weld-junit5 4.0.0.CR2. I'm toying around with JakartaEE 8 + Java 17 and weld-junit5 3.x got upset so I guess 4.x is the way to go?

The problem is that with a test class like

@EnableWeld
public class TestNotificationDao {

    @WeldSetup
    public WeldInitiator weld = WeldInitiator.of(NotificationDao.class, NotificationDaoImpl.class, TestNotificationDao.class);

    @Inject
    private NotificationDao notificationDao;

    public TestNotificationDao() {
    }

    @Produces
    static DataSource getDataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://localhost:3306/sps?useSSL=false");
        dataSource.setUser("sps");
        dataSource.setPassword("sps");
        dataSource.setDatabaseName("sps");
        return dataSource;
    }

    @Test
    public void testLoadQueuedNotifications() {
        Collection<QueuedNotification> notifications = notificationDao.getQueuedNotifications(1);
        assertEquals(1, notifications.size());
    }
}

If I use javax.inject.Inject, the notificationDao is null and If I use jakarta.inject.Inject (and Produces), notificationDao is injected but the @javax.inject.Inject private DataSource dataSource inside the NotificationDaoImpl is null.

1

There are 1 answers

0
Nicklas Karlsson On

Solved by moving to the

    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>9.1.0</version>
        <scope>provided</scope>
    </dependency>