Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c95c47

Browse files
committed
Applied patch - 7_08_JUnit5
1 parent cacb3bc commit 1c95c47

17 files changed

Lines changed: 181 additions & 198 deletions

pom.xml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<!--DB-->
2828
<postgresql.version>42.2.8</postgresql.version>
2929
<!-- Tests -->
30-
<junit.version>4.12</junit.version>
30+
<junit.jupiter.version>5.5.2</junit.jupiter.version>
3131
<hamcrest.version>1.3</hamcrest.version>
3232

3333
<!-- Hibernate -->
@@ -53,6 +53,7 @@
5353
</configuration>
5454
</plugin>
5555
<plugin>
56+
<!--https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven -->
5657
<groupId>org.apache.maven.plugins</groupId>
5758
<artifactId>maven-surefire-plugin</artifactId>
5859
<version>2.22.2</version>
@@ -204,17 +205,12 @@
204205

205206
<!--Test-->
206207
<dependency>
207-
<groupId>junit</groupId>
208-
<artifactId>junit</artifactId>
209-
<version>${junit.version}</version>
208+
<groupId>org.junit.jupiter</groupId>
209+
<artifactId>junit-jupiter-engine</artifactId>
210+
<version>${junit.jupiter.version}</version>
210211
<scope>test</scope>
211-
<exclusions>
212-
<exclusion>
213-
<artifactId>hamcrest-core</artifactId>
214-
<groupId>org.hamcrest</groupId>
215-
</exclusion>
216-
</exclusions>
217212
</dependency>
213+
218214
<dependency>
219215
<groupId>org.hamcrest</groupId>
220216
<artifactId>hamcrest-all</artifactId>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.javawebinar.topjava;
2+
3+
import org.junit.jupiter.api.extension.*;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.util.StopWatch;
7+
8+
public class TimingExtension implements
9+
BeforeTestExecutionCallback, AfterTestExecutionCallback, BeforeAllCallback, AfterAllCallback {
10+
11+
private static final Logger log = LoggerFactory.getLogger("result");
12+
13+
private StopWatch stopWatch;
14+
15+
@Override
16+
public void beforeAll(ExtensionContext extensionContext) throws Exception {
17+
stopWatch = new StopWatch("Execution time of " + extensionContext.getRequiredTestClass().getSimpleName());
18+
}
19+
20+
@Override
21+
public void beforeTestExecution(ExtensionContext extensionContext) throws Exception {
22+
log.info("Start stopWatch");
23+
stopWatch.start(extensionContext.getDisplayName());
24+
}
25+
26+
@Override
27+
public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
28+
stopWatch.stop();
29+
log.info("stop stopWatch");
30+
}
31+
32+
@Override
33+
public void afterAll(ExtensionContext extensionContext) throws Exception {
34+
log.info('\n' + stopWatch.prettyPrint() + '\n');
35+
}
36+
}

src/test/java/ru/javawebinar/topjava/TimingRules.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/test/java/ru/javawebinar/topjava/service/AbstractJpaUserServiceTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ru.javawebinar.topjava.service;
22

3-
import org.junit.Before;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import ru.javawebinar.topjava.model.Role;
77
import ru.javawebinar.topjava.model.User;
@@ -16,15 +16,15 @@ abstract public class AbstractJpaUserServiceTest extends AbstractUserServiceTest
1616
@Autowired
1717
private JpaUtil jpaUtil;
1818

19-
@Before
19+
@BeforeEach
2020
@Override
21-
public void setUp() throws Exception {
21+
void setUp() throws Exception {
2222
super.setUp();
2323
jpaUtil.clear2ndLevelHibernateCache();
2424
}
2525

2626
@Test
27-
public void createWithException() throws Exception {
27+
void createWithException() throws Exception {
2828
validateRootCause(() -> service.create(new User(null, " ", "[email protected]", "password", Role.ROLE_USER)), ConstraintViolationException.class);
2929
validateRootCause(() -> service.create(new User(null, "User", " ", "password", Role.ROLE_USER)), ConstraintViolationException.class);
3030
validateRootCause(() -> service.create(new User(null, "User", "[email protected]", " ", Role.ROLE_USER)), ConstraintViolationException.class);

src/test/java/ru/javawebinar/topjava/service/AbstractMealServiceTest.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ru.javawebinar.topjava.service;
22

3-
import org.junit.Assume;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.Assumptions;
4+
import org.junit.jupiter.api.Test;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import ru.javawebinar.topjava.model.Meal;
77
import ru.javawebinar.topjava.util.exception.NotFoundException;
@@ -11,6 +11,8 @@
1111
import java.time.Month;
1212

1313
import static java.time.LocalDateTime.of;
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertThrows;
1416
import static ru.javawebinar.topjava.MealTestData.*;
1517
import static ru.javawebinar.topjava.UserTestData.ADMIN_ID;
1618
import static ru.javawebinar.topjava.UserTestData.USER_ID;
@@ -21,26 +23,26 @@ public abstract class AbstractMealServiceTest extends AbstractServiceTest {
2123
protected MealService service;
2224

2325
@Test
24-
public void delete() throws Exception {
26+
void delete() throws Exception {
2527
service.delete(MEAL1_ID, USER_ID);
26-
thrown.expect(NotFoundException.class);
27-
service.get(MEAL1_ID, USER_ID);
28+
assertThrows(NotFoundException.class, () ->
29+
service.get(MEAL1_ID, USER_ID));
2830
}
2931

3032
@Test
31-
public void deleteNotFound() throws Exception {
32-
thrown.expect(NotFoundException.class);
33-
service.delete(1, USER_ID);
33+
void deleteNotFound() throws Exception {
34+
assertThrows(NotFoundException.class, () ->
35+
service.delete(1, USER_ID));
3436
}
3537

3638
@Test
37-
public void deleteNotOwn() throws Exception {
38-
thrown.expect(NotFoundException.class);
39-
service.delete(MEAL1_ID, ADMIN_ID);
39+
void deleteNotOwn() throws Exception {
40+
assertThrows(NotFoundException.class, () ->
41+
service.delete(MEAL1_ID, ADMIN_ID));
4042
}
4143

4244
@Test
43-
public void create() throws Exception {
45+
void create() throws Exception {
4446
Meal newMeal = getNew();
4547
Meal created = service.create(newMeal, USER_ID);
4648
Integer newId = created.getId();
@@ -50,44 +52,43 @@ public void create() throws Exception {
5052
}
5153

5254
@Test
53-
public void get() throws Exception {
55+
void get() throws Exception {
5456
Meal actual = service.get(ADMIN_MEAL_ID, ADMIN_ID);
5557
assertMatch(actual, ADMIN_MEAL1);
5658
}
5759

5860
@Test
59-
public void getNotFound() throws Exception {
60-
thrown.expect(NotFoundException.class);
61-
service.get(1, ADMIN_ID);
61+
void getNotFound() throws Exception {
62+
assertThrows(NotFoundException.class, () ->
63+
service.get(1, ADMIN_ID));
6264
}
6365

6466
@Test
65-
public void getNotOwn() throws Exception {
66-
thrown.expect(NotFoundException.class);
67-
service.get(MEAL1_ID, ADMIN_ID);
67+
void getNotOwn() throws Exception {
68+
assertThrows(NotFoundException.class, () ->
69+
service.get(MEAL1_ID, ADMIN_ID));
6870
}
6971

7072
@Test
71-
public void update() throws Exception {
73+
void update() throws Exception {
7274
Meal updated = getUpdated();
7375
service.update(updated, USER_ID);
7476
assertMatch(service.get(MEAL1_ID, USER_ID), updated);
7577
}
7678

7779
@Test
78-
public void updateNotFound() throws Exception {
79-
thrown.expect(NotFoundException.class);
80-
thrown.expectMessage("Not found entity with id=" + MEAL1_ID);
81-
service.update(MEAL1, ADMIN_ID);
80+
void updateNotFound() throws Exception {
81+
NotFoundException e = assertThrows(NotFoundException.class, () -> service.update(MEAL1, ADMIN_ID));
82+
assertEquals(e.getMessage(), "Not found entity with id=" + MEAL1_ID);
8283
}
8384

8485
@Test
85-
public void getAll() throws Exception {
86+
void getAll() throws Exception {
8687
assertMatch(service.getAll(USER_ID), MEALS);
8788
}
8889

8990
@Test
90-
public void getBetween() throws Exception {
91+
void getBetween() throws Exception {
9192
assertMatch(service.getBetweenDates(
9293
LocalDate.of(2015, Month.MAY, 30),
9394
LocalDate.of(2015, Month.MAY, 30), USER_ID), MEAL3, MEAL2, MEAL1);
@@ -100,7 +101,7 @@ public void getBetweenWithNullDates() throws Exception {
100101

101102
@Test
102103
public void createWithException() throws Exception {
103-
Assume.assumeTrue(isJpaBased());
104+
Assumptions.assumeTrue(isJpaBased(), "Validation not supported (JPA only)");
104105
validateRootCause(() -> service.create(new Meal(null, of(2015, Month.JUNE, 1, 18, 0), " ", 300), USER_ID), ConstraintViolationException.class);
105106
validateRootCause(() -> service.create(new Meal(null, null, "Description", 300), USER_ID), ConstraintViolationException.class);
106107
validateRootCause(() -> service.create(new Meal(null, of(2015, Month.JUNE, 1, 18, 0), "Description", 9), USER_ID), ConstraintViolationException.class);
Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,45 @@
11
package ru.javawebinar.topjava.service;
22

3-
import org.junit.Assert;
4-
import org.junit.ClassRule;
5-
import org.junit.Rule;
6-
import org.junit.rules.ExpectedException;
7-
import org.junit.rules.ExternalResource;
8-
import org.junit.rules.Stopwatch;
9-
import org.junit.runner.RunWith;
3+
import org.junit.jupiter.api.extension.ExtendWith;
104
import org.springframework.beans.factory.annotation.Autowired;
115
import org.springframework.core.env.Environment;
126
import org.springframework.test.context.ActiveProfiles;
13-
import org.springframework.test.context.ContextConfiguration;
147
import org.springframework.test.context.jdbc.Sql;
158
import org.springframework.test.context.jdbc.SqlConfig;
16-
import org.springframework.test.context.junit4.SpringRunner;
9+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
1710
import ru.javawebinar.topjava.ActiveDbProfileResolver;
1811
import ru.javawebinar.topjava.Profiles;
19-
import ru.javawebinar.topjava.TimingRules;
12+
import ru.javawebinar.topjava.TimingExtension;
2013

21-
import static org.hamcrest.CoreMatchers.instanceOf;
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
2215
import static ru.javawebinar.topjava.util.ValidationUtil.getRootCause;
2316

24-
@ContextConfiguration({
17+
@SpringJUnitConfig(locations = {
2518
"classpath:spring/spring-app.xml",
2619
"classpath:spring/spring-db.xml"
2720
})
28-
@RunWith(SpringRunner.class)
29-
@Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8"))
21+
//@ExtendWith(SpringExtension.class)
3022
@ActiveProfiles(resolver = ActiveDbProfileResolver.class)
31-
abstract public class AbstractServiceTest {
32-
@ClassRule
33-
public static ExternalResource summary = TimingRules.SUMMARY;
34-
35-
@Rule
36-
public Stopwatch stopwatch = TimingRules.STOPWATCH;
23+
@Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8"))
24+
@ExtendWith(TimingExtension.class)
25+
abstract class AbstractServiceTest {
3726

3827
@Autowired
39-
public Environment env;
40-
41-
@Rule
42-
public ExpectedException thrown = ExpectedException.none();
28+
private Environment env;
4329

44-
public boolean isJpaBased() {
30+
boolean isJpaBased() {
4531
// return Arrays.stream(env.getActiveProfiles()).noneMatch(Profiles.JDBC::equals);
4632
return env.acceptsProfiles(org.springframework.core.env.Profiles.of(Profiles.JPA, Profiles.DATAJPA));
4733
}
4834

4935
// Check root cause in JUnit: https://github.com/junit-team/junit4/pull/778
50-
public <T extends Throwable> void validateRootCause(Runnable runnable, Class<T> exceptionClass) {
51-
try {
52-
runnable.run();
53-
Assert.fail("Expected " + exceptionClass.getName());
54-
} catch (Exception e) {
55-
Assert.assertThat(getRootCause(e), instanceOf(exceptionClass));
56-
}
36+
<T extends Throwable> void validateRootCause(Runnable runnable, Class<T> exceptionClass) {
37+
assertThrows(exceptionClass, () -> {
38+
try {
39+
runnable.run();
40+
} catch (Exception e) {
41+
throw getRootCause(e);
42+
}
43+
});
5744
}
5845
}

0 commit comments

Comments
 (0)