Add MigrationBehaviorTests.java
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package com.ankurm.sdjpa4demo;
|
||||
|
||||
import com.ankurm.sdjpa4demo.domain.Author;
|
||||
import com.ankurm.sdjpa4demo.domain.Book;
|
||||
import com.ankurm.sdjpa4demo.repo.AuthorRepository;
|
||||
import com.ankurm.sdjpa4demo.repo.BookRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@Transactional
|
||||
class MigrationBehaviorTests {
|
||||
|
||||
@Autowired AuthorRepository authors;
|
||||
@Autowired BookRepository books;
|
||||
|
||||
private void seed() {
|
||||
Author a1 = authors.save(new Author("Joshua Bloch", "US"));
|
||||
authors.save(new Author("Anonymous", null));
|
||||
books.save(new Book("Effective Java", new BigDecimal("45.00"), a1));
|
||||
books.save(new Book("Refactoring", new BigDecimal("45.00"), a1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionalMissingReturnsEmpty() {
|
||||
seed();
|
||||
assertTrue(authors.findByName("nope").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonNullMissingThrows() {
|
||||
seed();
|
||||
assertThrows(EmptyResultDataAccessException.class, () -> authors.getByName("nope"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullMarkedRejectsNullArgument() {
|
||||
seed();
|
||||
assertThrows(IllegalArgumentException.class, () -> authors.getByName(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullableParamAcceptsNull() {
|
||||
seed();
|
||||
assertDoesNotThrow(() -> authors.findByCountry(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullsPrecedenceIsHonored() {
|
||||
seed();
|
||||
List<String> first = authors.findByNameStartingWith("",
|
||||
Sort.by(Sort.Order.asc("country").nullsFirst()))
|
||||
.stream().map(Author::getCountry).toList();
|
||||
assertNull(first.get(0), "NULLS FIRST should put null country first");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonUniqueSingleResultThrows() {
|
||||
seed();
|
||||
assertThrows(IncorrectResultSizeDataAccessException.class,
|
||||
() -> books.findByPrice(new BigDecimal("45.00")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void newJpqlFunctionsWork() {
|
||||
Author a = authors.save(new Author("Erich Gamma", "CH"));
|
||||
assertEquals("Erich_Gamma:CH", authors.badgeFor(a.getId()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user