Annotations for using Auto Configuratin for testing entities and repositories. You can easily write a test by using this. It has the following features. -Load the class with @Entitiy and @Repository into ApplicationContext. Classes with other @Service etc. are not loaded. -Rollback the transaction for each test (@Test). Transaction control can be changed. -Use in-memory DB in the test
In addition, TestEntityManager is prepared for manipulating test data in the test class.
See the official documentation for details. 43.3.11 Auto-configured Data JPA Tests
Add a dependency to build.gradle to use the H2 database when testing with JPA.
build.gradle
dependencies {
・ ・ ・
compile('org.springframework.boot:spring-boot-starter-data-jpa')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
}
As a sample, create an entity and repository that handles book information as shown below. The following example is written in Kotlin, but the description method is the same in Java.
@Entity
@Table(name = "book")
data class Book(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //ID is automatically generated
@Column(name = "id")
var id: Long = 0,
@Column(name = "title")
var title: String = "",
@Column(name = "author")
var author: String = "",
@Column
var price: Long = 0
)
Create the following repository for the above entity.
@Repository
interface BookRepository: JpaRepository<Book, Long> {
//A method that returns a list of books with the specified author in ascending order of price
fun findAllByAuthorOrderByPrice(author: String): List<Book>
}
Create a test class. By using TestEntityManager, test data can be stored in DB without using Repository.
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
@DataJpaTest
class BookRepositoryTest {
@Autowired
private lateinit var testEntityManager: TestEntityManager
@Autowired
private lateinit var bookRepository: BookRepository
@Test
fun test() {
//Test data creation
testEntityManager.persist(Book(title = "title1", author = "author1", price = 102))
testEntityManager.persist(Book(title = "title2", author = "author1", price = 101))
testEntityManager.persist(Book(title = "title3", author = "author2", price = 104))
testEntityManager.persist(Book(title = "title4", author = "author2", price = 103))
val foundBooks = bookRepository.findAllByAuthorOrderByPrice("author1")
//There are two books for author1
assertThat(foundBooks).size().isEqualTo(2)
//Search results are in ascending order of price, so title2,Should be in the order of title1
// title1
assertThat(foundBooks[0].id).isEqualTo(2)
assertThat(foundBooks[0].title).isEqualTo("title2")
assertThat(foundBooks[0].author).isEqualTo("author1")
assertThat(foundBooks[0].price).isEqualTo(101)
// title2
assertThat(foundBooks[1].id).isEqualTo(1)
assertThat(foundBooks[1].title).isEqualTo("title1")
assertThat(foundBooks[1].author).isEqualTo("author1")
assertThat(foundBooks[1].price).isEqualTo(102)
}
}
Recommended Posts