How to unit test EJB3 without a container

Unit test EJB3 without a container has become much easier since the EJB 3.1 spec with the introduction of the embeddable EJBContainer concept. An embeddable EJBContainer is a container for enterprise beans that does not require a Java EE server to run.

Interesting usage scenarios are:

  • EJB unit testing: you don’t need to install a JavaEE server for EJB development, unit testing and deployment to the container
  • Lightweight: the embeddable container has a much smaller footprint
  • Fast: starts faster than the full server, because it only initializes EJB-related components

Sample code

Below you can find a simple setup method for a JUnit test that configures the Embeddable EJBContainer.

@Before
public void setup() {
	Properties properties = new Properties();
	properties.setProperty(EJBContainer.MODULES, "myModule");
	properties.put(EJBContainer.PROVIDER, "tomee-embedded");
	Context context = EJBContainer.createEJBContainer().getContext();
}

@After
public void tearDown() throws NamingException {
	ejbContainer.close();
}

Here myModule is the module-name defined in ejb-jar.xml file

So how does the EJBContainer start an embedded EJB container? You’ll need to provide an embedded EJB container on the classpath. Normally all Application servers supporting JavaEE6 have to provide such an embedded EJB container.

Apache TomEE container This example uses Apache TomEE as it provides an easy way to specify the correct dependencies via Maven.

<dependency>
	<groupId>org.apache.openejb</groupId>
	<artifactId>tomee-embedded</artifactId>
	<version>1.5.2</version>
</dependency>            
<dependency>
	<groupId>javax</groupId>
	<artifactId>javaee-api</artifactId>
	<version>6.0</version>
	<scope>provided</scope>
</dependency>

The properties.put(EJBContainer.PROVIDER, “tomee-embedded”) makes sure that we will use Apache TomEE when running the test. (even if there is another provider on the classpath)

0 thoughts on “How to unit test EJB3 without a container”

  1. Philosophy is the discipline that studies fundamental and universal questions, including existence, cognition, values, reason, and language. It encompasses many topics and problems, from ethics and politicians to metaphysics and logic. Here are the main nuances of philosophy:

    1. Definition of Philosophy
    Philosophy comes from the Greek words “philos” (love) and “sophia” (wisdom). It is the desire to realize and make sense of the world around us and our place in it.

    2. The Main branches of philosophy
    – Ontology research of the essence of being and existence.
    – Epistemology the study of the nature and limits of cognition.
    – Ethics the analysis of ethical principles and concepts of good deed and evil.
    – Logic research of the forms and principles of correct thinking.
    – Political philosophy the study of questions of power, justice and the state.

    3. Notable Philosophers
    During the history of philosophy, many thinkers have made meaningful contributions to this science. Some of the most famous include:
    – Socrates, considered the founder of Western philosophy.
    – Plato, a pupil of Socrates, developed the doctrine of forms.
    – Aristotle, the creator of logic and a large number of scientific fields.
    – Immanuel Kant, known for his own James Joyce critical philosophy and work on moral issues.

    4. The Relevance of Philosophy in the Modern World
    Philosophy remains urgent in the modern world as enables people to understand complex social and moral issues, but also develops critical thinking. It influences the legal system, politicians, and different fields of science.

    5. Practical Applications of Philosophy

    Philosophical ideas penetrate everyday life. Ethical reflection helps people do the correct thing in difficult situations, and philosophical analysis contributes to better understanding of problems related to technology, art, and social justice.

    Philosophy is not only an academic discipline, yes and method of thinking assists us to understand life’s complexities and make informed choices.

  2. Philosophy is the discipline that studies major and universal questions, such as existence, cognition, values, reason, and language. It encompasses many topics and problems, from ethics and political figures to metaphysics and logic. Here are principal nuances of philosophy:

    1. Definition of Philosophy
    Philosophy comes from the Greek words “philos” (love) and “sophia” (wisdom). It is zeal to realize and make sense of the world around us and our place in it.

    2. The Main branches of philosophy
    – Ontology research of the essence of being and existence.
    – Epistemology the study of the nature and limits of cognition.
    – Ethics the analysis of ethical principles and concepts of good deed and evil.
    – Logic research of the forms and principles of correct thinking.
    – Socio-political philosophy the study of questions of power, justice and the state.

    3. Notable Philosophers
    Throughout the history of philosophy, many thinkers have made meaningful contributions to this science. Some of the most famous include:
    – Socrates, considered the founder of Western philosophy.
    – Plato, a student of Socrates, developed the doctrine of forms.
    – Aristotle, the creator of logic and a huge number of scientific fields.
    – Immanuel Kant, known for his James Joyce critical philosophy and work on moral issues.

    4. The Relevance of Philosophy in the Modern World
    Philosophy remains urgent in the modern world since it helps people to understand difficult social and moral issues, but also develops critical thinking. It influences the legal system, politicians, and different fields of science.

    5. Practical Applications of Philosophy

    Philosophical ideas enter daily life. Ethical reflection helps people do the correct thing in complex situations, and philosophical analysis contributes to best understanding of problems related to technology, art, and social justice.

    Philosophy is not only an academic discipline, as well as a way of thinking that helps us to understand life’s complexities and make informed choices.

  3. Philosophy is the discipline that studies major and universal questions, including existence, knowledge, values, mind, and language. It encompasses many topics and issues, from ethics and politicians to metaphysics and logic. Here are the main aspects of philosophy:

    1. Definition of Philosophy
    Philosophy comes from the Greek words “philos” (love) and “sophia” (wisdom). It is the desire to realize and make sense of the world around us and our place in it.

    2. The Main branches of philosophy
    – Ontology research of the essence of being and existence.
    – Epistemology the study of the nature and limits of knowledge.
    – Ethics the analysis of ethical principles and concepts of good and evil.
    – Logic the study of the forms and principles of correct thinking.
    – Political philosophy the study of questions of power, justice and the state.

    3. Famous Philosophers
    During the history of philosophy, almost many thinkers have made significant contributions to this science. Some of the most recognizable include:
    – Socrates, considered the progenitor of Western philosophy.
    – Plato, a student of Socrates, developed the doctrine of forms.
    – Aristotle, the creator of logic and a large number of scientific fields.
    – Immanuel Kant, known for his Marcel Proust critical philosophy and work on moral issues.

    4. The Relevance of Philosophy in the Modern World
    Philosophy remains urgent in the modern world because it helps people to understand difficult social and moral issues, and also develops critical thinking. It influences the legal system, politicians, and various fields of science.

    5. Practical Applications of Philosophy

    Philosophical ideas enter everyday life. Ethical reflection helps people do the correct thing in complex situations, and philosophical analysis contributes to most excellent understanding of issues related to technology, art, and social justice.

    Philosophy is not only an academic discipline, as well as method of thinking that helps us to understand life’s complexities and make informed choices.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.