You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Markus Wolf <ma...@emedia-solutions-wolf.de> on 2007/02/22 19:48:55 UTC

Unit Testing EJB with Junit4

Hi,

I have a problem using OpenEJB as JUnit4 container for my EJBs.
I use Java EE 5 and want to setup a unit test environment for this.
I follow the examples on the website with the following JUnit config:

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		Properties properties = new Properties();
		properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.apache.openejb.client.LocalInitialContextFactory");
		properties.setProperty("openejb.deployments.classpath.include",
				".*emediaservices.*");
		properties.setProperty("openejb.embedded.remotable", "true");
		// Uncomment these properties to change the defaults
		// properties.setProperty("openejb.ejbd.port", "4201");
		// properties.setProperty("openejb.ejbd.bind", "localhost");
		// properties.setProperty("openejb.ejbd.threads", "200");
		// properties.setProperty("openejb.ejbd.disabled", "false");
		// properties.setProperty("openejb.ejbd.only_from",
		// "127.0.0.1,192.168.1.1");

		new InitialContext(properties);
	}

But when I run any test in this class I get the following exception. The
class which java complains about varies, but the message and the
stacktrace are always the same:

lang.ClassFormatError: Absent Code attribute in method that is not
native or abstract in class file javax/resource/ResourceException
	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)

Any hint what is causing this?

Thanks
Markus
-- 
>
> emedia-solutions wolf    web: http://www.emedia-solutions-wolf.de
> Wedeler Landstrasse 63  mail: markus@emedia-solutions-wolf.de
> 22559 Hamburg            pgp: http://wwwkeys.de.pgp.net
> (040) 550 083 70       taxid: 57 / 126 / 16123
>

Re: Unit Testing EJB with Junit4

Posted by Markus Wolf <ma...@emedia-solutions-wolf.de>.
Hi David,

> Well, if the above approach of removing the sun javaee api jar doesn't
> fix things than perhaps there is something going on with JPA related
> class enhancing, but I'd guess the "do not use at runtime" jar is the
> bigger suspect.
> 
I'll tracked it down. :)
You mentioning about class enhancing let me thought about it. I'll have
to preenhance my classes for OpenJPA to have them available in unit
tests. I haven't done till now, because deploying into my appserver is
enhancing at runtime.
Thanks for your help. It does work pretty well now.

Markus
-- 
>
> emedia-solutions wolf    web: http://www.emedia-solutions-wolf.de
> Wedeler Landstrasse 63  mail: markus@emedia-solutions-wolf.de
> 22559 Hamburg            pgp: http://wwwkeys.de.pgp.net
> (040) 550 083 70       taxid: 57 / 126 / 16123
>

Re: Unit Testing EJB with Junit4

Posted by David Blevins <da...@visi.com>.
On Feb 22, 2007, at 4:50 PM, Markus Wolf wrote:

> Hi David,
>
>>> [...]
>>
>> Just a note on the "openejb.embedded.remotable" flag.  You don't  
>> need to
>> enable that and open ports if you're doing all your testing in the  
>> same
>> vm.  You really only need the "openejb.embedded.remotable" flag on if
>> you have clients connecting from another VM, which is probably  
>> something
>> you'd do more if OpenEJB was embedded in an application that  
>> wanted to
>> have an embedded EJB container to add remoting capabilities.
>>
>> To say it another way, it doesn't have anything to do if you want to
>> test local interfaces vs remote interfaces.  Using remote  
>> interfaces to
>> beans in the same vm as you works just fine without the network stuff
>> enabled.
>>
>> I don't really know JBoss at all, but I get the feeling their  
>> embedded
>> testing stuff doesn't work for people who are using remote interfaces
>> without opening sockets and the like -- which apparently they  
>> don't have
>> support for yet and there for can't test remote interfaces.  If  
>> someone
>> could confirm this that'd be really helpful as we'd probably want to
>> make an extra strong effort to document that things do not work  
>> this way
>> in OpenEJB.  At least it would be a big help in understand where  
>> users
>> are coming.
>>
> I've tried with both ways and none of it does work. So it does not
> depend on the jvm/remote/port stuff, but that was not what I thought.

Good to know.  I recall at least one other person suspecting the  
exact same thing.

>
>>> [...]
>>
>> Now that is truly strange.  Hmm.. don't know what could cause a
>> ClassFormatError on that class (or any class).  Best thing we can  
>> do is
>> start eliminating possibilities.  Hope you can follow my little debug
>> logic tree here :)
>>
> I've found a hint on a sun blog entry for this exception. There was  
> said
> that the javaee5.jar I have in my maven dependencies is just for
> compiling, but I think having the api besides the implementation
> dependencies of OpenEJB would not cause trouble. I maybe can remove  
> this
> jar and try again, but I guess that would not help.

I'd definitely try that.  The error is invalid class def for a J2CA  
API class so it's definitely worth trying.  If they are in the  
classpath then OpenEJB will use them at unit test time.  I'm not sure  
what they could do to a class (especially an exception) that would  
make them warn against using the library at runtime, but considering  
that's such a strange request to make they must have some kind of  
reason.

Looking at your classpath in the maven output you posted, that seems  
like the only possible culprit of who could be adding an invalid def  
of that class.

>
>> 1.  Have you been able to run any of the other examples (like  
>> calculator
>> maybe)?  If so and they run successfully, try adding a line that  
>> would
>> case ResourceException to be loaded.  Something innocuous like  
>> "Class c
>> = javax.resource.ResourceException.class;"
> I have written a simple stateless 'echo'-Bean to test if OpenEJB does
> work at all in maven and junit and that does work very well.

That's great.  I'm sure we can get the rest working for you too.

>
>>   1.a. If that doesn't work and you get the same error, then it's  
>> likely
>> that you somehow got a bad jar in your maven repo and you'd likely  
>> have
>> to delete your repo (or back it up) and have maven download  
>> everything
>> anew.
> The jars in my path should be ok, because if I deploy it into my
> appserver everything does work, but deploying untested code gives me a
> bad feeling.

That's totally fine.  That kind of action is more a last resort.   
FYI, if your app server is glassfish we do have experimental support  
for their descriptors so if there's something you notice that isn't  
getting picked up, definitely let us know and we'll fix it.

>>   1.b. If that does work, then there must be something being  
>> included in
>> your maven pom perhaps pulled in by one of your dependencies.  In  
>> which
>> case run maven with -X to have it spit out all the classpath and  
>> debug
>> information and post it.  Easy way would be 'mvn -X clean install >
>> output.log'
>>
> I'll can do that and post it here. That will take some minutes.
>
>> And just for good measure, what kind of beans are you running?
>>
> I'm using stateless and stateful beans which use an EntityMananger  
> with
> annotation injection as stated in the JavaEE5 API.
> Could the problem related to the dependency injection?

Well, if the above approach of removing the sun javaee api jar  
doesn't fix things than perhaps there is something going on with JPA  
related class enhancing, but I'd guess the "do not use at runtime"  
jar is the bigger suspect.

-David




Re: Unit Testing EJB with Junit4

Posted by Markus Wolf <ma...@emedia-solutions-wolf.de>.
Hi David,

>> [...]
> 
> Just a note on the "openejb.embedded.remotable" flag.  You don't need to
> enable that and open ports if you're doing all your testing in the same
> vm.  You really only need the "openejb.embedded.remotable" flag on if
> you have clients connecting from another VM, which is probably something
> you'd do more if OpenEJB was embedded in an application that wanted to
> have an embedded EJB container to add remoting capabilities.
> 
> To say it another way, it doesn't have anything to do if you want to
> test local interfaces vs remote interfaces.  Using remote interfaces to
> beans in the same vm as you works just fine without the network stuff
> enabled.
> 
> I don't really know JBoss at all, but I get the feeling their embedded
> testing stuff doesn't work for people who are using remote interfaces
> without opening sockets and the like -- which apparently they don't have
> support for yet and there for can't test remote interfaces.  If someone
> could confirm this that'd be really helpful as we'd probably want to
> make an extra strong effort to document that things do not work this way
> in OpenEJB.  At least it would be a big help in understand where users
> are coming.
> 
I've tried with both ways and none of it does work. So it does not
depend on the jvm/remote/port stuff, but that was not what I thought.

>> [...]
> 
> Now that is truly strange.  Hmm.. don't know what could cause a
> ClassFormatError on that class (or any class).  Best thing we can do is
> start eliminating possibilities.  Hope you can follow my little debug
> logic tree here :)
> 
I've found a hint on a sun blog entry for this exception. There was said
that the javaee5.jar I have in my maven dependencies is just for
compiling, but I think having the api besides the implementation
dependencies of OpenEJB would not cause trouble. I maybe can remove this
jar and try again, but I guess that would not help.

> 1.  Have you been able to run any of the other examples (like calculator
> maybe)?  If so and they run successfully, try adding a line that would
> case ResourceException to be loaded.  Something innocuous like "Class c
> = javax.resource.ResourceException.class;"
I have written a simple stateless 'echo'-Bean to test if OpenEJB does
work at all in maven and junit and that does work very well.

>   1.a. If that doesn't work and you get the same error, then it's likely
> that you somehow got a bad jar in your maven repo and you'd likely have
> to delete your repo (or back it up) and have maven download everything
> anew.
The jars in my path should be ok, because if I deploy it into my
appserver everything does work, but deploying untested code gives me a
bad feeling.

>   1.b. If that does work, then there must be something being included in
> your maven pom perhaps pulled in by one of your dependencies.  In which
> case run maven with -X to have it spit out all the classpath and debug
> information and post it.  Easy way would be 'mvn -X clean install >
> output.log'
> 
I'll can do that and post it here. That will take some minutes.

> And just for good measure, what kind of beans are you running?
> 
I'm using stateless and stateful beans which use an EntityMananger with
annotation injection as stated in the JavaEE5 API.
Could the problem related to the dependency injection?

-- 
>
> emedia-solutions wolf    web: http://www.emedia-solutions-wolf.de
> Wedeler Landstrasse 63  mail: markus@emedia-solutions-wolf.de
> 22559 Hamburg            pgp: http://wwwkeys.de.pgp.net
> (040) 550 083 70       taxid: 57 / 126 / 16123
>

Re: Unit Testing EJB with Junit4

Posted by Markus Wolf <ma...@emedia-solutions-wolf.de>.
>   1.b. If that does work, then there must be something being included in
> your maven pom perhaps pulled in by one of your dependencies.  In which
> case run maven with -X to have it spit out all the classpath and debug
> information and post it.  Easy way would be 'mvn -X clean install >
> output.log'
> 
The maven log is attached. There is a bunch of dependencies, but most
are included by openejb for testing.

-- 
>
> emedia-solutions wolf    web: http://www.emedia-solutions-wolf.de
> Wedeler Landstrasse 63  mail: markus@emedia-solutions-wolf.de
> 22559 Hamburg            pgp: http://wwwkeys.de.pgp.net
> (040) 550 083 70       taxid: 57 / 126 / 16123
>

Re: Unit Testing EJB with Junit4

Posted by David Blevins <da...@visi.com>.
On Feb 22, 2007, at 10:48 AM, Markus Wolf wrote:

> Hi,
>
> I have a problem using OpenEJB as JUnit4 container for my EJBs.
> I use Java EE 5 and want to setup a unit test environment for this.
> I follow the examples on the website with the following JUnit config:
>
> 	@BeforeClass
> 	public static void setUpBeforeClass() throws Exception {
> 		Properties properties = new Properties();
> 		properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
> 				"org.apache.openejb.client.LocalInitialContextFactory");
> 		properties.setProperty("openejb.deployments.classpath.include",
> 				".*emediaservices.*");
> 		properties.setProperty("openejb.embedded.remotable", "true");
> 		// Uncomment these properties to change the defaults
> 		// properties.setProperty("openejb.ejbd.port", "4201");
> 		// properties.setProperty("openejb.ejbd.bind", "localhost");
> 		// properties.setProperty("openejb.ejbd.threads", "200");
> 		// properties.setProperty("openejb.ejbd.disabled", "false");
> 		// properties.setProperty("openejb.ejbd.only_from",
> 		// "127.0.0.1,192.168.1.1");
>
> 		new InitialContext(properties);
> 	}

Just a note on the "openejb.embedded.remotable" flag.  You don't need  
to enable that and open ports if you're doing all your testing in the  
same vm.  You really only need the "openejb.embedded.remotable" flag  
on if you have clients connecting from another VM, which is probably  
something you'd do more if OpenEJB was embedded in an application  
that wanted to have an embedded EJB container to add remoting  
capabilities.

To say it another way, it doesn't have anything to do if you want to  
test local interfaces vs remote interfaces.  Using remote interfaces  
to beans in the same vm as you works just fine without the network  
stuff enabled.

I don't really know JBoss at all, but I get the feeling their  
embedded testing stuff doesn't work for people who are using remote  
interfaces without opening sockets and the like -- which apparently  
they don't have support for yet and there for can't test remote  
interfaces.  If someone could confirm this that'd be really helpful  
as we'd probably want to make an extra strong effort to document that  
things do not work this way in OpenEJB.  At least it would be a big  
help in understand where users are coming.

>
> But when I run any test in this class I get the following  
> exception. The
> class which java complains about varies, but the message and the
> stacktrace are always the same:
>
> lang.ClassFormatError: Absent Code attribute in method that is not
> native or abstract in class file javax/resource/ResourceException
> 	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)
>
> Any hint what is causing this?

Now that is truly strange.  Hmm.. don't know what could cause a  
ClassFormatError on that class (or any class).  Best thing we can do  
is start eliminating possibilities.  Hope you can follow my little  
debug logic tree here :)

1.  Have you been able to run any of the other examples (like  
calculator maybe)?  If so and they run successfully, try adding a  
line that would case ResourceException to be loaded.  Something  
innocuous like "Class c = javax.resource.ResourceException.class;"
   1.a. If that doesn't work and you get the same error, then it's  
likely that you somehow got a bad jar in your maven repo and you'd  
likely have to delete your repo (or back it up) and have maven  
download everything anew.
   1.b. If that does work, then there must be something being  
included in your maven pom perhaps pulled in by one of your  
dependencies.  In which case run maven with -X to have it spit out  
all the classpath and debug information and post it.  Easy way would  
be 'mvn -X clean install > output.log'

And just for good measure, what kind of beans are you running?


-David