11package com .baeldung .relationships ;
22
3- import static org . springframework . util . Assert . isTrue ;
4-
5- import java . util . Date ;
6- import java . util . List ;
7-
8- import javax . servlet . ServletContext ;
9-
10- import org .junit .AfterClass ;
3+ import com . baeldung . AppConfig ;
4+ import com . baeldung . data . repositories . TweetRepository ;
5+ import com . baeldung . data . repositories . UserRepository ;
6+ import com . baeldung . models . AppUser ;
7+ import com . baeldung . models . Tweet ;
8+ import com . baeldung . security . AppUserPrincipal ;
9+ import com . baeldung . util . DummyContentUtil ;
10+ import org .junit .After ;
1111import org .junit .Before ;
1212import org .junit .Test ;
1313import org .junit .runner .RunWith ;
1414import org .springframework .beans .factory .annotation .Autowired ;
1515import org .springframework .dao .InvalidDataAccessApiUsageException ;
1616import org .springframework .data .domain .Page ;
1717import org .springframework .data .domain .PageRequest ;
18+ import org .springframework .jdbc .core .JdbcTemplate ;
1819import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1920import org .springframework .security .core .Authentication ;
2021import org .springframework .security .core .context .SecurityContextHolder ;
2122import org .springframework .test .annotation .DirtiesContext ;
2223import org .springframework .test .context .ContextConfiguration ;
2324import org .springframework .test .context .junit4 .SpringRunner ;
2425import org .springframework .test .context .web .WebAppConfiguration ;
26+ import org .springframework .test .jdbc .JdbcTestUtils ;
2527import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
2628
27- import com .baeldung .AppConfig ;
28- import com .baeldung .data .repositories .TweetRepository ;
29- import com .baeldung .data .repositories .UserRepository ;
30- import com .baeldung .models .AppUser ;
31- import com .baeldung .models .Tweet ;
32- import com .baeldung .security .AppUserPrincipal ;
33- import com .baeldung .util .DummyContentUtil ;
29+ import javax .servlet .ServletContext ;
30+ import java .util .Date ;
31+ import java .util .List ;
32+
33+ import static org .springframework .util .Assert .isTrue ;
3434
3535@ RunWith (SpringRunner .class )
3636@ WebAppConfiguration
@@ -54,10 +54,22 @@ public void testInit() {
5454 tweetRepository .saveAll (DummyContentUtil .generateDummyTweets (appUsers ));
5555 }
5656
57- @ AfterClass
58- public static void tearDown () {
59- tweetRepository .deleteAll ();
60- userRepository .deleteAll ();
57+ /**
58+ * This is to ensure the tables are dropped in proper order.
59+ * After the Spring Boot 2.2.2 upgrade, DDL statements generated automatically try to drop Tweet table first.
60+ * As a result we get org.h2.jdbc.JdbcSQLSyntaxErrorException because Tweet_Likes table depends on Tweet.
61+ *
62+ * @see <a href="https://stackoverflow.com/questions/59364212/integrationtest-isolation-fails-in-springboot-2-2-2-release-error-dopping-table">
63+ * StackOverflow#59364212
64+ * </a>
65+ * @see <a href="https://stackoverflow.com/questions/59561551/hibernate-h2-specify-drop-table-order">
66+ * StackOverflow#59561551
67+ * </a>
68+ */
69+ @ After
70+ public void tearDown () {
71+ JdbcTemplate jdbcTemplate = ctx .getBean (JdbcTemplate .class );
72+ JdbcTestUtils .dropTables (jdbcTemplate , "Tweet_Likes" , "Tweet" );
6173 }
6274
6375 @ Test
0 commit comments