How to test session bean?

Discussions

EJB programming & troubleshooting: How to test session bean?

  1. How to test session bean? (2 messages)

    It's not easy to do unit test for session bean though there's quite a lot articles on unit test and unit test in container.
    Is Cactus widely adopted? How abt openEJB?
    Is there any other convinient way to test session bean?
    TIA

    Regards,
    Xiao Qing Lai

    Threaded Messages (2)

  2. How to test session bean?[ Go to top ]

    Yes, Cactus is fully functional tool, which allows to do unit testing of session beans too.
    A little code example, if you wish:

    public class ExampleServiceTest extends ServletTestCase {

        private ExampleService service;

        public ExampleServiceTest(String name) {
            super(name);
        }

        public static Test suite() {
            return new TestSuite(ExampleServiceTest.class);
        }

        public void setUp() throws Exception {
            Context jndiContext = new InitialContext();
            ExampleServiceHome home = (ExampleServiceHome)
                PortableRemoteObject.narrow(
                    jndiContext.lookup("java:comp/env/ejb/ExampleService"),
                        ExampleServiceHome.class);
            this.service = home.create();
        }

        public void testUserList() throws Exception {
            assertNotNull(service);
            String systemUserName = service.getSystemUserName("test");
            assertNotNull(systemUserName);
        }
    }

    Also you can try a JUnitEE. Also very good toll which may be easy integrated with Cactus.
  3. Re: How to test session bean?[ Go to top ]

    thanx Vladimir.
    currently most of, if not all, solutions for ejb's unit test are servlet-based.
    is there any non-servlet-based solution?

    another question is how to roll-back after test?
    shall we dump sql to database before test or depend on roll back which can be excuted automatically after test?

    thx

    Best Regards,
    Xiao Qing Lai