|
1 | 1 | package ru.javawebinar.topjava.service; |
2 | 2 |
|
| 3 | +import org.junit.AfterClass; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Rule; |
3 | 6 | import org.junit.Test; |
| 7 | +import org.junit.rules.Stopwatch; |
| 8 | +import org.junit.runner.Description; |
4 | 9 | import org.junit.runner.RunWith; |
| 10 | +import org.slf4j.Logger; |
5 | 11 | import org.springframework.beans.factory.annotation.Autowired; |
6 | 12 | import org.springframework.dao.DataAccessException; |
7 | 13 | import org.springframework.test.context.ContextConfiguration; |
|
13 | 19 |
|
14 | 20 | import java.time.LocalDate; |
15 | 21 | import java.time.Month; |
| 22 | +import java.util.concurrent.TimeUnit; |
16 | 23 |
|
17 | 24 | import static org.junit.Assert.assertThrows; |
| 25 | +import static org.slf4j.LoggerFactory.getLogger; |
18 | 26 | import static ru.javawebinar.topjava.MealTestData.*; |
19 | 27 | import static ru.javawebinar.topjava.UserTestData.ADMIN_ID; |
20 | 28 | import static ru.javawebinar.topjava.UserTestData.USER_ID; |
|
26 | 34 | @RunWith(SpringRunner.class) |
27 | 35 | @Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8")) |
28 | 36 | public class MealServiceTest { |
| 37 | + private static final Logger log = getLogger("result"); |
| 38 | + |
| 39 | + private static final StringBuilder results = new StringBuilder(); |
| 40 | + |
| 41 | + @Rule |
| 42 | + // http://stackoverflow.com/questions/14892125/what-is-the-best-practice-to-determine-the-execution-time-of-the-bussiness-relev |
| 43 | + public final Stopwatch stopwatch = new Stopwatch() { |
| 44 | + @Override |
| 45 | + protected void finished(long nanos, Description description) { |
| 46 | + String result = String.format("\n%-25s %7d", description.getMethodName(), TimeUnit.NANOSECONDS.toMillis(nanos)); |
| 47 | + results.append(result); |
| 48 | + log.info(result + " ms\n"); |
| 49 | + } |
| 50 | + }; |
29 | 51 |
|
30 | 52 | @Autowired |
31 | 53 | private MealService service; |
32 | 54 |
|
| 55 | + @AfterClass |
| 56 | + public static void printResult() { |
| 57 | + log.info("\n---------------------------------" + |
| 58 | + "\nTest Duration, ms" + |
| 59 | + "\n---------------------------------" + |
| 60 | + results + |
| 61 | + "\n---------------------------------"); |
| 62 | + } |
| 63 | + |
33 | 64 | @Test |
34 | 65 | public void delete() { |
35 | 66 | service.delete(MEAL1_ID, USER_ID); |
@@ -87,7 +118,8 @@ public void update() { |
87 | 118 |
|
88 | 119 | @Test |
89 | 120 | public void updateNotOwn() { |
90 | | - assertThrows(NotFoundException.class, () -> service.update(meal1, ADMIN_ID)); |
| 121 | + NotFoundException exception = assertThrows(NotFoundException.class, () -> service.update(meal1, ADMIN_ID)); |
| 122 | + Assert.assertEquals("Not found entity with id=" + MEAL1_ID, exception.getMessage()); |
91 | 123 | MEAL_MATCHER.assertMatch(service.get(MEAL1_ID, USER_ID), meal1); |
92 | 124 | } |
93 | 125 |
|
|
0 commit comments