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); }
The git project has been updated.