How to test session bean?


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?

    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) {

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

        public void setUp() throws Exception {
            Context jndiContext = new InitialContext();
            ExampleServiceHome home = (ExampleServiceHome)
            this.service = home.create();

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

    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?


    Best Regards,
    Xiao Qing Lai