BAEL-5986 - Getting Started with Blaze Persistence (#13107)

* BAEL-5986 - Getting Started with Blaze Persistence

* Optimize imports

* Fix test class name

* Switch to JUnit5

* Removed dependencies and Controller classes

* Update pom.xml

* Update Configuration class
This commit is contained in:
apeterlic
2022-12-09 18:40:18 +01:00
committed by GitHub
parent 0264bb82c8
commit 10d561451a
18 changed files with 643 additions and 0 deletions
@@ -0,0 +1,49 @@
package com.baeldung;
import com.baeldung.model.Person;
import com.baeldung.model.Post;
import com.baeldung.repository.PersonRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ContextConfiguration(classes = TestContextConfig.class)
@ExtendWith(SpringExtension.class)
public class PersonUnitTest {
@Autowired
private PersonRepository personRepository;
@Test
public void whenFind_thenReturnCorrectListSize() {
final Iterable<Person> listIterable = personRepository.find();
final List<Person> list = new ArrayList<>();
listIterable.forEach(list::add);
assertEquals(2, list.size());
assertEquals("John", list.get(0).getName());
}
@Test
public void whenFindAll_thenReturnCorrectListSize() {
final Iterable<Person> listIterable = personRepository.findAll();
final List<Person> list = new ArrayList<>();
listIterable.forEach(list::add);
assertEquals(3, list.size());
}
@Test
public void whenFindPostsByPerson_thenReturnCorrectListSize() {
final Iterable<Post> listIterable = personRepository.findPostsByPerson();
final List<Post> list = new ArrayList<>();
listIterable.forEach(list::add);
assertEquals(7, list.size());
}
}
@@ -0,0 +1,54 @@
package com.baeldung;
import com.baeldung.model.Post;
import com.baeldung.repository.PostRepository;
import com.baeldung.repository.PostViewRepository;
import com.baeldung.view.PostView;
import com.baeldung.view.PostWithAuthorView;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ContextConfiguration(classes = TestContextConfig.class)
@ExtendWith(SpringExtension.class)
public class PostUnitTest {
@Autowired
private PostViewRepository postViewRepository;
@Autowired
private PostRepository postRepository;
@Test
public void whenFindAll_thenReturnCorrectListViewSize() {
final Iterable<PostWithAuthorView> listIterable = postViewRepository.findAll();
final List<PostView> list = new ArrayList<>();
listIterable.forEach(list::add);
assertEquals(7, list.size());
}
@Test
public void givenPostIdAndAuthorName_whenFind_thenReturnCorrectResult() {
final Iterable<PostWithAuthorView> listIterable =
postRepository.findBy("Spring", "Peter");
final List<PostView> list = new ArrayList<>();
listIterable.forEach(list::add);
assertEquals(4, list.size());
}
@Test
public void whenFindAll_thenReturnCorrectListSize() {
final Iterable<Post> listIterable = postRepository.findAll();
final List<Post> list = new ArrayList<>();
listIterable.forEach(list::add);
assertEquals(7, list.size());
}
}
@@ -0,0 +1,15 @@
package com.baeldung;
import com.blazebit.persistence.integration.view.spring.EnableEntityViews;
import com.blazebit.persistence.spring.data.repository.config.EnableBlazeRepositories;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung")
@EnableEntityViews(basePackages = {"com.baeldung.view"})
@EnableBlazeRepositories(basePackages = "com.baeldung.repository")
public class TestContextConfig {
}