1+ package com .baeldung .repository ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertTrue ;
5+
6+ import java .util .List ;
7+
8+ import org .junit .jupiter .api .Test ;
9+ import org .junit .runner .RunWith ;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .boot .test .context .SpringBootTest ;
12+ import org .springframework .test .context .jdbc .Sql ;
13+ import org .springframework .test .context .junit4 .SpringRunner ;
14+ import org .springframework .transaction .annotation .Transactional ;
15+
16+ import com .baeldung .entity .Fruit ;
17+
18+ @ RunWith (SpringRunner .class )
19+ @ SpringBootTest
20+ class FruitRepositoryIntegrationTest {
21+
22+ @ Autowired
23+ private FruitRepository fruitRepository ;
24+
25+ @ Transactional
26+ @ Test
27+ @ Sql (scripts = { "/test-fruit-data.sql" })
28+ public void givenFruits_WhenDeletedByColor_DeletedFruitShouldReturn () {
29+
30+ List <Fruit > fruits = fruitRepository .deleteByColor ("green" );
31+
32+ assertEquals ("number of fruits are not matching" , 2 , fruits .size ());
33+ fruits .forEach (fruit -> assertEquals ("Its not a green fruit" , "green" , fruit .getColor ()));
34+ }
35+
36+ @ Transactional
37+ @ Test
38+ @ Sql (scripts = { "/test-fruit-data.sql" })
39+ public void givenFruits_WhenDeletedByName_DeletedFruitCountShouldReturn () {
40+
41+ Long deletedFruitCount = fruitRepository .deleteByName ("apple" );
42+
43+ assertEquals ("deleted fruit count is not matching" , 1 , deletedFruitCount .intValue ());
44+ }
45+
46+ @ Transactional
47+ @ Test
48+ @ Sql (scripts = { "/test-fruit-data.sql" })
49+ public void givenFruits_WhenRemovedByColor_DeletedFruitShouldReturn () {
50+
51+ List <Fruit > fruits = fruitRepository .removeByColor ("green" );
52+
53+ assertEquals ("number of fruits are not matching" , 2 , fruits .size ());
54+ fruits .forEach (fruit -> assertEquals ("Its not a green fruit" , "green" , fruit .getColor ()));
55+ }
56+
57+ @ Transactional
58+ @ Test
59+ @ Sql (scripts = { "/test-fruit-data.sql" })
60+ public void givenFruits_WhenRemovedByName_DeletedFruitCountShouldReturn () {
61+
62+ Long deletedFruitCount = fruitRepository .removeByName ("apple" );
63+
64+ assertEquals ("deleted fruit count is not matching" , 1 , deletedFruitCount .intValue ());
65+ }
66+
67+ @ Transactional
68+ @ Test
69+ @ Sql (scripts = { "/test-fruit-data.sql" })
70+ public void givenFruits_WhenDeletedByColorOrName_DeletedFruitShouldReturn () {
71+
72+ List <Fruit > fruits = fruitRepository .deleteFruits ("apple" , "green" );
73+
74+ assertEquals ("number of fruits are not matching" , 3 , fruits .size ());
75+ fruits .forEach (fruit -> assertTrue ("Its not a green fruit or apple" , ("green" .equals (fruit .getColor ())) || "apple" .equals (fruit .getColor ())));
76+ }
77+ }
0 commit comments