11package 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 ;
45import static org .mockito .Mockito .mock ;
56import static org .mockito .Mockito .verify ;
67import static org .mockito .Mockito .when ;
910import org .rest .common .IEntity ;
1011import org .rest .util .IDUtils ;
1112import 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 ;
1216import 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+ */
1623public 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