2022-12-09 18:40:18 +01:00
|
|
|
package com.baeldung;
|
|
|
|
|
|
2023-06-10 08:25:24 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2022-12-09 18:40:18 +01:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2023-06-10 08:25:24 +02:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
|
|
|
|
|
import com.baeldung.model.Person;
|
|
|
|
|
import com.baeldung.model.Post;
|
|
|
|
|
import com.baeldung.repository.PersonRepository;
|
2022-12-09 18:40:18 +01:00
|
|
|
|
2023-06-10 08:25:24 +02:00
|
|
|
@SpringBootTest
|
2022-12-09 18:40:18 +01:00
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|