用 cactus 测试 ejb,如何运行测试代码?
我参照 cactus的文档,写好测试代码代码后,编译也通过了,
ejb也正确的部署,并且可以正常的运行,但是,不知道如何运行 测试代码?
cactus的文档上面说这样来运行:
junit.swingui.TestRunner.main [test case class]
我在命令行输入:
junit.swingui.TestRunner.main ConverterTest
报错:
'junit.swingui.TestRunner.main' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
我这样来测试:
java junit.swingui.TestRunner ConverterTest
运行报错: Failure !!!
cactus的文档上还说的实在没看懂,谁来教教我?
---------------------------------------------------------
cactus 文档上关于如何编写测试代码:
(http://jakarta.apache.org/cactus/writing/howto_ejb_j2eeri.html)
cactus 文档上关于如何运行测试代码:
http://jakarta.apache.org/cactus/integration/index.html
---------------------------------------------------------
ejb 和 cactus 测试代码:
//ConverterTest.java --> 测试代码
//Converter.java --> ejb 的 Remote 接口
//ConverterHome.java --> ejb 的 Home 的接口
//ConverterBean.java --> ejb 的具体实现
//测试代码:ConverterTest.java
import javax.naming.*;
import javax.rmi.*;
import junit.framework.*;
import java.math.*;
import org.apache.cactus.*;
public class ConverterTest extends ServletTestCase
{
private Converter converter;
public ConverterTest(String name)
{
super(name);
}
public static Test suite()
{
return new TestSuite(ConverterTest.class);
}
public void setUp()
{
try
{
Context ctx = new InitialContext();
ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(ctx.lookup("java:comp/ejb/Converter"), ConverterHome.class);
this.converter = home.create();
}
catch ( Exception e )
{};
}
public void testConvert() throws Exception
{
BigDecimal dollar = converter.dollarToYen(new BigDecimal(100.00));
assertEquals("dollar", 1.0, dollar.doubleValue(), 0.01);
}
}
//Converter.java --> ejb 的Remote 接口
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.math.*;
import java.lang.*;
public interface Converter extends EJBObject
{
public BigDecimal dollarToYen(BigDecimal dollars) throws RemoteException;
public BigDecimal yenToEuro(BigDecimal yen) throws RemoteException;
}
//ConverterHome.java --> ejb 的Home 接口
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface ConverterHome extends EJBHome
{
Converter create() throws RemoteException, CreateException;
}
//ConverterBean.java --> ejb 的具体实现
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.math.*;
public class ConverterBean implements SessionBean
{
BigDecimal yenRate = new BigDecimal("2.0000");
BigDecimal euroRate = new BigDecimal("0.0002");
public BigDecimal dollarToYen(BigDecimal dollars)
{
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2,BigDecimal.ROUND_UP);
}
public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2,BigDecimal.ROUND_UP);
}
public ConverterBean() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}