BAEL-3438: migrate to junit5, added small changes

This commit is contained in:
emanuel.trandafir 2023-10-05 21:01:22 +02:00
parent 02a680c9d6
commit ac9f71f090
7 changed files with 63 additions and 61 deletions

View File

@ -54,6 +54,11 @@
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Test containers only dependencies --> <!-- Test containers only dependencies -->
<dependency> <dependency>
<groupId>org.testcontainers</groupId> <groupId>org.testcontainers</groupId>
@ -61,6 +66,12 @@
<version>${testcontainers.version}</version> <version>${testcontainers.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<!-- Test containers only dependencies --> <!-- Test containers only dependencies -->
</dependencies> </dependencies>
@ -87,7 +98,7 @@
<properties> <properties>
<mapstruct.version>1.3.1.Final</mapstruct.version> <mapstruct.version>1.3.1.Final</mapstruct.version>
<testcontainers.version>1.17.3</testcontainers.version> <testcontainers.version>1.19.1</testcontainers.version>
</properties> </properties>
</project> </project>

View File

@ -12,4 +12,5 @@ spring.datasource.url=jdbc:h2:mem:baeldung
spring.jpa.show-sql=false spring.jpa.show-sql=false
#hibernate.dialect=org.hibernate.dialect.H2Dialect #hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto= create
spring.jpa.properties.hibernate.id.new_generator_mappings=false spring.jpa.properties.hibernate.id.new_generator_mappings=false

View File

@ -1,8 +1,9 @@
package com.baeldung.boot.daos; package com.baeldung.boot.daos;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@ -25,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@SpringBootTest
public class UserRepositoryCommon { public class UserRepositoryCommon {
final String USER_EMAIL = "email@example.com"; final String USER_EMAIL = "email@example.com";
@ -274,7 +276,7 @@ public class UserRepositoryCommon {
.getName()).isEqualTo(USER_NAME_ADAM); .getName()).isEqualTo(USER_NAME_ADAM);
} }
@Test(expected = PropertyReferenceException.class) @Test
public void givenUsersInDB_WhenFindAllSortWithFunction_ThenThrowException() { public void givenUsersInDB_WhenFindAllSortWithFunction_ThenThrowException() {
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS)); userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS)); userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
@ -282,10 +284,7 @@ public class UserRepositoryCommon {
userRepository.findAll(Sort.by(Sort.Direction.ASC, "name")); userRepository.findAll(Sort.by(Sort.Direction.ASC, "name"));
List<User> usersSortByNameLength = userRepository.findAll(Sort.by("LENGTH(name)")); assertThrows(PropertyReferenceException.class, () -> userRepository.findAll(Sort.by("LENGTH(name)")));
assertThat(usersSortByNameLength.get(0)
.getName()).isEqualTo(USER_NAME_ADAM);
} }
@Test @Test
@ -556,7 +555,7 @@ public class UserRepositoryCommon {
assertEquals(0, nativeQuery.getResultList().get(0)); assertEquals(0, nativeQuery.getResultList().get(0));
} }
@After @AfterEach
public void cleanUp() { public void cleanUp() {
userRepository.deleteAll(); userRepository.deleteAll();
} }

View File

@ -2,11 +2,10 @@ package com.baeldung.boot.daos;
import com.baeldung.boot.Application; import com.baeldung.boot.Application;
import com.baeldung.boot.domain.User; import com.baeldung.boot.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate; import java.time.LocalDate;
@ -16,7 +15,6 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Created by adam. * Created by adam.
*/ */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
@DirtiesContext @DirtiesContext
public class UserRepositoryIntegrationTest extends UserRepositoryCommon { public class UserRepositoryIntegrationTest extends UserRepositoryCommon {

View File

@ -3,14 +3,15 @@ package com.baeldung.boot.daos;
import com.baeldung.boot.Application; import com.baeldung.boot.Application;
import com.baeldung.boot.domain.User; import com.baeldung.boot.domain.User;
import com.baeldung.util.BaeldungPostgresqlContainer; import com.baeldung.util.BaeldungPostgresqlContainer;
import org.junit.ClassRule;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.time.LocalDate; import java.time.LocalDate;
@ -19,12 +20,12 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Created by adam. * Created by adam.
*/ */
@RunWith(SpringRunner.class) @Testcontainers
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
@ActiveProfiles({"tc", "tc-auto"}) @ActiveProfiles({"tc", "tc-auto"})
public class UserRepositoryTCAutoLiveTest extends UserRepositoryCommon { public class UserRepositoryTCAutoLiveTest extends UserRepositoryCommon {
@ClassRule @Container
public static PostgreSQLContainer<BaeldungPostgresqlContainer> postgreSQLContainer = BaeldungPostgresqlContainer.getInstance(); public static PostgreSQLContainer<BaeldungPostgresqlContainer> postgreSQLContainer = BaeldungPostgresqlContainer.getInstance();
@Test @Test

View File

@ -1,21 +1,21 @@
package com.baeldung.boot.daos; package com.baeldung.boot.daos;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalDate;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import com.baeldung.boot.Application; import com.baeldung.boot.Application;
import com.baeldung.boot.daos.user.UserRepository; import com.baeldung.boot.daos.user.UserRepository;
import com.baeldung.boot.domain.User; import com.baeldung.boot.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@ActiveProfiles("tc-jdbc") @ActiveProfiles("tc-jdbc")
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
public class UserRepositoryTCJdbcLiveTest { public class UserRepositoryTCJdbcLiveTest {

View File

@ -1,33 +1,29 @@
package com.baeldung.boot.daos; package com.baeldung.boot.daos;
import com.baeldung.boot.Application;
import com.baeldung.boot.domain.User;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.PostgreSQLContainer;
import java.time.LocalDate;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) import java.time.LocalDate;
@SpringBootTest(classes = Application.class)
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import com.baeldung.boot.domain.User;
@Testcontainers
@SpringBootTest
@ActiveProfiles("tc") @ActiveProfiles("tc")
@ContextConfiguration(initializers = {UserRepositoryTCLiveTest.Initializer.class})
public class UserRepositoryTCLiveTest extends UserRepositoryCommon { public class UserRepositoryTCLiveTest extends UserRepositoryCommon {
@ClassRule @Container
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1") public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
.withDatabaseName("integration-tests-db") .withDatabaseName("integration-tests-db")
.withUsername("sa") .withUsername("sa")
.withPassword("sa"); .withPassword("sa");
@ -45,14 +41,10 @@ public class UserRepositoryTCLiveTest extends UserRepositoryCommon {
assertThat(updatedUsersSize).isEqualTo(2); assertThat(updatedUsersSize).isEqualTo(2);
} }
static class Initializer @DynamicPropertySource
implements ApplicationContextInitializer<ConfigurableApplicationContext> { static void registerPgProperties(DynamicPropertyRegistry registry) {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) { registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
TestPropertyValues.of( registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
} }
} }