code examples for @DataJpaTest
This commit is contained in:
@@ -4,3 +4,4 @@
|
||||
* [Structuring and Testing Modules and Layers with Spring Boot](https://reflectoring.io/testing-verticals-and-layers-spring-boot/)
|
||||
* [All You Need To Know About Unit Testing with Spring Boot](https://reflectoring.io/unit-testing-spring-boot/)
|
||||
* [All You Need To Know About Testing Web Controllers with Spring Boot](https://reflectoring.io/spring-boot-web-controller-test/)
|
||||
* [All You Need To Know About Testing JPA Queries with Spring Boot](https://reflectoring.io/spring-boot-data-jpa-test/)
|
||||
|
||||
@@ -27,6 +27,10 @@ repositories {
|
||||
dependencies {
|
||||
compile('org.springframework.boot:spring-boot-starter-data-jpa')
|
||||
compile('org.springframework.boot:spring-boot-starter-web')
|
||||
compile('org.flywaydb:flyway-core')
|
||||
compile('org.liquibase:liquibase-core')
|
||||
compile('com.github.springtestdbunit:spring-test-dbunit:1.3.0')
|
||||
compile('org.dbunit:dbunit:2.6.0')
|
||||
compileOnly('org.projectlombok:lombok')
|
||||
runtime('com.h2database:h2')
|
||||
testCompile('org.springframework.boot:spring-boot-starter-test')
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
package io.reflectoring.testing.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
@@ -9,14 +9,14 @@ import org.springframework.stereotype.Component;
|
||||
@RequiredArgsConstructor
|
||||
public class PersistenceAdapter implements SaveUserPort {
|
||||
|
||||
private final UserEntityRepository userEntityRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public Long saveUser(User user) {
|
||||
UserEntity userEntity = new UserEntity(
|
||||
user.getName(),
|
||||
user.getEmail());
|
||||
UserEntity savedUserEntity = userEntityRepository.save(userEntity);
|
||||
UserEntity savedUserEntity = userRepository.save(userEntity);
|
||||
return savedUserEntity.getId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
package io.reflectoring.testing.persistence;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Table(name = "user")
|
||||
public class UserEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package io.reflectoring.testing.persistence;
|
||||
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface UserEntityRepository extends CrudRepository<UserEntity, Long> {
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.reflectoring.testing.persistence;
|
||||
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
interface UserRepository extends CrudRepository<UserEntity, Long> {
|
||||
|
||||
UserEntity findByName(String name);
|
||||
|
||||
@Query("select u from UserEntity u where u.name = :name")
|
||||
UserEntity findByNameCustomQuery(@Param("name") String name);
|
||||
|
||||
@Query(value = "select * from user as u where u.name = :name", nativeQuery = true)
|
||||
UserEntity findByNameNativeQuery(@Param("name") String name);
|
||||
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.reflectoring.testing.web;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.reflectoring.testing.persistence;
|
||||
|
||||
import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener;
|
||||
import com.github.springtestdbunit.annotation.DatabaseSetup;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
import static org.assertj.core.api.Java6Assertions.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@DataJpaTest
|
||||
@TestPropertySource(properties = {
|
||||
"spring.jpa.hibernate.ddl-auto=create-drop",
|
||||
"spring.liquibase.enabled=false",
|
||||
"spring.flyway.enabled=false"
|
||||
})
|
||||
@TestExecutionListeners({
|
||||
DependencyInjectionTestExecutionListener.class,
|
||||
TransactionDbUnitTestExecutionListener.class
|
||||
})
|
||||
class SpringDbUnitTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
@DatabaseSetup("createUser.xml")
|
||||
void whenInitializedByDbUnit_thenFindsByName() {
|
||||
UserEntity user = userRepository.findByName("Zaphod Beeblebrox");
|
||||
assertThat(user).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.reflectoring.testing.persistence;
|
||||
|
||||
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.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import static org.assertj.core.api.Java6Assertions.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@DataJpaTest
|
||||
@TestPropertySource(properties = {
|
||||
"spring.jpa.hibernate.ddl-auto=create-drop",
|
||||
"spring.liquibase.enabled=false",
|
||||
"spring.flyway.enabled=false"
|
||||
})
|
||||
class SqlTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
@Sql("createUser.sql")
|
||||
void whenInitializedByDbUnit_thenFindsByName() {
|
||||
UserEntity user = userRepository.findByName("Zaphod Beeblebrox");
|
||||
assertThat(user).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
INSERT INTO USER
|
||||
(id,
|
||||
NAME,
|
||||
email)
|
||||
VALUES (1,
|
||||
'Zaphod Beeblebrox',
|
||||
'zaphod@galaxy.net');
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<dataset>
|
||||
<user
|
||||
id="1"
|
||||
name="Zaphod Beeblebrox"
|
||||
email="zaphod@galaxy.net"
|
||||
/>
|
||||
</dataset>
|
||||
Reference in New Issue
Block a user