How to taunt with the interface of MyBatis Mapper with Arquillian (PART2)?

This is my second attempt to create an integration test with MyBatis. I have tried many things, but it seems that there is no solution to this problem. Hope you guys can help me.

In my previous question, I tried to write an integration test to check the output of my rest API. The scenario was as follows: the rest API calls an embedded EJB that executes some SQL with MyBatis: rest api> ejb> mybatis. Unfortunately, I could neither enter nor prototype the MapBaris Mapper interface, so my test does not work :(

Now I have created another test case, but I ended up in the same situation. Now my scenario is crazy: I have an EJB with an injected MyBatis mapper. I would like to test it on the built-in Glassfish / Payara server with Arquillian.

This is my 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 abcAppleBean.accountDao at abcAppleBean.accountDao(AppleBean.java:0) 

EJB:

 @Stateless public class AppleBean { @Inject private AccountDao accountDao; public String say() { return "Apple"; } } 

Account (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); } 

My test class:

 @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, "ab"); } @Test public void say() throws Exception { assertNotNull(bean); System.out.println(bean.say()); } } 

If I comment out two lines in AppleBeanTest to remove the refelence for myBatis mapper, then my test will work fine.

I also uploaded the source code to github .


Decision

In my test, the following class was missing. @blackwizard thanks you for setting me in the right direction.

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); } } } 

The git project has been updated.

0
source share
1 answer

Here and now, I donโ€™t have the resources to clone and run your project to confirm what I will say. But I will be on Monday, if necessary, and in the meantime, the following may be the track:

I think this does not work because something very important is SqlSessionFactory : SqlSessionFactory .

mybatis-cdi doc says in the first paragraph:

SqlSessionFactory is the source of any MyBatis bean, so first you need to create one (at least) and report the existence to the container.

In fact, there is no reason to get a Mapper instance if there is no session. if the @Mapper annotation was sufficient, it could only provide an empty wrapper because it did not refer to any underlying data source. Then, if there is no Mapper, it cannot be entered into the EJB, which Weld complains about.

When the deployment succeeds, is it with @Inject private AccountDao accountDao ? I donโ€™t understand why Sward could enter anything. But if so, check the value of accountDao (debug point or log).

+1
source

Source: https://habr.com/ru/post/1264997/


All Articles