11package 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 ;
55import org .springframework .beans .factory .annotation .Autowired ;
66import ru .javawebinar .topjava .model .Meal ;
77import ru .javawebinar .topjava .util .exception .NotFoundException ;
1111import java .time .Month ;
1212
1313import static java .time .LocalDateTime .of ;
14+ import static org .junit .jupiter .api .Assertions .assertEquals ;
15+ import static org .junit .jupiter .api .Assertions .assertThrows ;
1416import static ru .javawebinar .topjava .MealTestData .*;
1517import static ru .javawebinar .topjava .UserTestData .ADMIN_ID ;
1618import 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 );
0 commit comments