@Deployment
public static Archive<?> deployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(Cart.class)
.addClass(CartBeanWithInterface.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
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
@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));
}
@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)));
}
@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));
}
@Test
@InSequence(3)
public void should_not_contain_any_items_in_second_cart() {
assertThat(bean2.getItems().isEmpty(), is(true));
}
This tests that a stateful bean is capable of calling a method via a business proxy on itself.
@Deployment
public static Archive<?> deployment() {
return ShrinkWrap.create(WebArchive.class)
.addClass(ReentrantStatefulBean.class);
}
@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();
}
@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));
}
There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.
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!