Skip to content

Commit a46fe82

Browse files
committed
4_3_tests_refactoring
1 parent 6715521 commit a46fe82

5 files changed

Lines changed: 57 additions & 43 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ru.javawebinar.topjava;
2+
3+
import java.util.Arrays;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
/**
8+
* Factory for creating test matchers.
9+
* <p>
10+
* Comparing actual and expected objects via AssertJ
11+
*/
12+
public class MatcherFactory {
13+
public static <T> Matcher<T> usingIgnoringFieldsComparator(String... fieldsToIgnore) {
14+
return new Matcher<>(fieldsToIgnore);
15+
}
16+
17+
public static class Matcher<T> {
18+
private final String[] fieldsToIgnore;
19+
20+
private Matcher(String... fieldsToIgnore) {
21+
this.fieldsToIgnore = fieldsToIgnore;
22+
}
23+
24+
public void assertMatch(T actual, T expected) {
25+
assertThat(actual).usingRecursiveComparison().ignoringFields(fieldsToIgnore).isEqualTo(expected);
26+
}
27+
28+
@SafeVarargs
29+
public final void assertMatch(Iterable<T> actual, T... expected) {
30+
assertMatch(actual, Arrays.asList(expected));
31+
}
32+
33+
public void assertMatch(Iterable<T> actual, Iterable<T> expected) {
34+
assertThat(actual).usingRecursiveFieldByFieldElementComparatorIgnoringFields(fieldsToIgnore).isEqualTo(expected);
35+
}
36+
}
37+
}

‎src/test/java/ru/javawebinar/topjava/MealTestData.java‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import java.util.List;
99

1010
import static java.time.LocalDateTime.of;
11-
import static org.assertj.core.api.Assertions.assertThat;
1211
import static ru.javawebinar.topjava.model.AbstractBaseEntity.START_SEQ;
1312

1413
public class MealTestData {
14+
public static final MatcherFactory.Matcher<Meal> MEAL_MATCHER = MatcherFactory.usingIgnoringFieldsComparator();
15+
1516
public static final int NOT_FOUND = 10;
1617
public static final int MEAL1_ID = START_SEQ + 3;
1718
public static final int ADMIN_MEAL_ID = START_SEQ + 10;
@@ -35,16 +36,4 @@ public static Meal getNew() {
3536
public static Meal getUpdated() {
3637
return new Meal(MEAL1_ID, meal1.getDateTime().plus(2, ChronoUnit.MINUTES), "Обновленный завтрак", 200);
3738
}
38-
39-
public static void assertMatch(Meal actual, Meal expected) {
40-
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
41-
}
42-
43-
public static void assertMatch(Iterable<Meal> actual, Meal... expected) {
44-
assertMatch(actual, Arrays.asList(expected));
45-
}
46-
47-
public static void assertMatch(Iterable<Meal> actual, Iterable<Meal> expected) {
48-
assertThat(actual).usingRecursiveFieldByFieldElementComparator().isEqualTo(expected);
49-
}
5039
}

‎src/test/java/ru/javawebinar/topjava/UserTestData.java‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import ru.javawebinar.topjava.model.Role;
44
import ru.javawebinar.topjava.model.User;
55

6-
import java.util.Arrays;
76
import java.util.Collections;
87
import java.util.Date;
98

10-
import static org.assertj.core.api.Assertions.assertThat;
119
import static ru.javawebinar.topjava.model.AbstractBaseEntity.START_SEQ;
1210

1311
public class UserTestData {
12+
public static final MatcherFactory.Matcher<User> USER_MATCHER = MatcherFactory.usingIgnoringFieldsComparator("registered", "roles");
13+
1414
public static final int USER_ID = START_SEQ;
1515
public static final int ADMIN_ID = START_SEQ + 1;
1616
public static final int GUEST_ID = START_SEQ + 2;
@@ -34,16 +34,4 @@ public static User getUpdated() {
3434
updated.setRoles(Collections.singletonList(Role.ADMIN));
3535
return updated;
3636
}
37-
38-
public static void assertMatch(User actual, User expected) {
39-
assertThat(actual).usingRecursiveComparison().ignoringFields("registered", "roles").isEqualTo(expected);
40-
}
41-
42-
public static void assertMatch(Iterable<User> actual, User... expected) {
43-
assertMatch(actual, Arrays.asList(expected));
44-
}
45-
46-
public static void assertMatch(Iterable<User> actual, Iterable<User> expected) {
47-
assertThat(actual).usingRecursiveFieldByFieldElementComparatorIgnoringFields("registered", "roles").isEqualTo(expected);
48-
}
4937
}

‎src/test/java/ru/javawebinar/topjava/service/MealServiceTest.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public void create() {
5757
int newId = created.getId();
5858
Meal newMeal = getNew();
5959
newMeal.setId(newId);
60-
assertMatch(created, newMeal);
61-
assertMatch(service.get(newId, USER_ID), newMeal);
60+
MEAL_MATCHER.assertMatch(created, newMeal);
61+
MEAL_MATCHER.assertMatch(service.get(newId, USER_ID), newMeal);
6262
}
6363

6464
@Test
@@ -70,7 +70,7 @@ public void duplicateDateTimeCreate() {
7070
@Test
7171
public void get() {
7272
Meal actual = service.get(ADMIN_MEAL_ID, ADMIN_ID);
73-
assertMatch(actual, adminMeal1);
73+
MEAL_MATCHER.assertMatch(actual, adminMeal1);
7474
}
7575

7676
@Test
@@ -87,30 +87,30 @@ public void getNotOwn() {
8787
public void update() {
8888
Meal updated = getUpdated();
8989
service.update(updated, USER_ID);
90-
assertMatch(service.get(MEAL1_ID, USER_ID), getUpdated());
90+
MEAL_MATCHER.assertMatch(service.get(MEAL1_ID, USER_ID), getUpdated());
9191
}
9292

9393
@Test
9494
public void updateNotOwn() {
9595
assertThrows(NotFoundException.class, () -> service.update(meal1, ADMIN_ID));
96-
assertMatch(service.get(MEAL1_ID, USER_ID), meal1);
96+
MEAL_MATCHER.assertMatch(service.get(MEAL1_ID, USER_ID), meal1);
9797
}
9898

9999
@Test
100100
public void getAll() {
101-
assertMatch(service.getAll(USER_ID), meals);
101+
MEAL_MATCHER.assertMatch(service.getAll(USER_ID), meals);
102102
}
103103

104104
@Test
105105
public void getBetweenInclusive() {
106-
assertMatch(service.getBetweenInclusive(
107-
LocalDate.of(2020, Month.JANUARY, 30),
108-
LocalDate.of(2020, Month.JANUARY, 30), USER_ID),
106+
MEAL_MATCHER.assertMatch(service.getBetweenInclusive(
107+
LocalDate.of(2020, Month.JANUARY, 30),
108+
LocalDate.of(2020, Month.JANUARY, 30), USER_ID),
109109
meal3, meal2, meal1);
110110
}
111111

112112
@Test
113113
public void getBetweenWithNullDates() {
114-
assertMatch(service.getBetweenInclusive(null, null, USER_ID), meals);
114+
MEAL_MATCHER.assertMatch(service.getBetweenInclusive(null, null, USER_ID), meals);
115115
}
116116
}

‎src/test/java/ru/javawebinar/topjava/service/UserServiceTest.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public void create() {
4242
Integer newId = created.getId();
4343
User newUser = getNew();
4444
newUser.setId(newId);
45-
assertMatch(created, newUser);
46-
assertMatch(service.get(newId), newUser);
45+
USER_MATCHER.assertMatch(created, newUser);
46+
USER_MATCHER.assertMatch(service.get(newId), newUser);
4747
}
4848

4949
@Test
@@ -66,7 +66,7 @@ public void deletedNotFound() {
6666
@Test
6767
public void get() {
6868
User user = service.get(USER_ID);
69-
assertMatch(user, UserTestData.user);
69+
USER_MATCHER.assertMatch(user, UserTestData.user);
7070
}
7171

7272
@Test
@@ -77,19 +77,19 @@ public void getNotFound() {
7777
@Test
7878
public void getByEmail() {
7979
User user = service.getByEmail("admin@gmail.com");
80-
assertMatch(user, admin);
80+
USER_MATCHER.assertMatch(user, admin);
8181
}
8282

8383
@Test
8484
public void update() {
8585
User updated = getUpdated();
8686
service.update(updated);
87-
assertMatch(service.get(USER_ID), getUpdated());
87+
USER_MATCHER.assertMatch(service.get(USER_ID), getUpdated());
8888
}
8989

9090
@Test
9191
public void getAll() {
9292
List<User> all = service.getAll();
93-
assertMatch(all, admin, guest, user);
93+
USER_MATCHER.assertMatch(all, admin, guest, user);
9494
}
9595
}

0 commit comments

Comments
 (0)