Stateful

Run
How to run the sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing we need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/ejb/stateful/
Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:
mvn test -Dtest=CartBeanWithInterfaceTestmvn test -Dtest=CartBeanStatefulnessTestmvn test -Dtest=ReentrantCallTestmvn test -Dtest=CartBeanTest

CartBeanWithInterfaceTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static Archive<?> deployment() {
	return ShrinkWrap.create(JavaArchive.class)
			.addClass(Cart.class)
			.addClass(CartBeanWithInterface.class)
			.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

Test of addItem method, of class CartBean

@Test
public void shouldAddOneItem() throws Exception {
	// given

	// when
	sut.addItem("apple");

	// then
	assertThat(sut.getItems(), hasItem("apple"));
}

Test of addItem method, of class CartBean

@Test
public void shouldAddManyItems() throws Exception {
	// given
	final List<String> items = Arrays.asList("apple", "banana", "mango", "kiwi", "passion fruit");

	// when
	for (final String item : items) {
		sut.addItem(item);
	}

	// then
	assertThat(sut.getItems(), is(items));
}

Test of removeItem method, of class CartBean

@Test
public void shouldRemoveOneItem() throws Exception {
	// given
	final List<String> items = Arrays.asList("apple", "banana", "mango", "kiwi", "passion fruit");
	for (final String item : items) {
		sut.addItem(item);
	}

	// when
	sut.removeItem("banana");

	// then
	assertThat(sut.getItems(), not(hasItem("banana")));
}

Test of getItems method, of class CartBean

@Test
public void shouldBeEmpty() throws Exception {
	// given

	// when
	final List<String> actual = sut.getItems();

	// then
	assertThat(actual.isEmpty(), is(true));
}

CartBeanStatefulnessTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static Archive<?> deployment() {
	return ShrinkWrap.create(JavaArchive.class, "test.jar")
			.addClass(CartBean.class)
			.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

JSR 318: Enterprise JavaBeans, Version 3.1 3.4.7.1 Session Object Identity / Stateful Session Beans

A stateful session object has a unique identity that is assigned by the container at the time the object is created. A client of the stateful session bean business interface can determine if two business interface or no-interface view references refer to the same session object by use of the equals method

@Test
@InSequence(1)
public void should_not_be_identical_beans() {
    assertThat("Expect different instances", bean1, is(not(bean2)));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
   @InSequence(2)
public void should_add_items_to_first_cart() {
	// when
	bean1.addItem(item_to_add);

	// then
	assertThat(bean1.getItems(), hasItem(item_to_add));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
   @InSequence(3)
public void should_not_contain_any_items_in_second_cart() {
	assertThat(bean2.getItems().isEmpty(), is(true));
}

ReentrantCallTest

This tests that a stateful bean is capable of calling a method via a business proxy on itself.

Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static Archive<?> deployment() {
    return ShrinkWrap.create(WebArchive.class)
                     .addClass(ReentrantStatefulBean.class);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void doReentrantCall() {
    // initialMethod() will internally call another method on itself.
    // This should not throw an exception. See e.g. https://issues.apache.org/jira/browse/OPENEJB-1099
    reentrantStatefulBean.initialMethod();
}

CartBeanTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static Archive<?> deployment() {
	return ShrinkWrap.create(JavaArchive.class)
			.addClass(CartBean.class)
			.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

Test of addItem method, of class CartBean

@Test
public void shouldAddOneItem() throws Exception {
	// given

	// when
	sut.addItem("apple");

	// then
	assertThat(sut.getItems(), hasItem("apple"));
}

Test of addItem method, of class CartBean

@Test
public void shouldAddManyItems() throws Exception {
	// given
	final List<String> items = Arrays.asList("apple", "banana", "mango", "kiwi", "passion fruit");

	// when
	for (final String item : items) {
		sut.addItem(item);
	}

	// then
	assertThat(sut.getItems(), is(items));
}

Test of removeItem method, of class CartBean

@Test
public void shouldRemoveOneItem() throws Exception {
	// given
	final List<String> items = Arrays.asList("apple", "banana", "mango", "kiwi", "passion fruit");
	for (final String item : items) {
		sut.addItem(item);
	}

	// when
	sut.removeItem("banana");

	// then
	assertThat(sut.getItems(), not(hasItem("banana")));
}

Test of getItems method, of class CartBean

@Test
public void shouldBeEmpty() throws Exception {
	// given

	// when
	final List<String> actual = sut.getItems();

	// then
	assertThat(actual.isEmpty(), is(true));
}

Share the Knowledge

Find this sample useful? Share on

There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.

Help Improve

Find a bug in the sample? Something missing? You can fix it by editing the source, making the correction and sending a pull request. Or report the problem to the issue tracker

Recent Changelog

  • Oct 06, 2014: Fixed formatting by arjan tijms
  • Oct 06, 2014: Test that stateful session bean is capable of calling a method on itself by arjan tijms
  • Jul 15, 2014: Removed header license. the licensing is now referenced in the license file in the root of the project by Roberto Cortez
  • Dec 09, 2013: Favouring injection over initialcontext for #75 ejb stateless and statefull tests by kubamarchwicki
  • Dec 08, 2013: Test new stateful bean on every ctx lookup by kubamarchwicki
  • Dec 08, 2013: Removing obsolete servlets and index.jsp files by kubamarchwicki
  • Dec 02, 2013: Tests for ejb/stateless & ejb/stateful by Rafał Roppel
  • Sep 17, 2013: Removing netbeans configuration file by Arun Gupta
  • Sep 05, 2013: Removing redundant files by Arun Gupta
  • Aug 29, 2013: Adding copyright by Arun Gupta
How to help improve this sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing you need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/ejb/stateful/

Do the changes as you see fit and send a pull request!

Good Luck!