I have a small (Java SE 11.x
) project where I'm testing CDI with Weld. I have these dependencies (among others but these are the relevant ones for this issue):
implementation("org.jboss.weld.se:weld-se-core:4.0.0.Final")
runtimeOnly("javax:javaee-api:8.0.1")
testImplementation("org.jboss.weld:weld-junit5:2.0.2.Final")
Then I have a simple entity class:
@Entity
@Table(name = "spell_books")
public final class SpellBook extends AbstractPersistable {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "id", nullable = false, updatable = false)
private UUID id;
@Column(name = "name")
private String name;
// ...some one-to-many and many-to-may relationships
public SpellBook() { // No-args constructor required by JPA
spells = new LinkedList<>();
wizards = new LinkedList<>();
}
public SpellBook(final String name) {
this();
this.name = name;
}
public void add(final Spell spell) {
spell.setSpellBook(this);
spells.add(spell);
}
}
@MappedSuperclass
public abstract class AbstractPersistable implements Serializable {
@Version
@Column(name = "version")
private Long version;
}
...and a DAO class to interface with the database:
import jakarta.inject.Singleton;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Singleton
public class SpellBookDao extends AbstractJpaDao<SpellBook> {
public Optional<SpellBook> findByName(final String name) {
logger.debug("Searching spell book with name [{}]...", name);
final var builder = entityManager.getCriteriaBuilder();
final var criteria = builder.createQuery(clazz);
final var model = criteria.from(clazz);
criteria.select(model).where(builder.equal(model.get("name"), name));
return Optional.ofNullable(entityManager.createQuery(criteria.select(model)).getSingleResult());
}
}
@Log4j2
public abstract class AbstractJpaDao<T extends AbstractPersistable> {
@PersistenceContext
protected EntityManager entityManager;
protected Class<T> clazz;
public void setClazz(final Class<T> clazz) {
this.clazz = clazz;
}
// ...some defaults for persist, merge, findAll, findOne, etc.
}
What I'm trying to do is to write a simple unit test for the DAO class:
import jakarta.inject.Inject;
import org.acme.service_layer.persistence.SpellBookDao;
import org.assertj.core.api.Assertions;
import org.jboss.weld.junit5.EnableWeld;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.util.UUID;
@EnableWeld
final class SpellBookDaoTest {
@Inject
private SpellBookDao dao;
@Test
void findByName_WhenSpellBookExists() {
final var optional = dao.findByName("The Dark Lord Ascending");
Assertions.assertThat(optional)
.hasValueSatisfying(it -> {
Assertions.assertThat(it.getId()).isEqualTo(UUID.fromString("715811c9-ae11-41ec-8652-671fd88cd2a0"));
Assertions.assertThat(it.getName()).isEqualTo("The Dark Lord Ascending");
Assertions.assertThat(it.getSpells()).isEmpty();
Assertions.assertThat(it.getWizards()).isEmpty();
// ...
Assertions.assertThat(it.getVersion()).isEqualTo(0L);
});
}
}
This always fails with this stacktrace:
WELD-001408: Unsatisfied dependencies for type SpellBookDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private org.acme.service_layer.persistence.internal.SpellBookDaoTest.dao
at org.acme.service_layer.persistence.internal.SpellBookDaoTest.dao(SpellBookDaoTest.java:0)
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type SpellBookDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private org.acme.service_layer.persistence.internal.SpellBookDaoTest.dao
at org.acme.service_layer.persistence.internal.SpellBookDaoTest.dao(SpellBookDaoTest.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:378)
...many more here
I've searched through a lot of similar questions/answers but still, I can't figure it out what I'm doing wrong here. Any advice?
By the way, I do have a main/resources/META-INF/beans.xml
and test/resources/META-INF/beans.xml
. The content for both is the same:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="all">
<scan>
<exclude name="org.jboss.weld.**" />
</scan>
</beans>
As I stated in the GitHub issue for this question - this is an issue because as of today (23/02/2021),
weld-junit
version2.x
is set to work with Java/Jakarta EE 8. I'll need to release a new major version for Jakarta EE 9 and itsjakarta
namespace which is implemented by Weld 4. It shouldn't be too hard and I expect to be able to find time for that within a week or two.EDIT: weld-junit version 3.0.0.Final is now available in Central and is designed to work with Jakarta EE 9 namespaces hence the above issue should be effectively solved.
Until then, you can only use
weld-junit
if you swap to EE 8 with itsjavax
namespaces and also use Weld 3 which implements that.Last but not least, your project mixes up EE 8 and EE 9 artifacts - this is a problem you need to solve on your side, mainly due to the namespace differences between the two, and would keep causing issues even after we fix
weld-junit
.