) response.getEntity(), containsPeopleInAnyOrder(people()));
}
@Test
public void testListDAOException() throws Exception {
expect(daoMock.list()).andThrow(new DAOException());
+
replay(daoMock);
final Response response = resource.list();
- assertEquals(Status.INTERNAL_SERVER_ERROR, response.getStatusInfo());
+
+ assertThat(response, hasInternalServerErrorStatus());
}
@Test
public void testGet() throws Exception {
- final Person person = new Person(1, "Pepe", "Pérez");
+ final Person person = existentPerson();
expect(daoMock.get(person.getId())).andReturn(person);
+
replay(daoMock);
final Response response = resource.get(person.getId());
- assertEquals(person, response.getEntity());
- assertEquals(Status.OK, response.getStatusInfo());
+
+ assertThat(response, hasOkStatus());
+ assertThat((Person) response.getEntity(), is(equalsToPerson(person)));
}
@Test
public void testGetDAOException() throws Exception {
expect(daoMock.get(anyInt())).andThrow(new DAOException());
+
replay(daoMock);
- final Response response = resource.get(1);
- assertEquals(Status.INTERNAL_SERVER_ERROR, response.getStatusInfo());
+ final Response response = resource.get(existentId());
+
+ assertThat(response, hasInternalServerErrorStatus());
}
@Test
public void testGetIllegalArgumentException() throws Exception {
expect(daoMock.get(anyInt())).andThrow(new IllegalArgumentException());
+
replay(daoMock);
- final Response response = resource.get(1);
- assertEquals(Status.BAD_REQUEST, response.getStatusInfo());
+ final Response response = resource.get(existentId());
+
+ assertThat(response, hasBadRequestStatus());
}
@Test
public void testDelete() throws Exception {
daoMock.delete(anyInt());
+
replay(daoMock);
final Response response = resource.delete(1);
- assertEquals(Status.OK, response.getStatusInfo());
+
+ assertThat(response, hasOkStatus());
}
@Test
public void testDeleteDAOException() throws Exception {
daoMock.delete(anyInt());
expectLastCall().andThrow(new DAOException());
+
replay(daoMock);
final Response response = resource.delete(1);
- assertEquals(Status.INTERNAL_SERVER_ERROR, response.getStatusInfo());
+
+ assertThat(response, hasInternalServerErrorStatus());
}
@Test
@@ -125,12 +148,15 @@ public class PeopleResourceUnitTest {
replay(daoMock);
final Response response = resource.delete(1);
- assertEquals(Status.BAD_REQUEST, response.getStatusInfo());
+
+ assertThat(response, hasBadRequestStatus());
}
@Test
public void testModify() throws Exception {
- final Person person = new Person(1, "Pepe", "Pérez");
+ final Person person = existentPerson();
+ person.setName(newName());
+ person.setSurname(newSurname());
daoMock.modify(person);
@@ -139,8 +165,8 @@ public class PeopleResourceUnitTest {
final Response response = resource.modify(
person.getId(), person.getName(), person.getSurname());
+ assertThat(response, hasOkStatus());
assertEquals(person, response.getEntity());
- assertEquals(Status.OK, response.getStatusInfo());
}
@Test
@@ -150,8 +176,9 @@ public class PeopleResourceUnitTest {
replay(daoMock);
- final Response response = resource.modify(1, "Paco", "Pérez");
- assertEquals(Status.INTERNAL_SERVER_ERROR, response.getStatusInfo());
+ final Response response = resource.modify(existentId(), newName(), newSurname());
+
+ assertThat(response, hasInternalServerErrorStatus());
}
@Test
@@ -160,24 +187,23 @@ public class PeopleResourceUnitTest {
expectLastCall().andThrow(new IllegalArgumentException());
replay(daoMock);
+
+ final Response response = resource.modify(existentId(), newName(), newSurname());
- final Response response = resource.modify(1, "Paco", "Pérez");
- assertEquals(Status.BAD_REQUEST, response.getStatusInfo());
+ assertThat(response, hasBadRequestStatus());
}
@Test
public void testAdd() throws Exception {
- final Person person = new Person(1, "Pepe", "Pérez");
-
- expect(daoMock.add(person.getName(), person.getSurname()))
- .andReturn(person);
+ expect(daoMock.add(newName(), newSurname()))
+ .andReturn(newPerson());
replay(daoMock);
- final Response response = resource.add(
- person.getName(), person.getSurname());
- assertEquals(person, response.getEntity());
- assertEquals(Status.OK, response.getStatusInfo());
+ final Response response = resource.add(newName(), newSurname());
+
+ assertThat(response, hasOkStatus());
+ assertThat((Person) response.getEntity(), is(equalsToPerson(newPerson())));
}
@Test
@@ -186,8 +212,9 @@ public class PeopleResourceUnitTest {
.andThrow(new DAOException());
replay(daoMock);
- final Response response = resource.add("Paco", "Pérez");
- assertEquals(Status.INTERNAL_SERVER_ERROR, response.getStatusInfo());
+ final Response response = resource.add(newName(), newSurname());
+
+ assertThat(response, hasInternalServerErrorStatus());
}
@Test
@@ -196,7 +223,8 @@ public class PeopleResourceUnitTest {
.andThrow(new IllegalArgumentException());
replay(daoMock);
- final Response response = resource.add("Paco", "Pérez");
- assertEquals(Status.BAD_REQUEST, response.getStatusInfo());
+ final Response response = resource.add(newName(), newSurname());
+
+ assertThat(response, hasBadRequestStatus());
}
}
diff --git a/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java b/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java
index fffda9e014e1500799fbe6b71dffca61a75e5c5e..a21ca3663b624f6cf2ea326a347de55ae8f96ca9 100644
--- a/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java
+++ b/src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java
@@ -5,9 +5,11 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import es.uvigo.esei.daa.dao.PeopleDAOUnitTest;
+import es.uvigo.esei.daa.entities.PersonUnitTest;
import es.uvigo.esei.daa.rest.PeopleResourceUnitTest;
-@SuiteClasses({
+@SuiteClasses({
+ PersonUnitTest.class,
PeopleDAOUnitTest.class,
PeopleResourceUnitTest.class
})
diff --git a/src/test/java/es/uvigo/esei/daa/TestUtils.java b/src/test/java/es/uvigo/esei/daa/util/ContextUtils.java
similarity index 52%
rename from src/test/java/es/uvigo/esei/daa/TestUtils.java
rename to src/test/java/es/uvigo/esei/daa/util/ContextUtils.java
index 50b51fcfe669458fc80bbb912afaa4d418c2ef85..331044f0f1f201bf6d93b2805d08ac4c85031b4c 100644
--- a/src/test/java/es/uvigo/esei/daa/TestUtils.java
+++ b/src/test/java/es/uvigo/esei/daa/util/ContextUtils.java
@@ -1,18 +1,15 @@
-package es.uvigo.esei.daa;
-
-import static org.junit.Assert.assertEquals;
+package es.uvigo.esei.daa.util;
import javax.naming.NamingException;
import javax.sql.DataSource;
-import javax.ws.rs.core.Response;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
-public final class TestUtils {
+public final class ContextUtils {
private final static SimpleNamingContextBuilder CONTEXT_BUILDER =
new SimpleNamingContextBuilder();
- private TestUtils() {}
+ private ContextUtils() {}
public static void createFakeContext(DataSource datasource)
throws IllegalStateException, NamingException {
@@ -24,12 +21,4 @@ public final class TestUtils {
CONTEXT_BUILDER.clear();
CONTEXT_BUILDER.deactivate();
}
-
- public static void assertOkStatus(final Response response) {
- assertEquals("Unexpected status code", Response.Status.OK.getStatusCode(), response.getStatus());
- }
-
- public static void assertBadRequestStatus(final Response response) {
- assertEquals("Unexpected status code", Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
- }
}
diff --git a/src/test/java/es/uvigo/esei/daa/DatabaseQueryUnitTest.java b/src/test/java/es/uvigo/esei/daa/util/DatabaseQueryUnitTest.java
similarity index 59%
rename from src/test/java/es/uvigo/esei/daa/DatabaseQueryUnitTest.java
rename to src/test/java/es/uvigo/esei/daa/util/DatabaseQueryUnitTest.java
index a78f0bdbe837b932692875ecd894af3b29169302..e541cb7ce2edb3b91ed6c40164b296ef10a3f34c 100644
--- a/src/test/java/es/uvigo/esei/daa/DatabaseQueryUnitTest.java
+++ b/src/test/java/es/uvigo/esei/daa/util/DatabaseQueryUnitTest.java
@@ -1,4 +1,4 @@
-package es.uvigo.esei.daa;
+package es.uvigo.esei.daa.util;
import static org.easymock.EasyMock.anyString;
import static org.easymock.EasyMock.createMock;
@@ -18,6 +18,15 @@ import org.junit.Before;
import com.mysql.jdbc.PreparedStatement;
+/**
+ * Super-class for unit tests in the DAO layer.
+ *
+ * The default {@link DatabaseQueryUnitTest#setUp()} method in this class
+ * create mocks for the datasource, connection, statement, and result variables
+ * that can be used by the DAO object under test.
+ *
+ * @author Miguel Reboiro Jato
+ */
public abstract class DatabaseQueryUnitTest {
protected DataSource datasource;
protected Connection connection;
@@ -26,6 +35,11 @@ public abstract class DatabaseQueryUnitTest {
protected boolean verify;
+ /**
+ * Configures the mocks and enables the verification.
+ *
+ * @throws Exception if an error happens while configuring the mocks.
+ */
@Before
public void setUp() throws Exception {
datasource = createMock(DataSource.class);
@@ -36,31 +50,46 @@ public abstract class DatabaseQueryUnitTest {
expect(datasource.getConnection())
.andReturn(connection);
expect(connection.prepareStatement(anyString()))
- .andReturn(statement);
+ .andReturn(statement)
+ .anyTimes(); // statement is optional
expect(statement.executeQuery())
.andReturn(result)
- .anyTimes(); // executeQuery is optional;
+ .anyTimes(); // executeQuery is optional
statement.close();
connection.close();
verify = true;
}
+ /**
+ * Removes the default behavior of the mock instances and disables the mock
+ * verification.
+ */
protected void resetAll() {
reset(result, statement, connection, datasource);
verify = false;
}
-
+
+ /**
+ * Replays the configured behavior of the mock instances and enables the
+ * mock verification. The mocked datasource is also added to a new context.
+ */
protected void replayAll()
throws Exception {
replay(result, statement, connection, datasource);
+ verify = true;
- TestUtils.createFakeContext(datasource);
+ ContextUtils.createFakeContext(datasource);
}
+ /**
+ * Clears the context and verifies the mocks if the verification is enabled.
+ *
+ * @throws Exception if an error happens during verification.
+ */
@After
public void tearDown() throws Exception {
- TestUtils.clearContextBuilder();
+ ContextUtils.clearContextBuilder();
try {
if (verify) {
diff --git a/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java b/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java
index fbd1e5e718e29458eeb69c5c3bc0875f59c6ff89..4fc50b65dd2dbca6986c90f7f048c4cfc16d8623 100644
--- a/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java
+++ b/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java
@@ -1,7 +1,16 @@
package es.uvigo.esei.daa.web;
-import static org.junit.Assert.assertEquals;
+import static es.uvigo.esei.daa.dataset.PeopleDataset.existentId;
+import static es.uvigo.esei.daa.dataset.PeopleDataset.existentPerson;
+import static es.uvigo.esei.daa.dataset.PeopleDataset.newName;
+import static es.uvigo.esei.daa.dataset.PeopleDataset.newPerson;
+import static es.uvigo.esei.daa.dataset.PeopleDataset.newSurname;
+import static es.uvigo.esei.daa.dataset.PeopleDataset.people;
+import static es.uvigo.esei.daa.matchers.IsEqualToPerson.containsPeopleInAnyOrder;
+import static es.uvigo.esei.daa.matchers.IsEqualToPerson.equalsToPerson;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
import java.util.concurrent.TimeUnit;
@@ -78,42 +87,36 @@ public class PeopleWebTest {
@Test
public void testList() throws Exception {
- assertEquals(10, mainPage.countPeople());
+ assertThat(mainPage.listPeople(), containsPeopleInAnyOrder(people()));
}
@Test
@ExpectedDatabase("/datasets/dataset-add.xml")
public void testAdd() throws Exception {
- final String name = "John";
- final String surname = "Doe";
+ final Person newPerson = mainPage.addPerson(newName(), newSurname());
- final Person newPerson = mainPage.addPerson(name, surname);
-
- assertEquals(name, newPerson.getName());
- assertEquals(surname, newPerson.getSurname());
+ assertThat(newPerson, is(equalsToPerson(newPerson())));
}
@Test
@ExpectedDatabase("/datasets/dataset-modify.xml")
public void testEdit() throws Exception {
- final int id = 5;
- final String newName = "John";
- final String newSurname = "Doe";
+ final Person person = existentPerson();
+ person.setName(newName());
+ person.setSurname(newSurname());
- mainPage.editPerson(id, "John", "Doe");
+ mainPage.editPerson(person);
- final Person person = mainPage.getPerson(id);
+ final Person webPerson = mainPage.getPerson(person.getId());
- assertEquals(id, person.getId());
- assertEquals(newName, person.getName());
- assertEquals(newSurname, person.getSurname());
+ assertThat(webPerson, is(equalsToPerson(person)));
}
@Test
@ExpectedDatabase("/datasets/dataset-delete.xml")
public void testDelete() throws Exception {
- mainPage.deletePerson(4);
+ mainPage.deletePerson(existentId());
- assertFalse(mainPage.hasPerson(4));
+ assertFalse(mainPage.hasPerson(existentId()));
}
}
diff --git a/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java b/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java
index cd8428e80a7209ce17b3fa4a3ab578ad9004ec80..9aa4d32db07aac8721f4f6a9abb7e21f6ce0dd0a 100644
--- a/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java
+++ b/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java
@@ -1,5 +1,6 @@
package es.uvigo.esei.daa.web.pages;
+import static java.util.stream.Collectors.toList;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElement;
@@ -42,6 +43,10 @@ public class MainPage {
return new PeopleTable(this.driver).countPeople();
}
+ public List listPeople() {
+ return new PeopleTable(this.driver).listPeople();
+ }
+
public Person getLastPerson() {
return new PeopleTable(this.driver).getPersonInLastRow();
}
@@ -66,13 +71,13 @@ public class MainPage {
return table.getPerson(name, surname);
}
- public void editPerson(int id, String newName, String newSurname) {
+ public void editPerson(Person person) {
final PeopleTable table = new PeopleTable(this.driver);
- table.editPerson(id);
+ table.editPerson(person.getId());
final PersonForm form = new PersonForm(this.driver);
- form.setName(newName);
- form.setSurname(newSurname);
+ form.setName(person.getName());
+ form.setSurname(person.getSurname());
form.submit();
}
@@ -83,7 +88,6 @@ public class MainPage {
}
private final static class PeopleTable {
-
private final WebDriver driver;
private final WebElement table;
@@ -146,12 +150,19 @@ public class MainPage {
}
public int countPeople() {
+ return getRows().size();
+ }
+
+ public List listPeople() {
+ return getRows().stream()
+ .map(this::rowToPerson)
+ .collect(toList());
+ }
+
+ private List getRows() {
final String xpathQuery = "//tr[starts-with(@id, '" + ID_PREFIX + "')]";
- final List peopleRows =
- this.table.findElements(By.xpath(xpathQuery));
-
- return peopleRows.size();
+ return this.table.findElements(By.xpath(xpathQuery));
}
private Person rowToPerson(WebElement row) {
diff --git a/src/test/resources/contexts/mem-context.xml b/src/test/resources/contexts/mem-context.xml
index dc250b9a0db01f97bfbad33d9c824f94e880e307..a37be3358a4bdc5d6e9e630b12210a2da842f9d2 100644
--- a/src/test/resources/contexts/mem-context.xml
+++ b/src/test/resources/contexts/mem-context.xml
@@ -4,7 +4,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
-
+
diff --git a/src/test/resources/datasets/dataset-delete.xml b/src/test/resources/datasets/dataset-delete.xml
index 40470fabca572647ad7ea2750adc54acd580490d..51b0b21175dba6d713466042cf4c73ebc3f9d556 100644
--- a/src/test/resources/datasets/dataset-delete.xml
+++ b/src/test/resources/datasets/dataset-delete.xml
@@ -5,7 +5,7 @@
-
+