[BAEL-3841] declarative caching testing

This commit is contained in:
Adrian Maghear 2020-06-04 16:29:55 +02:00
parent 455d2a34a4
commit 536008dff3
7 changed files with 194 additions and 26 deletions

View File

@ -58,6 +58,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
</dependencies>
<properties>
<ehcache.version>3.5.2</ehcache.version>

View File

@ -0,0 +1,21 @@
package com.baeldung.springdatacaching.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.UUID;
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class Book {
@Id
private UUID id;
private String title;
}

View File

@ -0,0 +1,15 @@
package com.baeldung.springdatacaching.repositories;
import com.baeldung.springdatacaching.model.Book;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.repository.CrudRepository;
import java.util.Optional;
import java.util.UUID;
public interface BookRepository extends CrudRepository<Book, UUID> {
@Cacheable(value = "books", unless="#a0=='Foundation'")
Optional<Book> findFirstByTitle(String title);
}

View File

@ -1,7 +0,0 @@
INSERT INTO CUSTOMER VALUES(1001,'BAELDUNG');
INSERT INTO ITEM VALUES(10001,'ITEM1',50.0);
INSERT INTO ITEM VALUES(10002,'ITEM2',100.0);
INSERT INTO ORDERDETAIL VALUES(300001,1001,10001,2);
INSERT INTO ORDERDETAIL VALUES(300002,1001,10002,5);

View File

@ -1,19 +0,0 @@
CREATE TABLE CUSTOMER(
CUSTOMERID INT PRIMARY KEY,
CUSTOMERNAME VARCHAR(250) NOT NULL
);
CREATE TABLE ITEM(
ITEMID INT PRIMARY KEY,
ITEMDESC VARCHAR(250),
PRICE DOUBLE
);
CREATE TABLE ORDERDETAIL(
ORDERID INT PRIMARY KEY,
CUSTOMERID INT NOT NULL,
ITEMID INT NOT NULL,
QUANTITY INT,
FOREIGN KEY (customerid) references CUSTOMER(customerid),
FOREIGN KEY (itemid) references ITEM(itemid)
);

View File

@ -0,0 +1,86 @@
package com.baeldung.springdatacaching.repositories;
import com.baeldung.springdatacaching.model.Book;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.AopTestUtils;
import java.util.UUID;
import static java.util.Optional.of;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
@ContextConfiguration
@ExtendWith(SpringExtension.class)
public class BookRepositoryCachingIntegrationTest {
private static final Book DUNE = new Book(UUID.randomUUID(), "Dune");
private static final Book FOUNDATION = new Book(UUID.randomUUID(), "Foundation");
private BookRepository mock;
@Autowired
private BookRepository bookRepository;
@EnableCaching
@Configuration
public static class CachingTestConfig {
@Bean
public BookRepository bookRepositoryMockImplementation() {
return mock(BookRepository.class);
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("books");
}
}
@BeforeEach
void setUp() {
mock = AopTestUtils.getTargetObject(bookRepository);
reset(mock);
when(mock.findFirstByTitle(eq("Foundation")))
.thenReturn(of(FOUNDATION));
when(mock.findFirstByTitle(eq("Dune")))
.thenReturn(of(DUNE))
.thenThrow(new RuntimeException("Book should be cached!"));
}
@Test
void givenCachedBookWhenFindByTitleThenRepositoryShouldNotBeHit() {
assertEquals(of(DUNE), bookRepository.findFirstByTitle("Dune"));
verify(mock).findFirstByTitle("Dune");
assertEquals(of(DUNE), bookRepository.findFirstByTitle("Dune"));
assertEquals(of(DUNE), bookRepository.findFirstByTitle("Dune"));
verifyNoMoreInteractions(mock);
}
@Test
void givenNotCachedBookWhenFindByTitleThenRepositoryShouldBeHit() {
assertEquals(of(FOUNDATION), bookRepository.findFirstByTitle("Foundation"));
assertEquals(of(FOUNDATION), bookRepository.findFirstByTitle("Foundation"));
assertEquals(of(FOUNDATION), bookRepository.findFirstByTitle("Foundation"));
verify(mock, times(3)).findFirstByTitle("Foundation");
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.springdatacaching.repositories;
import com.baeldung.caching.boot.CacheApplication;
import com.baeldung.springdatacaching.model.Book;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional;
import java.util.UUID;
import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@EntityScan(basePackageClasses = Book.class)
@SpringBootTest(classes = CacheApplication.class)
@EnableJpaRepositories(basePackageClasses = BookRepository.class)
public class BookRepositoryIntegrationTest {
@Autowired
CacheManager cacheManager;
@Autowired
BookRepository repository;
@BeforeEach
void setUp() {
repository.save(new Book(UUID.randomUUID(), "Dune"));
repository.save(new Book(UUID.randomUUID(), "Foundation"));
}
@Test
void givenBookThatShouldBeCachedWhenFindByTitleThenResultShouldBePutInCache() {
Optional<Book> dune = repository.findFirstByTitle("Dune");
assertEquals(dune, getCachedBook("Dune"));
}
@Test
void givenBookThatShouldNotBeCachedWhenFindByTitleThenResultShouldNotBePutInCache() {
repository.findFirstByTitle("Foundation");
assertEquals(empty(), getCachedBook("Foundation"));
}
private Optional<Book> getCachedBook(String title) {
return ofNullable(cacheManager.getCache("books")).map(c -> c.get(title, Book.class));
}
}