Created
August 30, 2018 09:00
-
-
Save JorgenRingen/a56837fc07e630c32280b8e3d14c2d24 to your computer and use it in GitHub Desktop.
Clean up database when using @SpringBootTest and initiating transactions from the test. Test and server runs in different threads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package no.vegvesen.kjoretoy.registrering.register.web; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.persistence.Table; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import org.springframework.beans.factory.InitializingBean; | |
import org.springframework.context.annotation.Profile; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
/** | |
* Inspirert av: <a href="https://medium.com/@dSebastien/cleaning-up-database-tables-after-each-integration-test-method-with-spring-boot-2-and-kotlin-7279abcdd5cc"> | |
* https://medium.com/@dSebastien/cleaning-up-database-tables-after-each-integration-test-method-with-spring-boot-2-and-kotlin-7279abcdd5cc | |
* </a> | |
* <p> | |
* Gjør manuell opprydding etter hver test, da automatisk rollback ikke er mulig med kombinasjonen @SpringBootTest og SpringBootTest.WebEnvironment.RANDOM_PORT. | |
* <blockquote> | |
* If your test is @Transactional, it rolls back the transaction at the end of each test method by default. | |
* However, as using this arrangement with either RANDOM_PORT or DEFINED_PORT implicitly provides a real servlet environment, | |
* the HTTP client and server run in separate threads and, thus, in separate transactions. Any transaction initiated on | |
* the server does not roll back in this case. | |
* <cite> | |
* <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html"> | |
* https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html | |
* </a> | |
* </cite> | |
* </blockquote> | |
*/ | |
@Service | |
@Profile("test") | |
public class DatabaseCleanup implements InitializingBean { | |
@PersistenceContext | |
private EntityManager entityManager; | |
private List<String> tableNames; | |
@Transactional | |
public void execute() { | |
entityManager.flush(); | |
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate(); | |
for (final String tableName : tableNames) { | |
entityManager.createNativeQuery("TRUNCATE TABLE " + tableName).executeUpdate(); | |
} | |
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate(); | |
} | |
@Override | |
public void afterPropertiesSet() throws Exception { | |
tableNames = entityManager.getMetamodel().getEntities().stream() | |
.filter(e -> e.getJavaType().getAnnotation(Table.class) != null) | |
.map(e -> e.getJavaType().getAnnotation(Table.class).name()) | |
.collect(Collectors.toList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment