5 Replies Latest reply on Feb 24, 2015 11:43 AM by rickcr

    Having the most difficult time getting my MyBatis test to work...

    rickcr

      I'm new to using CDI (coming over from Spring) and I'm trying to get a simple MyBatis test working using Arquillian.

       

      I have a sample app I put together that works fine deployed to Tomcat or JBoss, and I'd love to get the simple Integration test I have there working.

      https://github.com/rickcr/mybatis-cdi-zk/blob/master/src/test/java/net/learntechnology/empmaint/net/learntechnology/empmaint/services/DepartmentIntegrationIT.java

       

      It's difficult for me to debug since I keep getting

       

      java.lang.RuntimeException: Could not inject members

      ...

      Caused by: java.util.NoSuchElementException

       

      my deployment setup you can see in the above link.. also showing here:

       

      @Deployment
      public static WebArchive createDeployment() {

        WebArchive archive = ShrinkWrap.create(WebArchive.class)

        .addClass(SqlSessionFactoryProvider.class)

        .addClass(BaseVO.class)

        .addClass(Department.class)

        .addClass(DepartmentService.class)

        .addClass(DepartmentMapper.class)

        .addAsManifestResource(new File("src/main/webapp//META-INF/context.xml"))

        .addAsResource("mybatis-config.xml").addAsResource("mybatis-config.properties")

        .addAsWebInfResource(new File("src/main/webapp/WEB-INF/beans.xml"))

        ;

        System.out.println("Archive = "+archive.toString(true));

        archive.as(ZipExporter.class).exportTo(

         new File("/Users/rick/projects/mybatis-cdi-zk/temp/departmentTest.zip"), true);

         return archive;

      }

        • 1. Re: Having the most difficult time getting my MyBatis test to work...
          dibu

          You must add all used test classes to the web archive too.

          • 2. Re: Having the most difficult time getting my MyBatis test to work...
            rickcr

            Thanks Dirk. I'm now just trying to use the entire war generated by Maven first. (Tried adding a few more things missing and still the same issue.)  I figure if I can get the tests to work  using the generated maven war, then maybe at some point I can work backwards and just add the classes I want to test later.

            However even trying to use the entire war, I'm having some issues. Seems to appear to 'sort of work' as I see in the logs the mapper dependencies discovered, but then I get that cryptic "Could not inject members" error

             

            Currently I'm defining the archive now as

             

            WebArchive archive = ShrinkWrap.create(ZipImporter.class, "mybatis-cdi-zk-1.0.war").importFrom(new File("target/mybatis-cdi-zk-1.0.war"))

              .as(WebArchive.class);

             

             

            Feb 23, 2015 8:32:33 AM org.mybatis.cdi.Extension afterBeanDiscovery

            INFO: MyBatis CDI Module - Activated

            Feb 23, 2015 8:32:33 AM org.mybatis.cdi.Extension afterBeanDiscovery

            INFO: MyBatis CDI Module - Mapper dependency discovered: net.learntechnology.empmaint.mapper.DepartmentMapper

            Feb 23, 2015 8:32:33 AM org.mybatis.cdi.Extension afterBeanDiscovery

            INFO: MyBatis CDI Module - Mapper dependency discovered: net.learntechnology.empmaint.mapper.EmployeeMapper

             

            java.lang.RuntimeException: Could not inject members....

             

            I assume even though it's a war I'm using,  I should still be able to directly test any of the java methods that are bundled in the war?

            For example my test is trying to test one of the service methods in the war....

             

            @Deployment
            public static WebArchive createDeployment() { ... }

             

            @Inject
            private DepartmentService departmentService;

             

            @Test
            public void getDepartmentsTest() {

              List<Department> departments = departmentService.getAllDepartments();

               ....

            • 3. Re: Re: Having the most difficult time getting my MyBatis test to work...
              dibu

              I look at you code and the problem is still missing dependencies in the test archive. You must add ALL dependencies (classes, test classes and used libraries).

              You can use the Maven resolver like this:

              WebArchive archive = ...

              Srting key = "org.mybatis:mybatis-cdi";

              JavaArchive[] libraries = Maven.resolver().loadPomFromFile("pom.xml").resolve(key).withTransitivity().as(JavaArchive.class);

              archive.addAsLibraries(libraries);

              • 4. Re: Having the most difficult time getting my MyBatis test to work...
                rickcr

                Thanks Dirk...

                I set it up like this now and it worked!

                 

                My final deployment that works is set up as:

                 

                WebArchive archive = ShrinkWrap.create(ZipImporter.class, "mybatis-cdi-zk-1.0.war").importFrom(new File("target/mybatis-cdi-zk-1.0.war"))

                  .as(WebArchive.class);

                String key = "org.mybatis:mybatis-cdi";

                JavaArchive[] libraries = Maven.resolver().loadPomFromFile("pom.xml").resolve(key).withTransitivity().as(JavaArchive.class);

                archive.addAsLibraries(libraries);

                 

                Why isn't what you mentioned listed in any of the startup guides Guides · Arquillian ?  What you proposed worked and is a million times easier than trying to figure out every dependency my project has in it  in order to add it to my archive.  Thank you!

                • 5. Re: Having the most difficult time getting my MyBatis test to work...
                  rickcr

                  Problem with using the war directly though is it doesn't use my test resources. I'll try to package all my mybatis-cdi services into its own jar for testing and see how that goes.

                  Worst case the war can be used directly as is for testing... I'd just have to use a different profile for the maven build.