Topic: 如何用junit测试EJB组件

  Print this page

1.如何用junit测试EJB组件 Copy to clipboard
Posted by: NameNick
Posted on: 2003-10-14 13:04

我将一个Ejb对象(ConverEjb)部署成功了。然后我写了一个测试该Ejb对象的junit类。我在junit类的setup方法中创建Ejb对象的调用,但运行junit测试时报错。我的junit类的文件名为EjbTest.java内容如下:
import junit.framework.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.math.BigDecimal;

public class EjbTest extends TestCase {

protected BigDecimal ExpectedValue;
protected BigDecimal param;
protected BigDecimal AMount;

public EjbTest(String name) {
super(name);
}
  
public void TestMyEjb() {

}

protected void setUp() {
ExpectedValue = new BigDecimal ("1216.00");
param = new BigDecimal ("10.00");
AMount = new BigDecimal ("10.00");
try {
Context initial = new InitialContext();
Context myEnv = (Context)initial.lookup("java:comp/env");
Object objref = myEnv.lookup("ejb/SimpleConverter");
ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(objref, ConverterHome.class);
Converter currencyConverter = home.create();
AMount=currencyConverter.dollarToYen(param);
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}

protected void tearDown() {
System.exit(0);
}

public boolean equals(BigDecimal Excepted,BigDecimal Value2) {
return true ;
}


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

TestSuite suite= new TestSuite();
suite.addTest(new EjbTest("testdollarToYen"));
return suite;

}

public void testdollarToYen() {
assertEquals(ExpectedValue, AMount);
}
}

报错内容为:

C:\junit3.8.1\junit\tests>java junit.textui.TestRunner EjbTes
.Caught an unexpected exception!
javax.naming.NameNotFoundException: No object bound for java:
at com.sun.enterprise.naming.java.javaURLContext.look
ava:116)
at javax.naming.InitialContext.lookup(InitialContext.
at EjbTest.setUp(EjbTest.java:27)
at junit.framework.TestCase.runBare(TestCase.java:125
at junit.framework.TestResult$1.protect(TestResult.ja
at junit.framework.TestResult.runProtected(TestResult
at junit.framework.TestResult.run(TestResult.java:109
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.start(TestRunner.java:172)
at junit.textui.TestRunner.main(TestRunner.java:138)

2.Re:如何用junit测试EJB组件 [Re: NameNick] Copy to clipboard
Posted by: charleszq
Posted on: 2003-10-14 14:54

As my point of view, this is nothing to do with JUnit. Please check the JNDI name of your bean.

BTW, the method in your TestCase 'TestMyEjb' is not correct, you should rename the name to testMyEjb.

3.Re:如何用junit测试EJB组件 [Re: NameNick] Copy to clipboard
Posted by: why
Posted on: 2003-10-14 19:35

Have you tried Catcus of Jakarta?
http://jakarta.apache.org/cactus/writing/howto_ejb.html

There's an article on TSS, actually an excerpt from a book called "Jakarta Pitfalls", which may be helpful.

Try and share your experience with us. Big Smile

4.Re:如何用junit测试EJB组件 [Re: NameNick] Copy to clipboard
Posted by: hitaco
Posted on: 2003-10-16 16:27

我碰到的问题是用juit测试数据库应用,如何保证每次setup时候数据都是一样的,前提是数据库数据量太大,不太可能在setup里完成init操作

5.Re:如何用junit测试EJB组件 [Re: NameNick] Copy to clipboard
Posted by: NameNick
Posted on: 2003-10-16 16:40

thanks.

6.Re:如何用junit测试EJB组件 [Re: NameNick] Copy to clipboard
Posted by: ajinjin
Posted on: 2003-10-16 18:48

what mean

7.Re:如何用junit测试EJB组件 [Re: NameNick] Copy to clipboard
Posted by: floater
Posted on: 2003-10-16 22:38

There are two ways to set up your data:

1. Use ANT in conjunction with JUNIT (You really should use catcus for EJB ). Check the book, java extreme tool.

2. Use TestSetup class in JUNIT, check JUNIT doc. It will run once and only once during the entire test run.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923