In the previous tutorial, we have seen how to write Integration tests for REST API using Testcontainers.
YouTube Video
In this tutorial, we will learn how to write Integration tests for Data Access or Repository layer using Testcontainers.
The complete source code of this tutorial is on my GitHub repository at https://github.com/RameshMF/springboot-testcontainers-demo
Well, this tutorial is a continuation of the previous tutorial so you have to watch first how to write Integration tests for REST API using Testcontainers tutorial.
If you want to learn more about Spring boot testing then highly suggest my Udemy course: Testing Spring Boot Application with JUnit and Mockito (Includes Testcontainers)
Prerequisite
Spring Boot Testing - REST API Integration Testing using Testcontainers
Create Integration Tests for Data Access or Repository Layer with MySQL database
package net.javaguides.spirngboot.repository;
import net.javaguides.spirngboot.AbstractContainerBaseTest;
import net.javaguides.spirngboot.entity.Student;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class StudentRepositoryTest {
@Autowired
private StudentRepository studentRepository;
// JUnit for save student operation - BDD style
@Test
public void givenStudentObject_whenSave_thenReturnSavedStudent(){
// given - setup or precondition
Student student = Student.builder().firstName("Ramesh")
.lastName("fadatare").email("ramesh@gmail.com").build();
// when - action or the testing
Student savedStudent = studentRepository.save(student);
// then - very output
Assertions.assertNotNull(savedStudent);
Assertions.assertNotNull(savedStudent.getId());
}
// JUnit for findById student operation - BDD style
@Test
public void givenStudentId_whenfindbyId_thenReturnSavedStudent(){
// given - setup or precondition
Student student = Student.builder().firstName("Ramesh")
.lastName("fadatare").email("ramesh@gmail.com").build();
Student savedStudent = studentRepository.save(student);
// when - action or the testing
Student studentDB = studentRepository.findById(student.getId()).get();
// then - very output
Assertions.assertNotNull(studentDB);
}
}
Run Integration tests
What is the problem with the Integration test that we have written?
Write Integration Tests for Data Access or Repository Layer using Testcontainers
Let's change the Integration test to use Testcontainers.@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class StudentRepositoryTest extends AbstractContainerBaseTest {
// code goes here
}
package net.javaguides.spirngboot.repository;
import net.javaguides.spirngboot.AbstractContainerBaseTest;
import net.javaguides.spirngboot.entity.Student;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class StudentRepositoryTest extends AbstractContainerBaseTest {
@Autowired
private StudentRepository studentRepository;
// JUnit for save student operation - BDD style
@Test
public void givenStudentObject_whenSave_thenReturnSavedStudent(){
// given - setup or precondition
Student student = Student.builder().firstName("Ramesh")
.lastName("fadatare").email("ramesh@gmail.com").build();
// when - action or the testing
Student savedStudent = studentRepository.save(student);
// then - very output
Assertions.assertNotNull(savedStudent);
Assertions.assertNotNull(savedStudent.getId());
}
// JUnit for save student operation - BDD style
@Test
public void givenStudentId_whenfindbyId_thenReturnSavedStudent(){
// given - setup or precondition
Student student = Student.builder().firstName("Ramesh")
.lastName("fadatare").email("ramesh@gmail.com").build();
Student savedStudent = studentRepository.save(student);
// when - action or the testing
Student studentDB = studentRepository.findById(student.getId()).get();
// then - very output
Assertions.assertNotNull(studentDB);
}
}
Run Integration tests using Testcontainers
Conclusion
In this tutorial, we have discussed how to perform Spring Boot Data Access or Repository layer Integration testing using Testcontainers.
If you want to learn more about Spring boot testing then highly suggest my Udemy course:
Testing Spring Boot Application with JUnit and Mockito (Includes Testcontainers)
Comments
Post a Comment
Leave Comment