Ejb

2 respostas
robson_vs

Galera gostaria de saber se alguem pode me ajudar estou fazendo uns ejbs e quando e quando vou testalos com un junit me dá o seguinte erro, ja ñ sei mais o q fazer acho q sei + ou - o erro mais ñ consigo resolver se alguem puder me ajudar ficarei muito grato.

Obrigado a todos

2 Respostas

Daniel_Quirino_Olive
  1. Poste seu código para fornecer mais base para nós ajudarmos
  2. Como você está resolvendo as dependências de seu EJB nos unit tests?
robson_vs

Cara vou passar para vcs darem uma olhada em parte do código pois está espalhado em varias camadas

classe de teste

package br.com.greenline.falegreen.junit;

import java.util.List;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import br.com.greenline.falegreen.delegate.FuncionarioDelegate;
import br.com.greenline.falegreen.model.Funcionario;

/**
 * @author Robson Vieira da Silva
 */
public class FuncionarioEJBTest extends TestCase {

	// Mensagens gerais
	private static final String TESTE_OK = "O TESTE REALIZADO COM SUCESSO!!!";

	private static final String TESTE_ERRO = "O TESTE NÃO FOI REALIZADO COM SUCESSO!!!";

	private static FuncionarioDelegate funcionarioDelegate;

	private Funcionario funcionario;

	// Dados para os testes.
	private static final Long FUNCIONARIO_IDENTIFICADOR = new Long(1);

	public FuncionarioEJBTest() {
		super();
	}

	public FuncionarioEJBTest(String testeName) {
		super(testeName);
	}

	public static Test suite() {
		TestSuite suite = new TestSuite("Teste de Funcionario");
		suite.addTest(new FuncionarioEJBTest("testeListar"));
		return suite;
	}

	static {
		funcionarioDelegate = new FuncionarioDelegate();
	}

	public void testeListar() {
		try {
			List funcionarios = funcionarioDelegate.listar();
			assertNotNull(TESTE_OK + "Numero de Elementos: "
					+ funcionarios.size());
		} catch (Exception e) {
			//throw new AssertionFailedError(TESTE_ERRO + e.getMessage());
			e.printStackTrace();
			System.out.println(TESTE_ERRO);
		}
	}
}

classe delegate

package br.com.greenline.falegreen.delegate;

import java.rmi.RemoteException;
import java.util.List;

import javax.ejb.CreateException;
import javax.naming.NamingException;

import br.com.greenline.falegreen.interfaces.Funcionario;
import br.com.greenline.falegreen.interfaces.FuncionarioUtil;
import br.com.greenline.falegreen.servicelocator.ICParams;

/**
 * @author Robson Vieira da Silva
 */
public class FuncionarioDelegate {

	private Funcionario ejb;

	public FuncionarioDelegate() {
		try {
			ejb = FuncionarioUtil.getHome(ICParams.getParams()).create();
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		} catch (CreateException e) {
			throw new RuntimeException(e);
		} catch (NamingException e) {
			throw new RuntimeException(e);
			//e.printStackTrace();
		}
	}

	public void salvar(br.com.greenline.falegreen.model.Funcionario funcionario) {
		try {
			ejb.salvar(funcionario);
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}

	public List listar() {
		try {
			return ejb.listar();
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}

	public void excluir(br.com.greenline.falegreen.model.Funcionario funcionario) {
		try {
			ejb.excluir(funcionario);
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}
}

classe ejb o restante dos ejbs focam gerados pelo x-doclets

package br.com.greenline.falegreen.ejb;

import java.util.List;

import javax.ejb.SessionBean;

import org.apache.log4j.Logger;

import br.com.greenline.falegreen.dao.DAOFactory;
import br.com.greenline.falegreen.dao.FuncionarioDAO;
import br.com.greenline.falegreen.model.Funcionario;

/**
 * @ejb.bean name="Funcionario"
 * jndi-name="fg/Funcionario" 
 * type="Stateless"
 * view-type="remote"
 * 
 * @ejb.permission role-name = "fg"
 * 
 * @ejb.util generate = "physical"
 * 
 * @jboss.container-configuration name = "SSL Clustered Stateless SessionBean"
 * 
 * @jboss.clustered cluster = "true"
 * 
 * @jboss.cluster-config partition-name = "Greenline" bean-policy =
 *                       "org.jboss.ha.framework.interfaces.RoundRobin"
 *                       home-policy =
 *                       "org.jboss.ha.framework.interfaces.RoundRobin"
 * 
 * @author Robson Vieira da Silva
 */
public abstract class FuncionarioEJB implements SessionBean {

	private static Logger log = Logger.getLogger(FuncionarioEJB.class);

	// private Funcionario funcionario = new Funcionario();

	/**
	 * @ejb.interface-method
	 * @return
	 */
	public List listar() {
		if (log.isDebugEnabled()) {
			log.debug("FuncionarioEJB.lista()");
		}

		DAOFactory factory = (DAOFactory) DAOFactory
				.getDAOFactory(DAOFactory.HIBERNATE);

		FuncionarioDAO dao = (FuncionarioDAO) factory.getFuncionarioDAO();

		return dao.listar();
	}

	/**
	 * @ejb.interface-method
	 * @param funcionario
	 * @return
	 */
	public void salvar(Funcionario funcionario) {
		if (log.isDebugEnabled()) {
			log.debug("FuncionarioEJB.salva()");
		}

		DAOFactory factory = (DAOFactory) DAOFactory
				.getDAOFactory(DAOFactory.HIBERNATE);

		FuncionarioDAO dao = (FuncionarioDAO) factory.getFuncionarioDAO();
		dao.salvar(funcionario);
	}

	/**
	 * @ejb.interface-method
	 * @param funcionario
	 * @return
	 */
	public void excluir(Funcionario funcionario) {
		if (log.isDebugEnabled()) {
			log.debug("Funcionario.detelte()");
		}

		DAOFactory factory = (DAOFactory) DAOFactory
				.getDAOFactory(DAOFactory.HIBERNATE);

		FuncionarioDAO dao = (FuncionarioDAO) factory.getFuncionarioDAO();
		dao.excluir(funcionario);
	}
}

e agora o novo erro

log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ExceptionInInitializerError
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.getTest(RemoteTestRunner.java:403)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.RuntimeException: javax.naming.NameNotFoundException: fg/Funcionario
	at br.com.greenline.falegreen.delegate.FuncionarioDelegate.<init>(FuncionarioDelegate.java:28)
	at br.com.greenline.falegreen.junit.FuncionarioEJBTest.<clinit>(FuncionarioEJBTest.java:43)
	... 8 more
Caused by: javax.naming.NameNotFoundException: fg/Funcionario
	at org.jboss.ha.jndi.TreeHead.lookup(TreeHead.java:242)
	at org.jboss.ha.jndi.HAJNDI.lookup(HAJNDI.java:155)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.jboss.ha.framework.server.HARMIServerImpl.invoke(HARMIServerImpl.java:209)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
	at sun.rmi.transport.Transport$1.run(Transport.java:153)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
	at java.lang.Thread.run(Thread.java:595)
	at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
	at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
	at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
	at org.jboss.ha.framework.server.HARMIServerImpl_Stub.invoke(Unknown Source)
	at org.jboss.ha.framework.interfaces.HARMIClient.invokeRemote(HARMIClient.java:172)
	at org.jboss.ha.framework.interfaces.HARMIClient.invoke(HARMIClient.java:267)
	at $Proxy0.lookup(Unknown Source)
	at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
	at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
	at javax.naming.InitialContext.lookup(InitialContext.java:351)
	at br.com.greenline.falegreen.interfaces.FuncionarioUtil.lookupHome(FuncionarioUtil.java:20)
	at br.com.greenline.falegreen.interfaces.FuncionarioUtil.getHome(FuncionarioUtil.java:49)
	at br.com.greenline.falegreen.delegate.FuncionarioDelegate.<init>(FuncionarioDelegate.java:22)
	... 9 more
Criado 14 de agosto de 2006
Ultima resposta 14 de ago. de 2006
Respostas 2
Participantes 2