gpt4 book ai didi

java - 如何使用 Arquillian 模拟 MyBatis 映射器接口(interface)(第 2 部分)?

转载 作者:行者123 更新时间:2023-12-02 13:41:19 42 4
gpt4 key购买 nike

这是我第二次尝试使用 MyBatis 创建集成测试。我已经尝试了很多方法,但似乎没有解决这个问题的方法。希望大家能够帮助我。

In my previous question我尝试编写一个集成测试来检查其余 API 的输出。场景如下:rest API 调用注入(inject)的 EJB,该 EJB 使用 MyBatis 执行一些 SQL:rest api > ejb > mybatis。不幸的是,我既无法注入(inject),也无法模拟 MyBatis 映射器接口(interface),因此我的测试不起作用:(

现在我创建了另一个测试场景,但最终遇到了同样的情况。现在我的场景非常简单:我有一个带有注入(inject)的 MyBatis 映射器的 EJB。我想在带有 Arquillian 的嵌入式 Glassfish/Payara 服务器中测试它。

这是我的异常(exception):

org.glassfish.deployment.common.DeploymentException: CDI deployment failure:WELD-001408: Unsatisfied dependencies for type AccountDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private a.b.c.AppleBean.accountDao
at a.b.c.AppleBean.accountDao(AppleBean.java:0)

EJB:

@Stateless
public class AppleBean {
@Inject
private AccountDao accountDao;

public String say() {
return "Apple";
}
}

帐户映射器 (DAO):

@Mapper
public interface AccountDao {

@Select("SELECT * FROM account WHERE id = #{id}")
@Results({
@Result(property = "email", column = "email", javaType = String.class),
@Result(property = "firstName", column = "first_name", javaType = String.class),
@Result(property = "lastName", column = "last_name", javaType = String.class),
})
Account findById(@Param("id") Long id);
}

我的测试类:

@RunWith(Arquillian.class)
public class AppleBeanTest {
@EJB
private AppleBean bean;

@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap
.createFromZipFile(WebArchive.class, new File("target/war-demo-test-1.0.war"))
.addPackages(true, "a.b");
}

@Test
public void say() throws Exception {
assertNotNull(bean);
System.out.println(bean.say());
}
}

如果我注释 AppleBeanTest 中的两行以删除对 MyBatis 映射器的引用,那么我的测试工作正常。

I uploaded the source code to github as well.

<小时/>

解决方案

我的测试中缺少以下类(class)。 @blackwizard 谢谢你让我找到了正确的方向。

SessionFactoryProducer.java

@ApplicationScoped
public class SessionFactoryProducer {
@ApplicationScoped
@Produces
@SessionFactoryProvider
public SqlSessionFactory produce() throws Exception {
SqlSessionFactory sessionFactory;
try (Reader reader = Resources.getResourceAsReader("mybatis.xml")) {
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
// create sample table
//createTable(sessionFactory);

return sessionFactory;
}

private void createTable(final SqlSessionFactory manager) throws Exception {
try (SqlSession session = manager.openSession()) {
LOGGER.info("-> Initializing database...");
Connection conn = session.getConnection();
Reader reader = Resources.getResourceAsReader("create-table-postgresql.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.runScript(reader);
reader.close();
LOGGER.info("=> Database has been initialized properly.");
} catch (Exception ex) {
LOGGER.error("Error executing SQL Script...", ex);
}
}
}

git 项目已更新。

最佳答案

此时此地,我没有资源来克隆和运行您的项目来确认我要说的话。但如果有必要的话,我会在周一,同时,以下可能是一个轨道:

我认为它不起作用,因为缺少一些非常重要的东西:SqlSessionFactory

mybatis-cdi doc第一段中指出:

The SqlSessionFactory is the source of any MyBatis bean so first you need to create one (at least) and let the container know about it existence.

确实,如果没有Session,就没有理由获取Mapper实例。如果@Mapper注释足够了,它只能提供一个空壳,因为不链接到任何底层数据源。那么如果没有Mapper,就无法注入(inject)到EJB中,这就是Weld提示的地方。

部署成功后,是用@Inject private AccountDao accountDao吗?我不明白为什么 Weld 允许不注入(inject)任何东西。但如果确实如此,请检查 accountDao 值(调试断点或日志)。

关于java - 如何使用 Arquillian 模拟 MyBatis 映射器接口(interface)(第 2 部分)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42728129/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com