aspose file tools*
The moose likes Testing and the fly likes little help with start ejb testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "little help with start ejb testing" Watch "little help with start ejb testing" New topic
Author

little help with start ejb testing

Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
Hi,

I am new to jUnit and I need to test lots of someones code, and I need to start from somewhere. I have do lots of reading, but I cant find similar examle that was tested like I have. I now that I need to test myEjb method LocationProfileVO getLocationProfile(int aLocationNumber) wich is calling DAO objects, but I dont now what are my next steps.

Could anyone give me an example steps based on my LocationServiceBean, so I can do the whole app by my self?





Thanks.

p.s. my IDE is Eclipse, and I have downladed jUnit4.4.jar and put it in my library.

thanks

[ October 03, 2007: Message edited by: Marko Debac ]

Hi, this is my trying:


thanks.

[ October 03, 2007: Message edited by: Marko Debac ]

Hi, my second trying, and wierd errors:


and after I goes at LocationServiceBeanTest file, run As, JUnit Test, a get strange error:

java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:425)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


p.s. I have added MockEjb jars into app.

Thanks.
[ October 03, 2007: Message edited by: Marko Debac ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I would do something like the following with JUnit and EasyMock.

Note that I wrote this off-the-cuff into the editor so it might not even compile. I just wanted to illustrate how you might use interaction-based testing with a tool like EasyMock to test that the EJB does what you expect it to do.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
Hi Lasse,

question: if we test ejb, shouldnt we call it instance over jndi? You didnt in above example? How can we do this with easymock (because we dont want do our testing on the server)? Can we do everything with easymock (because we want to test a servlets to)?

Another question:

We need to test statefull ejb wich has one function where he call simple function on another stateless ejb - we are doing simple forwarding of parameters. Can you show us example with easymock? Should we do jndi call 2 times - for each ejb?


About jndi:

we have defined

at , and we calling this stateles ejb (only local) from statefull


this is function to test


// private UserProfileVO userProfile; - is some class

and how we know (example for this case) what test to do? Maybe for input parameters...or for something else?

Thanks.
[ October 10, 2007: Message edited by: Marko Debac ]
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
Hi,
what I am trying to do is: (i.e. user session stateful bean calling service manager stateles bean)


is this setup ok?
I am getting error.. java.lang.NullPointExeption
java.lang.NullPointerException
at pack.server.ejb.UserSessionBean.getValueObjectVector(UserSessionBean.java:172)
at pack.server.ejb.test.UserSessionBeanTest.testValueObjectVectorIsAcquiredUsingServiceManagerBean(UserSessionBeanTest.java:92)
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 junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



this is test:



please help, because we still struggleing to learn tests.
Thanks.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Marko Debac:
Can we do everything with easymock (because we want to test a servlets to)?

You can create dynamic mock objects with EasyMock for a given interface or class. If that's what you need for testing whatever you want to test then the answer is "yes".

Unit testing Servlets, for example, requires instances of the javax.servlet.http.* interfaces that you can either implement manually, generate with EasyMock (or similar tools), or you can grab implementations from existing mock object libraries (Spring Framework, for example, includes mock implementations for many of those interfaces).

Originally posted by Marko Debac:
if we test ejb, shouldnt we call it instance over jndi?

Obtaining the bean instance over JNDI would be done by the client anyway, right? So it's not really testing the EJB as such.

Now, having said that...
Originally posted by Marko Debac:
We need to test statefull ejb wich has one function where he call simple function on another stateless ejb - we are doing simple forwarding of parameters. Can you show us example with easymock? Should we do jndi call 2 times - for each ejb?

This is a case where you should consider faking the JNDI tree. That is, you'd still create an instance of the stateful bean class with "new MyStatefulBean()" but you'd let MyStatefulBean lookup MyStatelessBean from JNDI.

For faking the JNDI tree, take a look at the bonus chapter of my book (pages 10-13).
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ah, I just realized that you're already faking the JNDI tree...

Originally posted by Marko Debac:
I am getting error.. java.lang.NullPointExeption
java.lang.NullPointerException
at pack.server.ejb.UserSessionBean.getValueObjectVector(UserSessionBean.java:172)
at pack.server.ejb.test.UserSessionBeanTest.testValueObjectVectorIsAcquiredUsingServiceManagerBean(UserSessionBeanTest.java:92)
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 junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

This seems to be because your test is not invoking ejbCreate() and therefore your bean instance never obtains its "serviceManager". Think of your tests as simulating the EJB container. One of the things that your tests need to do is invoke these lifecycle methods in the appropriate places.
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
Hi,

I have added userSessionBean.ejbCreate();, but new error come up (jndi initialize i suppose)..



And this is at the console output




In the real application, before we are accesing beans operation we are initialize contextProperties for weblogic, but for testing I cant see the connection, because we dont want do testing at the server

So, this is access to the real ServiceManagerBean from UserSessionBean:


Thanks.


And another:
1.When we are testing bean who calls another bean - we must call two jndi_name for each -
a)through context.bind for bean whos function is calling, and
b)in the sessionDescriptor for testing bean

2. When we are testing bean who calls POJOs (like you did upper on DAO objects example) - we shoudnt have call jndi_name not even once

??

Thanks.
[ October 11, 2007: Message edited by: Marko Debac ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: little help with start ejb testing