users@glassfish.java.net

Re: [Fwd: [embedded] How to unit test an EJB with Maven ?]

From: <glassfish_at_javadesktop.org>
Date: Fri, 04 Sep 2009 04:09:00 PDT

Hello

I'm in the same problem. I'm trying now sins over a week to get this working and i'm having the same errors. so do not post them, also my application is the same It is the Chapter example for the book [b]Java EE6 Platform with GlassFish 3[/b]. I'm using Glassfish-embedded-all version 3.0-b62

My testclass

public class ClientEJBTest {

    private static EJBContainer ec;
    private static Context ctx;

    public ClientEJBTest() {
    }

    @Test
    //_at_Ignore("EJBContainer not implemented yet")
    public void CreateClient() throws Exception{

        ec = EJBContainer.createEJBContainer();
        ctx = ec.getContext();

        System.out.println("createClient");
        Client client = new Client();

        client.setCName("LexMS");
        client.setCIsDefault(1);

        ClientEJB clientEJB = (ClientEJB) ctx.lookup("java:global/ClientEJB");

        client=clientEJB.createClient(client);
        assertNotNull("ID should not be null", client.getCid());

        ec.close();
    }

}

My ClientEJB

@Stateless
public class ClientEJB implements ClientEJBRemote {

    @PersistenceContext
    private EntityManager em;

    @Override
    public Client createClient(Client client) {
        em.persist(client);
        return client;
    }

    @Override
    public void deleteClient(Client client) {
        em.remove(client);
    }

    @Override
    public Client getClientById(Long cid) {
        Client client = new Client();

        if (cid == null) {
            client = getDefaultClient();
        } else {
            try {
                Query q = em.createNamedQuery("Client.getClientById");
                q.setParameter("cid", cid);
                if (q != null) {
                    client = (Client) q.getSingleResult();
                    if (client != null) {
                        return client;
                    }
                }

            } catch (NonUniqueResultException e) {
            } catch (NoResultException e) {
            }
        }

        return client;
    }

    @Override
    public Client getDefaultClient() {

        Client client = new Client();
        try {
            Query q = em.createNamedQuery("Client.getDefaultClient");
            if (q != null) {
                client = (Client) q.getSingleResult();
                if (client != null) {
                    return client;
                }
            }

        } catch (NonUniqueResultException e) {
        } catch (NoResultException e) {
        }

        return client;
    }

    @Override
    public Client updateClient(Client client) {
        em.merge(client);
        return client;
    }
}

Can some one please help me.

kind regards

Martin
[Message sent by forum member 'martin_wh99' (martin.krueger_at_bluewin.ch)]

http://forums.java.net/jive/thread.jspa?messageID=362933