This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes EJB3 Testing - Comments on this implementation and suggestions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3 Testing - Comments on this implementation and suggestions?" Watch "EJB3 Testing - Comments on this implementation and suggestions?" New topic
Author

EJB3 Testing - Comments on this implementation and suggestions?

Brick Riccardi
Greenhorn

Joined: Jun 01, 2008
Posts: 22
I've been searching the forums and google and I'm interested in others how others test their stateless session beans and JPA entities.

My current approach follows the idea presented in this forum post:
http://escx.blogspot.com/2007/03/testing-ejb3-jpa-with-maven2.html but I'm not sure that is the best approach? I was curious how to make sure things worked nicely when SLSBs call other SLSBs, and I also saw the entityManager initializing being somewhat common code, so if someone can review how I'm doing this in junit here I'd appreciate it (it's very short):

[URL= http://pastie.org/293219] http://pastie.org/293219[/url]

Is the above a bad approach? (Maven runs the tests)

Is a better approach to maybe stand up an instance of my server (JBoss5) and have some manually clickable JMX components or even JSP links for testing some things?

I'm curious how others handle testing. (By the way, I did look at EJB3Unit and thought it was overly complicated and the documentation was extremely sparse.) Thanks for any help.
Brick Riccardi
Greenhorn

Joined: Jun 01, 2008
Posts: 22
One thing I just realized is, it's stupid to even extend that BaseTest since the BeforeClass and AfterClass seem to have to be static, so the code is modified to just not extend BaseTest and injection of the entityManager is just

userService.setEm(BaseTest.em);

There must be a better way to handle what I'm trying in JUnit though?
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Brick,

Unfortunately, I don't have time to do a detailed write-up of this at the moment. Believe it or not, we have devoted an entire chapter in the second edition plan for unit and integration testing. Here are brief pointers you can look into:

1. Basic unit/integration testing in EJB 3 is a no-brainer since everything is a POJO and mockable.
2. For integration testing with container services (e.g. DI, AOP, transactions, persistence) look into using an embeddable container like OpenEJB, EasyBeans or Embedded JBoss in your unit tests. Be careful with this, however, you can accidentally start many container instances and bring your machine to its knees. As long as you start only a few embeddable container instances, things work very nicely. You can integrate this via Maven, ANT or anything else you want.

2. If you are looking for tighter integration with JUnit, look into having Spring JUnit support APIs in conjunction with an embedded container. If you are an experienced server-side developer, it won't take you long to figure out how to have Spring inject stuff from a JNDI context. This particular Spring feature is a little obscurely documented, but works just fine. Take a look at the TSS thread here: http://www.theserverside.com/news/thread.tss?thread_id=51081.

That's all I can do on this large topic right now :-).

Hope it helps,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Johann Dobbins
Ranch Hand

Joined: Oct 16, 2008
Posts: 62
is everything mockable?

say i have XInterface which is implemented by XSlsb. XSlsb injects a reference to YSlsb. I would like to test XSlsb, so i use jmock to mock YSlsb. However, how do i set XSlsb.YSlsb to the mocked slsb? i have to include a XSlsb.setYSlsb(...) method? which is acceptable, but i believe i also have to expose this method in XInterface. This seems questionable, to be exposing an internal setter used only for testing purposes in my business interface?

I'm very new to this, your thoughts would be greatly appreciated...

thank you
[ October 16, 2008: Message edited by: Johann Dobbins ]
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Johann,

For the case of "internal" unit testing as you described, I don't see a particular need to expose it via an interface. This really just does back to standard techniques applied to unit testing any POJO.

Cheers,
Reza
Brick Riccardi
Greenhorn

Joined: Jun 01, 2008
Posts: 22
Originally posted by Reza Rahman:
[QB]Brick,

Believe it or not, we have devoted an entire chapter in the second edition plan for unit and integration testing. /QB]


Ah, So I have to wait for the second edition I already have your first edition and think it's excellent by the way. (Is there a beta version draft of the testing chapter available to read?)

Actually what I came up with is using TestNG in the following manner:
(mind you I might change things a bit for now the blow seems to work pretty well. Any recommendations/comments would be appreciated on how to make it better.)

testng.xml




Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Brick,

Thanks for the kind words on the book. What you have looks good.

However, what you are doing also looks more in the realm of integration testing on a larger scale. I would consider using an embedded container like OpenEJB, EasyBeans or Embedded JBoss. Personally, I prefer OpenEJB because they are highly responsive in their community and the container is super-lightweight. It may also be worth your time to take a look at something like EasyGloss if all you are interested in is basic EJB 3 injection.

As I said in the earlier post, I would also consider using Spring support for JUnit/TestNG in conjunction with an EJB 3 embedded container (it's a lot easier than it sounds). I have content devoted to this in a talk I am giving at a seminar in CT in a couple of days: http://www.ctjava.org/. I am sure the seminar will post my demo code and slides afterwards.

As to the testing chapter in the second edition, it is still quite a ways away since we are still in the early planning stages.

My new co-author for the second edition, Rob Di Marco, did some work on using Embedded JBoss for unit testing: http://www.innovationontherun.com/presentations. Feel free to email and ask him for his content.

Cheers,
Reza

P.S.: If I might ask a favor, would you be kind enough to review the book on Amazon? The reviews so far have been great (a lot of five stars), but we have just a handful, especially compared to other titles that we are at par with or better than IMHO.
Brick Riccardi
Greenhorn

Joined: Jun 01, 2008
Posts: 22
Sure, I'll definitely give the book a nice review on Amazon.

Thanks for those links, I think I'll look into the embedded JBoss route since I'm going to be using JBoss5 for this project anyway. Alghough I suppose I'll look at OpenEJB (if it's as easy or easier to set up?) I briefly looked at the slides from the embedded jboss talk and that looks pretty cool. (Although I still think for unit testing the daos it might make sense to do it using the pojo approach I'm taking with the EntityManger being injected. I'll have to do more research.) I definitely look forward to checking out your presentation as well after you give it.

Now in regard to Spring, I really have mixed feelings about it. I'll have to look again at how that will really help with the EJB3 testing. Ironically, when I was about to start this project I was debating between a Spring/Hibernate/JPA/Tomcat approach, but as I started getting into the configuration I quickly realized that setting this all JPA stuff up with Spring for Tomcat was NO WAY easier than getting up and running on JBoss. I understand that Spring has some huge advantages (especially in regards to testing), but I've been around and around with others debating about how much 'better' it is than coding and EJB3 application. (You can follow a lot of the debate which started here http://java.dzone.com/articles/the-cost-springsource-enterpri ).

I'm all about trying to keep things as simple as possible and after doing some Rails stuff I definitely prefer convention over configuration now. It seems like with Spring it's just one more thing I have to work on configuring and getting setup. I'm certainly not opposed to it, but it has to give me some clear worthwhile advantages. For example you mentioned earlier: "it won't take you long to figure out how to have Spring inject stuff from a JNDI context." I suppose this will be a bit cleaner than what I'm doing, but do I help things 'that' much? Using Spring I'd get rid of this call in my subclasses I suppose:
setUp(BaseTest.EM_TEST_HSQLDB);
But to me that's actually quite clear and I'm not needing Spring to handle it. I'm sort of rambling now, so I should up and try to see what Spring will give me.

I also looked over the article you posted on the serverside in regard to standardized jndi names - a good thing - but it didn't really get into anything with Spring too much.

I'm certainly not opposed to Spring and use it for the DAOs when I code my iBATIS applications. If you know of any links that will illustrate how it helps in the EJB3 world for testing, I'd appreciate you posting them (I'll be googling some later on tonight or tomorrow.) (Or I'll just wait to check out the notes from your presentation
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Brick,

I saw your review...thanks so much for writing it!

I share your mixed feelings about Spring.

I can honestly say I would not seriously consider learning it if I had not been using it since Spring 1.0 and have a lot of clients that have pre-existing applications in Spring and developers that are already familiar with it. Hands down EJB 3 applications are a lot cleaner, easier to get started with and easier to maintain. EJB 3 is without a doubt a lot easier to learn.

For a couple of newer clients, I've been using EJB 3 as the primary framework with Spring APIs only where needed (such as Spring JDBC, Spring JMS template and Spring JUnit/TestNG support). In theory, bare @Components configured Spring beans, some advanced DI features and AOP could be useful, but the scenario has not come up yet. As you mentioned, iBATIS boilerplate setup and Quartz setup can be useful in an EJB 3 application too, but again I have not done it yet.

I think in your case, it makes the most sense to use embedded JBoss since you are using JBoss 5. As I said, I like OpenEJB primarily because of active community support and unit test start-up performance (mere seconds in most cases). I really think you should give EasyGloss a spin too. You'll cut down on *all* the custom code you wrote...

Cheers,
Reza
Brick Riccardi
Greenhorn

Joined: Jun 01, 2008
Posts: 22
Originally posted by Reza Rahman:
I really think you should give EasyGloss a spin too. You'll cut down on *all* the custom code you wrote...


Wow *all*, I definitely will be checking EasyGloss out then. I thought I was up on things and yet this is the first time I've heard of it. Thanks.
Brick Riccardi
Greenhorn

Joined: Jun 01, 2008
Posts: 22
Originally posted by Reza Rahman:
I really think you should give EasyGloss a spin too. You'll cut down on *all* the custom code you wrote...


I've been looking over the usage examples/docs for EasyGloss that you see here https://easygloss.dev.java.net/#usage and I really don't see how this cuts down on custom code at all. It actually looks like I might not more lines of code, but I might be missing something?

The only thing I see it saving is that using easyGloss I wouldn't have to create any setters in my ejbs. (But adding a setter is so simple, that I think that part is negligible.)

In EasyGloss you still need to write all the code to add the ejbs to gloss (ie gloss.addEJB(someEJB). This doesn't seem to save the much over just calling the setter of the ejbs you care about for a particular unit test.

In regard to setting up the EntityManager you still need to do:

JavaEEGloss gloss = new JavaEEGloss();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("fred");
EntityManager em = emf.createEntityManager();
gloss.addGenericEM(em);

which I'd be doing only once in my Base class as well. The difference here is, using EasyGloss I wouldn't need to have to inject the EntityManager manually with "myEjb.setEntityManager(em)" however I'd still have to add my ejb to gloss with gloss.addEJB(myEJB)so it's still one line I'd have to write either way.

I'll look at it some more though.
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Rick,

Totally up to you but imagine all the setters you'd do away with on a larger project...

Regards,
Reza
Brick Riccardi
Greenhorn

Joined: Jun 01, 2008
Posts: 22
As a side note, I've been looking at using Embedded JBoss for my testing, and I have to admit - it's extremely frustrating. The wiki doesn't provide any clear examples. The downloadable application does show some unit testing using it, but it's confusing about what practice to follow and the wiki mentions things not shown in the example code. Also if look online how people are using it, it varies widely (the link to the example you gave Reza for Rob's stuff is cool, but I think it might be sort of outdated?) I'm hearing if you are using JBoss5 things might work differently in regard to what you need to use. Overall, it's just a painful endeavor as is a lot of JEE lately.


Actually as more of a sidenote/rant this is why I'm growing and more and more weary of Java/JEE. There are just TOO many of ways doing things. I suppose this is a blessing and a curse, but I see it more as a curse when developers today "just want to get stuff done." I think this is why you see the allure of things like Rails and even Grails for that matter. It shouldn't take one pouring over wiki entries to figure out the best way to test things.

I'm also thinking of using OpenEJB/Tomcat instead of JBoss5. I'm curious if others have had experience with this combo and their thoughts? (Easy to get setup and running?)
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Brick,

At the risk of getting the JBoss zealots up in arms, I share your frustration with embedded JBoss, in terms of poor documentation, bugs and performance. JBoss seems to really be resting on its laurels far too much these days.

My opinion is that OpenEJB or even GlassFish is a lot better (GlassFish now has an embedded version too). With regards to Embedded JBoss, feel free to contact Rob directly for help. The publicly available material he has *is* dated. As to best practices, as I said, we'll try to cover than in the EJB 3 in Action second edition. If I really see a lot of demand for it, I and Rob can consider writing an early article if we can make the time somehow...

Cheers,
Reza
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB3 Testing - Comments on this implementation and suggestions?
 
Similar Threads
Connecting to MySQL 5 in JBoss 5 AS
JSF Browser Refresh
Passed SCBCD5 Upgrade 88%
resources for preparing section 1 and 5 in IBM XML Test 141
JBoss' suitability for automated tests