Thursday 15 August 2013

Top Most Unit Testing EJB using EJB3UNIT

Unit Testing EJB is something that not everyone needs. EJB is so much container managed, and has been around for a long time. But no one can skip the Unit Testing.

Let me discuss a scenario(/ an architecture), where unit testing is nightmare.

In my recent project, its SOA based architecture, and each EJB going into its own .ear file. Right now, around 150 .ear files. On a new requirement, there are 10-12 new EJB's and all having huge amount of changes, and lot of cuntionality. Complication is they are very much interdependent, and all are going to be exposed as Web Services, and there is an Integration layer over these EJB's.

Now, Imagine you are working on a new component( an EJB session bean), which is dependent on other 8-9 EJB's, which are in different EAR's, of which most are not developed. Some are EJB 2.1, and some are EJB 3.0, many new tables( and entities). And similar case is with all other developers.

With EJB3Unit, you can design a framework for such circumstances.

We all know that Unit test cases are written befor the actual development starts.

So, if there are 8 developers woking on 8 new components, and all are interdependent. They can start using the EJB3Unit framework.

Each developer starts with wriring the EJB interface, and a implementation class with hard coded response(which makes sense), and write a Unit test class for the EJB, in a common project.
And anyone developing its own component first can test its own components.
Because there are mocks for all component, which are not developed yet.
Best thing is we dont need Application Server. And we dont need to alter the common build prematurely.
Apart from that what we can do using the EJB3Unit:
  • Dependency Injection(@Resource, @PersistenceContext, etc.)
  • Entities and Queries over entities( EJB3UNIT comes with In memory database).  
  • Component Interdependancy( EJB injection)
  •  Automated JUNIT test cases.

Starting with EJB3UNIT is very simple. As with most of the open source project, EJB3UNIT also comes with a sample project which can be download and imported into IDE.
For new ones to Java/J2EE, it might be difficult to understand the sample project.
There can be another way, I am going to explain how to start working with EJB3UNIT, without using Sample project. 
I am using Eclipse 3.6.1, and I also tested on RAD 7.5. 

I am going to create a Sample project named: EJB3UnitUsage.
I have create few source folders:
 src/main/java - to hold my java classes, i.e. session and entity beans.
src/test/java    - to hold the Unit Test Classes
src/test/resources - to hold the properties file.
If you are wondering, what is the contet of properties file.
Actually I just copied them from the downloaded Sample project from the EJB3UNIT site.
And we only need two properties file right now:
ejb3unit.properties, and log4j.properties
It would be better if you follow the same or similar structure.
Below is the screen shot of the package structure and the classes:


 MyEntity is my Entity bean
MyInterface is my Session bean Interface.
(Local Interface, It wont make any difference if you make it Remote also).
MySessionBean is my Session Bean.
Below is the coe for MySessionBean:

I have injected the EntityManager and I can use it also.
We would test if at runtime it is null or not, and also what the operation "getName()" returns at runtime.
Below is my Test class:



Advanced:
 There would be cases, when there is already a project with entities.
We can import those entities, and persistence.xml, in our framework.
For eg, I created a sample project with three entities, and persistence.xml.

I exported the java classes as jar file, and imported the jar file into my project, i.e. "EJB3UnitUsage".
I added the persistence.xml to the "EJB3UnitUsage" project.
Now, I am going to perform some operations over the imported entities.
Inside my session bean, I write code to persist the Book entity, and then find it later.
Our framework is using In mememory database, and so entity would be persisted there.
Also, we need to add following properties in the "ejb3unit.properties":
#Configuration for Persistence classes

ejb3unit.loadPersistenceXML=true
ejb3unit.persistenceUnit.name=mgr
Remember, "mgr" is the name of persistence-unit defiend in the persistence.xml.
Now, when we would run our test case, the persistence.xml, would be read, and also the persistence-unit, and entities would be loaded in memory.
Note the path of persistence.xml, it should in class path. I placed it inside the META-INF.
I have added following code in my Session Bean:
And inside my the test class I add code for testing the entity operation:
I have also tested Entities with @OneToMany, and @ManyToOne relations.
They also work.
I dont know why ArrayList doesn't work in the case of @OneToMany. List works, and Collection works.
For eg., below is the raltion between two entities:

And my code of Lesson.java looks like:

And in my Session EJB I added code to perform some operations over these related entities:

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Labels

Core Java programming core java interview question Core Java Faq's Servlets coding database jsp-servlet spring Java linux unix interview questions java investment bank Web Services Interview investment bank mysql Senior java developer interviews best practices java collection tutorial RMI SQL Eclipse FIX protocol tutorial tibco J2EE groovy java questions SCJP grails java 5 tutorial jdbc beginner error and exception Design Patterns Java Programming Tutorials fundamentals general object oriented programming xml Java Programs Hibernate Examples Flex JAMon Java xml tutorial logging Jsp Struts 2.0 Sybase and SQL Server debugging java interviews performance FIX Protocol interview questions JUnit testing WebSphere date and time tutorial experienced java IO tutorial java concurrency thread Ejb Freshers Papers IT Management Java Exapmle Java Script SQL and database tutorial examples Scwcd ant tutorials concurrency example and tutorial future state homework java changes java threading tricky Agile Business of IT Development JSTL Java JSON tutorial Java multithreading Tutorials PM Scrum data structure and algorithm java puzzles java tips testing tips windows 8 5 way to create Singleton Object Architect Interview Questions and Answers Architecture Architecure Bluetooth server as swing application that searches bluetooth device in 10 meter circle and show all devices. You can send file to any bluetooth device. C Programming CIO Callable Statement in Java Circular dependency of Objects in Java Comparable Example in Collection Custom annotation in Java Developer Interview Divide and rule example in java Drupal Example of Singleton Pattern FIX protocol ForkJoin Example in Java 7 Get data from dynamic table with Java Script Git HTML and JavaScript Health Hello World TCP Client Server Networking Program Hibernate Basics Hibernate Interview Question Answer J2EE Interview Question And Answers J2ME GUI Program JEE Interview QA JMS interview question Java J2EE Hibernate Spring Struts Interview Question Java System Property Java Threads Manager Portlets Provident Fund Read data from any file in same location and give the required result. Reading Properties File in Java Redpoint Rest WebService Client Rest Webservice Test SAL join with ven diagram SCP UNIX COMMAND SSL Singleton Pattern in Java Spring Bean Initialization methods and their order Spring Interview Questions Struts Struts 2.0 Basics Struts 2.0 Design Pattern Submit Html Form With Java Script On The Fly Unix executable For Java Program XOM DOM SAX XP books computers core java; core java; object oriented programming data structure; java investment bank; design pattern dtd duplicate rows in table get browser name with jquery grails podcast inner class java beginners tutorial java cache java networking tutorial java spring java util; java collections; java questions java.java1.5 linked list mailto function with all browser oracle database oracle duplicate rows orm schema social spring mvc questions struts transaction tricks tweet windows xslt