From 0e9525cde45c5b139fbda6bdfcb49e3728874b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Tue, 1 Oct 2024 20:46:31 +0200 Subject: [PATCH] NOTICKET: Fix Testing setup --- build.gradle.kts | 2 +- .../java/de/hmmh/pmt/auth/AuthConfig.java | 1 - .../java/de/hmmh/pmt/IntegrationTest.java | 61 ++++++++++++++++--- .../hmmh/pmt/PostgresContextInitializer.java | 28 --------- .../resources/application-test.properties | 8 ++- 5 files changed, 60 insertions(+), 40 deletions(-) delete mode 100644 src/test/java/de/hmmh/pmt/PostgresContextInitializer.java diff --git a/build.gradle.kts b/build.gradle.kts index ad78b7f..7b6b717 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -65,6 +65,7 @@ dependencies { testImplementation("org.springframework.security:spring-security-test") testImplementation("org.testcontainers:junit-jupiter") testImplementation("org.testcontainers:postgresql") + testRuntimeOnly("com.h2database:h2") testRuntimeOnly("org.junit.platform:junit-platform-launcher") //OAS @@ -95,7 +96,6 @@ swaggerSources { } tasks { - withType() withType { reports { xml.required.set(true) diff --git a/src/main/java/de/hmmh/pmt/auth/AuthConfig.java b/src/main/java/de/hmmh/pmt/auth/AuthConfig.java index 268a9ec..a1efeb1 100644 --- a/src/main/java/de/hmmh/pmt/auth/AuthConfig.java +++ b/src/main/java/de/hmmh/pmt/auth/AuthConfig.java @@ -11,7 +11,6 @@ import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy; import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy; import org.springframework.security.web.session.HttpSessionEventPublisher; -import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @Configuration @EnableWebSecurity diff --git a/src/test/java/de/hmmh/pmt/IntegrationTest.java b/src/test/java/de/hmmh/pmt/IntegrationTest.java index 6df5bcc..f25bb51 100644 --- a/src/test/java/de/hmmh/pmt/IntegrationTest.java +++ b/src/test/java/de/hmmh/pmt/IntegrationTest.java @@ -1,26 +1,73 @@ package de.hmmh.pmt; +//import de.hmmh.pmt.db.Project; +//import de.hmmh.pmt.db.ProjectRepository; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; +import java.time.LocalDateTime; + @SpringBootTest -@AutoConfigureMockMvc +@AutoConfigureMockMvc(addFilters = false) @ActiveProfiles("test") -@ContextConfiguration(initializers = PostgresContextInitializer.class) public abstract class IntegrationTest { - @Autowired - protected MockMvc mockMvc; + protected final static String baseUri = "/api/v1"; - //protected Repository repository; + @Autowired + protected MockMvc mvc; + //@Autowired + //protected ProjectRepository projectRepository; @BeforeEach void setUp() { - //repository.deleteAll(); + //projectRepository.deleteAll(); + } + + @AfterEach + void cleanUp() { + //projectRepository.deleteAll(); + } + + protected void createTestData() { + /*Project testProject1 = new Project( + 0L, + "testName1", + "testGoaaaaaaaal!", + 0L, + 0L, + LocalDateTime.of(2001, 9, 11, 13, 34), + LocalDateTime.of(2002, 8, 13, 14, 34), + LocalDateTime.of(2003, 7, 12, 23, 34) + ); + Project testProject2 = new Project( + 0L, + "testName2", + "testGoaaaaaaaal!", + 0L, + 0L, + LocalDateTime.of(2009, 9, 11, 13, 34), + LocalDateTime.of(2009, 12, 13, 14, 34), + LocalDateTime.of(2010, 7, 12, 23, 34) + ); + Project testProject3 = new Project( + 0L, + "testName3", + "testGoaaaaaaaal!", + 0L, + 0L, + LocalDateTime.of(2010, 9, 11, 13, 34), + LocalDateTime.of(2012, 8, 13, 14, 34), + LocalDateTime.of(2013, 7, 12, 23, 34) + ); + projectRepository.save(testProject1); + projectRepository.save(testProject2); + projectRepository.save(testProject3); + */ } } diff --git a/src/test/java/de/hmmh/pmt/PostgresContextInitializer.java b/src/test/java/de/hmmh/pmt/PostgresContextInitializer.java deleted file mode 100644 index 97e42a9..0000000 --- a/src/test/java/de/hmmh/pmt/PostgresContextInitializer.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.hmmh.pmt; - -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.utility.DockerImageName; - -public class PostgresContextInitializer implements ApplicationContextInitializer { - - private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>( - DockerImageName.parse("postgres:16") - ) - .withDatabaseName("test_db") - .withUsername("test-db-user") - .withPassword("test-db-password") - .withReuse(true); - - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - postgres.start(); - - TestPropertyValues.of( - "spring.datasource.url=" + postgres.getJdbcUrl(), - "spring.datasource.username=" + postgres.getUsername(), - "spring.datasource.password=" + postgres.getPassword() - ).applyTo(configurableApplicationContext.getEnvironment()); - } -} diff --git a/src/test/resources/application-test.properties b/src/test/resources/application-test.properties index b8de14f..315885a 100644 --- a/src/test/resources/application-test.properties +++ b/src/test/resources/application-test.properties @@ -1,3 +1,5 @@ -spring.datasource.url=set_by_test_containers -spring.datasource.username=set_by_test_containers -spring.datasource.password=set_by_test_containers \ No newline at end of file +spring.datasource.url=jdbc:h2:mem:test_db +spring.datasource.username=test +spring.datasource.password=test +spring.datasource.driver-class-name=org.h2.Driver +spring.jpa.hibernate.ddl-auto=create-drop