Introduction to Testing with Arquillian (#1877)

This commit is contained in:
Jesus Boadas 2017-05-19 02:46:47 -04:00 committed by maibin
parent a6168a139b
commit e338d3eb0e
9 changed files with 215 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package com.baeldung.arquillian;
import javax.ejb.Stateless;
@Stateless
public class CapsConvertor {
public ConvertToLowerCase getLowerCase(){
return new ConvertToLowerCase();
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.arquillian;
import javax.ejb.Stateless;
import javax.inject.Inject;
@Stateless
public class CapsService {
@Inject
private CapsConvertor capsConvertor;
public String getConvertedCaps(final String word){
return capsConvertor.getLowerCase().convert(word);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.arquillian;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class Car {
@Id
@GeneratedValue
private Long id;
@NotNull
private String name;
public Long getId() {
return id;
}
public void setId(final Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@Override
public String toString() {
return "Car [id=" + id + ", name=" + name + "]";
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.arquillian;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class CarEJB {
@PersistenceContext(unitName = "defaultPersistenceUnit")
private EntityManager em;
public Car saveCar(final Car car) {
em.persist(car);
return car;
}
@SuppressWarnings("unchecked")
public List<Car> findAllCars() {
final Query query = em.createQuery("SELECT b FROM Car b ORDER BY b.name ASC");
List<Car> entries = query.getResultList();
if (entries == null) {
entries = new ArrayList<Car>();
}
return entries;
}
public void deleteCar(Car car) {
car = em.merge(car);
em.remove(car);
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.arquillian;
import java.io.PrintStream;
public class Component {
public void sendMessage(PrintStream to, String msg) {
to.println(message(msg));
}
public String message(String msg) {
return "Message, " + msg;
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.arquillian;
public class ConvertToLowerCase {
public String convert(String word){
return word.toLowerCase();
}
}

View File

@ -0,0 +1,13 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="defaultPersistenceUnit" transaction-type="JTA">
<class>com.baeldung.arquillian.Car</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,73 @@
package com.baeldung.arquillan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.baeldung.arquillian.CapsConvertor;
import com.baeldung.arquillian.CapsService;
import com.baeldung.arquillian.Car;
import com.baeldung.arquillian.CarEJB;
import com.baeldung.arquillian.Component;
import com.baeldung.arquillian.ConvertToLowerCase;
@RunWith(Arquillian.class)
public class ArquillianTest {
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class).addClasses(Component.class, CapsService.class, CapsConvertor.class, ConvertToLowerCase.class, Car.class, CarEJB.class).addAsResource("META-INF/persistence.xml").addAsManifestResource(EmptyAsset.INSTANCE,
"beans.xml");
}
@Inject
Component component;
@Test
public void givenMessage_WhenComponentSendMessage_ThenMessageReceived() {
Assert.assertEquals("Message, MESSAGE", component.message(("MESSAGE")));
component.sendMessage(System.out, "MESSAGE");
}
@Inject
private CapsService capsService;
@Test
public void givenWord_WhenUppercase_ThenLowercase() {
assertTrue("capitalize".equals(capsService.getConvertedCaps("CAPITALIZE")));
assertEquals("capitalize", capsService.getConvertedCaps("CAPITALIZE"));
}
@Inject
private CarEJB carEJB;
@Test
public void testCars() {
assertTrue(carEJB.findAllCars().isEmpty());
Car c1 = new Car();
c1.setName("Impala");
Car c2 = new Car();
c2.setName("Maverick");
Car c3 = new Car();
c3.setName("Aspen");
Car c4 = new Car();
c4.setName("Lincoln");
carEJB.saveCar(c1);
carEJB.saveCar(c2);
carEJB.saveCar(c3);
carEJB.saveCar(c4);
assertEquals(4, carEJB.findAllCars().size());
carEJB.deleteCar(c4);
assertEquals(3, carEJB.findAllCars().size());
}
}

View File

@ -0,0 +1,13 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="defaultPersistenceUnit" transaction-type="JTA">
<class>com.baeldung.arquillian.Car</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>