我有一个小型(Java SE 11.x
)项目,我正在使用 Weld 测试 CDI。我有这些依赖项(除其他外,但这些是与此问题相关的):
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")
然后我有一个简单的实体类:
@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;
}
...以及与数据库交互的 DAO 类:
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.
}
我要做的是为 DAO 类编写一个简单的单元测试:
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);
});
}
}
这个堆栈跟踪总是失败:
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
我已经搜索了很多类似的问题/答案,但仍然无法弄清楚我在这里做错了什么。有什么建议吗?
顺便说一句,我确实有一个main/resources/META-INF/beans.xml
and test/resources/META-INF/beans.xml
。两者的内容是一样的:
<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>