Skip to content

Commit c883215

Browse files
author
eugenp
committed
work on unit testing
1 parent 927f880 commit c883215

6 files changed

Lines changed: 106 additions & 95 deletions

File tree

‎eclipse/INTEGRATION-SINGLE.launch‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
33
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
4-
<listEntry value="/rest/src/test/java/org/rest/sec/web/privilege/PrivilegeSearchRESTIntegrationTest.java"/>
4+
<listEntry value="/rest/src/test/java/org/rest/test/suite/UnitTestSuite.java"/>
55
</listAttribute>
66
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
77
<listEntry value="1"/>
@@ -12,10 +12,10 @@
1212
</mapAttribute>
1313
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
1414
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
15-
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value="givenResourceExists_whenResourceIfSearchedByWrongIdAndWrongNameAndUnmarshalled_thenResourceIsFound"/>
15+
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
1616
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
1717
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
18-
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.web.privilege.PrivilegeSearchRESTIntegrationTest"/>
18+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.test.suite.UnitTestSuite"/>
1919
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="rest"/>
2020
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
2121
</launchConfiguration>

‎src/test/java/org/rest/persistence/AbstractPersistenceServiceIntegrationTest.java‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void whenEntitiesAreRetrieved_thenTheResultIsNotNull() {
3737

3838
@Test
3939
public void givenAnEntityExists_whenEntitiesAreRetrieved_thenThereIsAtLeastOneEntity() {
40-
this.getAPI().create(this.createNewEntity());
40+
getAPI().create(createNewEntity());
4141

4242
final List<T> owners = getAPI().findAll();
4343

@@ -46,7 +46,7 @@ public void givenAnEntityExists_whenEntitiesAreRetrieved_thenThereIsAtLeastOneEn
4646

4747
@Test
4848
public void givenAnEntityExists_whenEntitiesAreRetrieved_thenTheExistingEntityIsIndeedAmongThem() {
49-
final T existingEntity = this.getAPI().create(this.createNewEntity());
49+
final T existingEntity = getAPI().create(createNewEntity());
5050

5151
final List<T> owners = getAPI().findAll();
5252

@@ -57,7 +57,7 @@ public void givenAnEntityExists_whenEntitiesAreRetrieved_thenTheExistingEntityIs
5757

5858
@Test
5959
public void givenEntityExists_whenEntityIsRetrieved_thenNoExceptions() {
60-
final T existingEntity = this.getAPI().create(this.createNewEntity());
60+
final T existingEntity = getAPI().create(createNewEntity());
6161
getAPI().findOne(existingEntity.getId());
6262
}
6363

@@ -68,7 +68,7 @@ public void givenEntityDoesNotExist_whenEntityIsRetrieved_thenNoExceptions() {
6868

6969
@Test
7070
public void givenEntityExists_whenEntityIsRetrieved_thenTheResultIsNotNull() {
71-
final T existingEntity = this.getAPI().create(this.createNewEntity());
71+
final T existingEntity = getAPI().create(createNewEntity());
7272
final T retrievedEntity = getAPI().findOne(existingEntity.getId());
7373
assertNotNull(retrievedEntity);
7474
}
@@ -81,7 +81,7 @@ public void givenEntityDoesNotExist_whenEntityIsRetrieved_thenTheResultIsNull()
8181

8282
@Test
8383
public void givenEntityExists_whenEntityIsRetrieved_thenEntityIsRetrievedCorrectly() {
84-
final T existingEntity = this.getAPI().create(this.createNewEntity());
84+
final T existingEntity = getAPI().create(createNewEntity());
8585
final T retrievedEntity = getAPI().findOne(existingEntity.getId());
8686
assertEquals(existingEntity, retrievedEntity);
8787
}
@@ -96,12 +96,12 @@ public void whenNullEntityIsCreated_thenException() {
9696
@Test
9797
@Rollback
9898
public void whenEntityIsCreated_thenNoExceptions() {
99-
this.getAPI().create(this.createNewEntity());
99+
getAPI().create(createNewEntity());
100100
}
101101

102102
@Test
103103
public void whenEntityIsCreated_thenEntityIsRetrievable() {
104-
final T existingEntity = this.getAPI().create(this.createNewEntity());
104+
final T existingEntity = getAPI().create(createNewEntity());
105105

106106
assertNotNull(getAPI().findOne(existingEntity.getId()));
107107
}
@@ -142,22 +142,22 @@ public void whenNullEntityIsUpdated_thenException() {
142142

143143
@Test
144144
public void whenEntityIsUpdated_thenNoExceptions() {
145-
final T existingEntity = this.getAPI().create(this.createNewEntity());
145+
final T existingEntity = getAPI().create(createNewEntity());
146146

147147
getAPI().update(existingEntity);
148148
}
149149

150150
@Test(expected = DataAccessException.class)
151151
public void whenEntityIsUpdatedWithFailedConstraints_thenException() {
152-
final T existingEntity = this.getAPI().create(this.createNewEntity());
152+
final T existingEntity = getAPI().create(createNewEntity());
153153
invalidate(existingEntity);
154154

155155
getAPI().update(existingEntity);
156156
}
157157

158158
@Test
159159
public void whenEntityIsUpdated_thenTheUpdatedAreCorrectlyPersisted() {
160-
final T existingEntity = this.getAPI().create(this.createNewEntity());
160+
final T existingEntity = getAPI().create(createNewEntity());
161161
changeEntity(existingEntity);
162162

163163
getAPI().update(existingEntity);
@@ -183,7 +183,7 @@ public void whenEntityIsDeletedByNegativeId_thenDataAccessException() {
183183
@Test
184184
public void givenEntityExists_whenEntityIsDeleted_thenNoExceptions() {
185185
// Given
186-
final T existingLocation = this.getAPI().create(this.createNewEntity());
186+
final T existingLocation = getAPI().create(createNewEntity());
187187

188188
// When
189189
getAPI().delete(existingLocation.getId());
@@ -192,7 +192,7 @@ public void givenEntityExists_whenEntityIsDeleted_thenNoExceptions() {
192192
@Test
193193
public void givenEntityExists_whenEntityIsDeleted_thenEntityIsNoLongerRetrievable() {
194194
// Given
195-
final T existingEntity = this.getAPI().create(this.createNewEntity());
195+
final T existingEntity = getAPI().create(createNewEntity());
196196

197197
// When
198198
getAPI().delete(existingEntity.getId());
Lines changed: 88 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.rest.persistence.service;
22

3-
import static org.mockito.Matchers.any;
3+
import static org.junit.Assert.assertSame;
4+
import static org.mockito.Matchers.eq;
45
import static org.mockito.Mockito.mock;
56
import static org.mockito.Mockito.verify;
67
import static org.mockito.Mockito.when;
@@ -9,21 +10,18 @@
910
import org.rest.common.IEntity;
1011
import org.rest.util.IDUtils;
1112
import org.springframework.context.ApplicationEventPublisher;
13+
import org.springframework.data.domain.Page;
14+
import org.springframework.data.domain.PageImpl;
15+
import org.springframework.data.domain.PageRequest;
1216
import org.springframework.data.jpa.repository.JpaRepository;
1317

14-
import com.google.common.base.Preconditions;
18+
import com.google.common.collect.Lists;
1519

20+
/**
21+
* A base class for service layer unit tests.
22+
*/
1623
public abstract class AbstractServiceUnitTest<T extends IEntity> {
1724

18-
private Class<T> clazz;
19-
20-
public AbstractServiceUnitTest(final Class<T> clazzToSet) {
21-
super();
22-
23-
Preconditions.checkNotNull(clazzToSet);
24-
this.clazz = clazzToSet;
25-
}
26-
2725
// fixtures
2826

2927
public void before() {
@@ -34,87 +32,100 @@ public void before() {
3432
// tests
3533

3634
@Test
37-
public final void whenServiceIsInitialized_thenNoException() {
35+
public void whenServiceIsInitialized_thenNoException() {
3836
// When
3937
// Then
4038
}
4139

4240
// create
4341

42+
@Test(expected = NullPointerException.class)
43+
public void whenCreateIsTriggeredForNullEntity_thenException() {
44+
// When
45+
getService().create(null);
46+
47+
// Then
48+
}
49+
4450
@Test
45-
public final void whenCreateIsTriggered_thenNoException() {
51+
public void whenCreateIsTriggered_thenNoException() {
4652
// When
4753
getService().create(this.createNewEntity());
4854

4955
// Then
5056
}
5157

52-
@Test(expected = NullPointerException.class)
53-
public final void whenCreateIsTriggeredForNullEntity_thenException() {
58+
@Test
59+
public void whenCreatingANewEntity_thenEntityIsSaved() {
60+
// Given
61+
final T entity = createNewEntity();
62+
stubDaoSave(entity);
63+
5464
// When
55-
getService().create(null);
65+
getService().create(entity);
5666

5767
// Then
68+
verify(getDAO()).save(entity);
5869
}
5970

71+
// update
72+
6073
@Test
61-
public final void whenCreateIsTriggered_thenEntityIsCreated() {
74+
public void whenUpdateIsTriggered_thenNoException() {
6275
// When
63-
getService().create(this.createNewEntity());
76+
getService().update(this.createNewEntity());
6477

6578
// Then
66-
verify(getDAOMock()).save(any(clazz));
6779
}
6880

69-
// get
70-
/*
71-
* @Test public final void whenGetIsTriggered_thenNoException(){ this.configureGet( 1l );
72-
*
73-
* // When getService().findOne( 1l );
74-
*
75-
* // Then }
76-
*/
7781
@Test(expected = NullPointerException.class)
78-
public final void whenGetIsTriggeredForNullId_thenException() {
82+
public void givenNullEntity_whenUpdate_thenException() {
83+
getService().update(null);
84+
}
85+
86+
@Test
87+
public void whenUpdateIsTriggered_thenEntityIsUpdated() {
7988
// When
80-
getService().create(null);
89+
final T entity = this.createNewEntity();
90+
getService().update(entity);
8191

8292
// Then
93+
verify(getDAO()).save(entity);
8394
}
8495

85-
/*
86-
* @Test public final void whenGetIsTriggered_thenEntityIsRetrieved(){ this.configureGet( 1l );
87-
*
88-
* // When getService().findOne( 1l );
89-
*
90-
* // Then verify( getDAOMock() ).findOne( 1l ); }
91-
*/
92-
// update
96+
// find - paged
9397

9498
@Test
95-
public final void whenUpdateIsTriggered_thenNoException() {
99+
public void whenPageOfEntitiesIsRetrieved_thenResultIsCorrect() {
100+
// Given
101+
final PageRequest pageRequest = new PageRequest(1, 10);
102+
final Page<T> page = new PageImpl<T>(Lists.<T> newArrayList(), pageRequest, 10L);
103+
when(getDAO().findAll(eq(pageRequest))).thenReturn(page);
104+
96105
// When
97-
getService().update(this.createNewEntity());
106+
final Page<T> found = getService().findPaginated(1, 10, null);
98107

99108
// Then
109+
assertSame(page, found);
100110
}
101111

102-
@Test(expected = NullPointerException.class)
103-
public final void whenUpdateIsTriggeredForNullEntity_thenException() {
112+
// find - all
113+
114+
@Test
115+
public void whenGetAllIsTriggered_thenNoException() {
104116
// When
105-
getService().update(null);
117+
getService().findAll();
106118

107119
// Then
108120
}
109121

110122
@Test
111-
public final void whenUpdateIsTriggered_thenEntityIsUpdated() {
123+
public void whenGetAllIsTriggered_thenAllEntitiesAreRetrieved() {
112124
// When
113-
final T entity = this.createNewEntity();
114-
getService().update(entity);
125+
getService().findAll();
115126

116127
// Then
117-
verify(getDAOMock()).save(entity);
128+
verify(getDAO()).findAll();
118129
}
119130

120131
// delete
@@ -123,19 +134,19 @@ public final void whenUpdateIsTriggered_thenEntityIsUpdated() {
123134
* - note: the responsibility of ensuring data integrity belongs to the database; because this is an unit test, then no exception is thrown
124135
*/
125136
@Test
126-
public final void givenResourceDoesNotExist_whenDeleteIsTriggered_thenNothingHappens() {
137+
public void givenResourceDoesNotExist_whenDeleteIsTriggered_thenNoExceptions() {
127138
// When
128139
getService().delete(IDUtils.randomPositiveLong());
129140

130141
// Then
131142
}
132143

133144
@Test
134-
public final void givenResourceExists_whenDeleteIsTriggered_thenNoExceptions() {
145+
public void givenResourceExists_whenDeleteIsTriggered_thenNoExceptions() {
135146
final long id = IDUtils.randomPositiveLong();
136147

137148
// Given
138-
when(getDAOMock().findOne(id)).thenReturn(this.createNewEntity());
149+
when(getDAO().findOne(id)).thenReturn(this.createNewEntity());
139150

140151
// When
141152
getService().delete(id);
@@ -144,43 +155,55 @@ public final void givenResourceExists_whenDeleteIsTriggered_thenNoExceptions() {
144155
}
145156

146157
@Test
147-
public final void givenResourceExists_whenDeleteIsTriggered_thenEntityIsDeleted() {
158+
public void givenResourceExists_whenDeleteIsTriggered_thenEntityIsDeleted() {
148159
final long id = IDUtils.randomPositiveLong();
149160

150161
// Given
151-
when(getDAOMock().findOne(id)).thenReturn(this.createNewEntity());
162+
when(getDAO().findOne(id)).thenReturn(this.createNewEntity());
152163

153164
// When
154165
getService().delete(id);
155166

156167
// Then
157-
verify(getDAOMock()).delete(id);
168+
verify(getDAO()).delete(id);
158169
}
159170

160-
// getAll
171+
// delete - all
161172

162173
@Test
163-
public final void whenGetAllIsTriggered_thenNoException() {
174+
public void whenDeleteAllEntities_thenEntitiesAreDeleted() {
164175
// When
165-
getService().findAll();
176+
getService().deleteAll();
166177

167178
// Then
168-
}
169-
170-
@Test
171-
public final void whenGetAllIsTriggered_thenAllEntitiesAreRetrieved() {
172-
// When
173-
getService().findAll();
174-
175-
// Then
176-
verify(getDAOMock()).findAll();
179+
verify(getDAO()).deleteAll();
177180
}
178181

179182
// template method
180183

181184
protected abstract IService<T> getService();
182185

183-
protected abstract JpaRepository<T, Long> getDAOMock();
186+
protected abstract JpaRepository<T, Long> getDAO();
187+
188+
//
184189

185190
protected abstract T createNewEntity();
191+
192+
/**
193+
* Creates and returns the instance of entity that is existing (ie ID is not null).
194+
*
195+
* @return the created entity
196+
*/
197+
protected T createSimulatedExistingEntity() {
198+
final T entity = createNewEntity();
199+
entity.setId(IDUtils.randomPositiveLong());
200+
return entity;
201+
}
202+
203+
//
204+
205+
protected void stubDaoSave(final T entity) {
206+
when(getDAO().save(entity)).thenReturn(entity);
207+
}
208+
186209
}

0 commit comments

Comments
 (0)