Permalink
Browse files

work on unit testing

  • Loading branch information...
eugenp committed Mar 21, 2012
1 parent 927f880 commit c88321585e5cf791c5f9aa2a7d7c661bc23b9968
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/rest/src/test/java/org/rest/sec/web/privilege/PrivilegeSearchRESTIntegrationTest.java"/>
+<listEntry value="/rest/src/test/java/org/rest/test/suite/UnitTestSuite.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
@@ -12,10 +12,10 @@
</mapAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value="givenResourceExists_whenResourceIfSearchedByWrongIdAndWrongNameAndUnmarshalled_thenResourceIsFound"/>
+<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.web.privilege.PrivilegeSearchRESTIntegrationTest"/>
+<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.test.suite.UnitTestSuite"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="rest"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
</launchConfiguration>
@@ -37,7 +37,7 @@ public void whenEntitiesAreRetrieved_thenTheResultIsNotNull() {
@Test
public void givenAnEntityExists_whenEntitiesAreRetrieved_thenThereIsAtLeastOneEntity() {
- this.getAPI().create(this.createNewEntity());
+ getAPI().create(createNewEntity());
final List<T> owners = getAPI().findAll();
@@ -46,7 +46,7 @@ public void givenAnEntityExists_whenEntitiesAreRetrieved_thenThereIsAtLeastOneEn
@Test
public void givenAnEntityExists_whenEntitiesAreRetrieved_thenTheExistingEntityIsIndeedAmongThem() {
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
final List<T> owners = getAPI().findAll();
@@ -57,7 +57,7 @@ public void givenAnEntityExists_whenEntitiesAreRetrieved_thenTheExistingEntityIs
@Test
public void givenEntityExists_whenEntityIsRetrieved_thenNoExceptions() {
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
getAPI().findOne(existingEntity.getId());
}
@@ -68,7 +68,7 @@ public void givenEntityDoesNotExist_whenEntityIsRetrieved_thenNoExceptions() {
@Test
public void givenEntityExists_whenEntityIsRetrieved_thenTheResultIsNotNull() {
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
final T retrievedEntity = getAPI().findOne(existingEntity.getId());
assertNotNull(retrievedEntity);
}
@@ -81,7 +81,7 @@ public void givenEntityDoesNotExist_whenEntityIsRetrieved_thenTheResultIsNull()
@Test
public void givenEntityExists_whenEntityIsRetrieved_thenEntityIsRetrievedCorrectly() {
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
final T retrievedEntity = getAPI().findOne(existingEntity.getId());
assertEquals(existingEntity, retrievedEntity);
}
@@ -96,12 +96,12 @@ public void whenNullEntityIsCreated_thenException() {
@Test
@Rollback
public void whenEntityIsCreated_thenNoExceptions() {
- this.getAPI().create(this.createNewEntity());
+ getAPI().create(createNewEntity());
}
@Test
public void whenEntityIsCreated_thenEntityIsRetrievable() {
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
assertNotNull(getAPI().findOne(existingEntity.getId()));
}
@@ -142,22 +142,22 @@ public void whenNullEntityIsUpdated_thenException() {
@Test
public void whenEntityIsUpdated_thenNoExceptions() {
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
getAPI().update(existingEntity);
}
@Test(expected = DataAccessException.class)
public void whenEntityIsUpdatedWithFailedConstraints_thenException() {
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
invalidate(existingEntity);
getAPI().update(existingEntity);
}
@Test
public void whenEntityIsUpdated_thenTheUpdatedAreCorrectlyPersisted() {
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
changeEntity(existingEntity);
getAPI().update(existingEntity);
@@ -183,7 +183,7 @@ public void whenEntityIsDeletedByNegativeId_thenDataAccessException() {
@Test
public void givenEntityExists_whenEntityIsDeleted_thenNoExceptions() {
// Given
- final T existingLocation = this.getAPI().create(this.createNewEntity());
+ final T existingLocation = getAPI().create(createNewEntity());
// When
getAPI().delete(existingLocation.getId());
@@ -192,7 +192,7 @@ public void givenEntityExists_whenEntityIsDeleted_thenNoExceptions() {
@Test
public void givenEntityExists_whenEntityIsDeleted_thenEntityIsNoLongerRetrievable() {
// Given
- final T existingEntity = this.getAPI().create(this.createNewEntity());
+ final T existingEntity = getAPI().create(createNewEntity());
// When
getAPI().delete(existingEntity.getId());
@@ -1,6 +1,7 @@
package org.rest.persistence.service;
-import static org.mockito.Matchers.any;
+import static org.junit.Assert.assertSame;
+import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -9,21 +10,18 @@
import org.rest.common.IEntity;
import org.rest.util.IDUtils;
import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
-import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+/**
+ * A base class for service layer unit tests.
+ */
public abstract class AbstractServiceUnitTest<T extends IEntity> {
- private Class<T> clazz;
-
- public AbstractServiceUnitTest(final Class<T> clazzToSet) {
- super();
-
- Preconditions.checkNotNull(clazzToSet);
- this.clazz = clazzToSet;
- }
-
// fixtures
public void before() {
@@ -34,87 +32,100 @@ public void before() {
// tests
@Test
- public final void whenServiceIsInitialized_thenNoException() {
+ public void whenServiceIsInitialized_thenNoException() {
// When
// Then
}
// create
+ @Test(expected = NullPointerException.class)
+ public void whenCreateIsTriggeredForNullEntity_thenException() {
+ // When
+ getService().create(null);
+
+ // Then
+ }
+
@Test
- public final void whenCreateIsTriggered_thenNoException() {
+ public void whenCreateIsTriggered_thenNoException() {
// When
getService().create(this.createNewEntity());
// Then
}
- @Test(expected = NullPointerException.class)
- public final void whenCreateIsTriggeredForNullEntity_thenException() {
+ @Test
+ public void whenCreatingANewEntity_thenEntityIsSaved() {
+ // Given
+ final T entity = createNewEntity();
+ stubDaoSave(entity);
+
// When
- getService().create(null);
+ getService().create(entity);
// Then
+ verify(getDAO()).save(entity);
}
+ // update
+
@Test
- public final void whenCreateIsTriggered_thenEntityIsCreated() {
+ public void whenUpdateIsTriggered_thenNoException() {
// When
- getService().create(this.createNewEntity());
+ getService().update(this.createNewEntity());
// Then
- verify(getDAOMock()).save(any(clazz));
}
- // get
- /*
- * @Test public final void whenGetIsTriggered_thenNoException(){ this.configureGet( 1l );
- *
- * // When getService().findOne( 1l );
- *
- * // Then }
- */
@Test(expected = NullPointerException.class)
- public final void whenGetIsTriggeredForNullId_thenException() {
+ public void givenNullEntity_whenUpdate_thenException() {
+ getService().update(null);
+ }
+
+ @Test
+ public void whenUpdateIsTriggered_thenEntityIsUpdated() {
// When
- getService().create(null);
+ final T entity = this.createNewEntity();
+ getService().update(entity);
// Then
+ verify(getDAO()).save(entity);
}
- /*
- * @Test public final void whenGetIsTriggered_thenEntityIsRetrieved(){ this.configureGet( 1l );
- *
- * // When getService().findOne( 1l );
- *
- * // Then verify( getDAOMock() ).findOne( 1l ); }
- */
- // update
+ // find - paged
@Test
- public final void whenUpdateIsTriggered_thenNoException() {
+ public void whenPageOfEntitiesIsRetrieved_thenResultIsCorrect() {
+ // Given
+ final PageRequest pageRequest = new PageRequest(1, 10);
+ final Page<T> page = new PageImpl<T>(Lists.<T> newArrayList(), pageRequest, 10L);
+ when(getDAO().findAll(eq(pageRequest))).thenReturn(page);
+
// When
- getService().update(this.createNewEntity());
+ final Page<T> found = getService().findPaginated(1, 10, null);
// Then
+ assertSame(page, found);
}
- @Test(expected = NullPointerException.class)
- public final void whenUpdateIsTriggeredForNullEntity_thenException() {
+ // find - all
+
+ @Test
+ public void whenGetAllIsTriggered_thenNoException() {
// When
- getService().update(null);
+ getService().findAll();
// Then
}
@Test
- public final void whenUpdateIsTriggered_thenEntityIsUpdated() {
+ public void whenGetAllIsTriggered_thenAllEntitiesAreRetrieved() {
// When
- final T entity = this.createNewEntity();
- getService().update(entity);
+ getService().findAll();
// Then
- verify(getDAOMock()).save(entity);
+ verify(getDAO()).findAll();
}
// delete
@@ -123,19 +134,19 @@ public final void whenUpdateIsTriggered_thenEntityIsUpdated() {
* - note: the responsibility of ensuring data integrity belongs to the database; because this is an unit test, then no exception is thrown
*/
@Test
- public final void givenResourceDoesNotExist_whenDeleteIsTriggered_thenNothingHappens() {
+ public void givenResourceDoesNotExist_whenDeleteIsTriggered_thenNoExceptions() {
// When
getService().delete(IDUtils.randomPositiveLong());
// Then
}
@Test
- public final void givenResourceExists_whenDeleteIsTriggered_thenNoExceptions() {
+ public void givenResourceExists_whenDeleteIsTriggered_thenNoExceptions() {
final long id = IDUtils.randomPositiveLong();
// Given
- when(getDAOMock().findOne(id)).thenReturn(this.createNewEntity());
+ when(getDAO().findOne(id)).thenReturn(this.createNewEntity());
// When
getService().delete(id);
@@ -144,43 +155,55 @@ public final void givenResourceExists_whenDeleteIsTriggered_thenNoExceptions() {
}
@Test
- public final void givenResourceExists_whenDeleteIsTriggered_thenEntityIsDeleted() {
+ public void givenResourceExists_whenDeleteIsTriggered_thenEntityIsDeleted() {
final long id = IDUtils.randomPositiveLong();
// Given
- when(getDAOMock().findOne(id)).thenReturn(this.createNewEntity());
+ when(getDAO().findOne(id)).thenReturn(this.createNewEntity());
// When
getService().delete(id);
// Then
- verify(getDAOMock()).delete(id);
+ verify(getDAO()).delete(id);
}
- // getAll
+ // delete - all
@Test
- public final void whenGetAllIsTriggered_thenNoException() {
+ public void whenDeleteAllEntities_thenEntitiesAreDeleted() {
// When
- getService().findAll();
+ getService().deleteAll();
// Then
- }
-
- @Test
- public final void whenGetAllIsTriggered_thenAllEntitiesAreRetrieved() {
- // When
- getService().findAll();
-
- // Then
- verify(getDAOMock()).findAll();
+ verify(getDAO()).deleteAll();
}
// template method
protected abstract IService<T> getService();
- protected abstract JpaRepository<T, Long> getDAOMock();
+ protected abstract JpaRepository<T, Long> getDAO();
+
+ //
protected abstract T createNewEntity();
+
+ /**
+ * Creates and returns the instance of entity that is existing (ie ID is not null).
+ *
+ * @return the created entity
+ */
+ protected T createSimulatedExistingEntity() {
+ final T entity = createNewEntity();
+ entity.setId(IDUtils.randomPositiveLong());
+ return entity;
+ }
+
+ //
+
+ protected void stubDaoSave(final T entity) {
+ when(getDAO().save(entity)).thenReturn(entity);
+ }
+
}
Oops, something went wrong.

0 comments on commit c883215

Please sign in to comment.