[BAEL-3614] - Spring Redis Configuration From application.properties File (#9226)
* BAEL-3614 - Initial commit * BAEL-3614 - Removing unnecessary properties
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.spring.redis.configuration.controller;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.spring.redis.configuration.entity.Book;
|
||||
import com.baeldung.spring.redis.configuration.repository.BooksRepository;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class BooksControllerUnitTest {
|
||||
|
||||
@Spy
|
||||
@InjectMocks
|
||||
private BooksController booksController;
|
||||
|
||||
@Mock
|
||||
private BooksRepository booksRepository;
|
||||
|
||||
private static final Long BOOK_ID = 1l;
|
||||
private static final Long OTHER_BOOK_ID = 2l;
|
||||
private static final String BOOK_NAME = "Ulysses";
|
||||
private static final String ADDED = "Added";
|
||||
|
||||
@Test
|
||||
public void whenNewBook_thenCorrect() {
|
||||
assertEquals(ADDED, booksController.newBook(new Book(BOOK_ID, BOOK_NAME)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindOneFinds_thenCorrect() {
|
||||
Book book = new Book(BOOK_ID, BOOK_NAME);
|
||||
when(booksRepository.findById(BOOK_ID)).thenReturn(book);
|
||||
assertSame(book, booksController.findOne(BOOK_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindOneNotFound_thenReturnsNull() {
|
||||
Book book = new Book(BOOK_ID, BOOK_NAME);
|
||||
when(booksRepository.findById(BOOK_ID)).thenReturn(book);
|
||||
assertNull(booksController.findOne(OTHER_BOOK_ID));
|
||||
}
|
||||
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.spring.redis.configuration.repository;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.spring.redis.configuration.entity.Book;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class BooksRepositoryUnitTest {
|
||||
|
||||
@Spy
|
||||
@InjectMocks
|
||||
private BooksRepository booksRepository;
|
||||
|
||||
@Mock
|
||||
private RedisTemplate<Long, Book> redisTemplate;
|
||||
|
||||
@Mock
|
||||
private ValueOperations<Long, Book> valueOperations;
|
||||
|
||||
private static final Long BOOK_ID = 1l;
|
||||
private static final Long OTHER_BOOK_ID = 2l;
|
||||
private static final String BOOK_NAME = "Ulysses";
|
||||
|
||||
@Test
|
||||
public void whenSave_thenCorrect() {
|
||||
Book book = new Book(BOOK_ID, BOOK_NAME);
|
||||
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
|
||||
booksRepository.save(book);
|
||||
verify(redisTemplate, times(1)).opsForValue();
|
||||
verify(valueOperations, times(1)).set(BOOK_ID, book);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByIdFound_thenReturnsBook() {
|
||||
Book book = new Book(BOOK_ID, BOOK_NAME);
|
||||
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
|
||||
when(valueOperations.get(BOOK_ID)).thenReturn(book);
|
||||
assertSame(book, booksRepository.findById(BOOK_ID));
|
||||
verify(redisTemplate, times(1)).opsForValue();
|
||||
verify(valueOperations, times(1)).get(BOOK_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByIdNotFound_thenReturnsNull() {
|
||||
Book book = new Book(BOOK_ID, BOOK_NAME);
|
||||
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
|
||||
when(valueOperations.get(BOOK_ID)).thenReturn(book);
|
||||
assertNull(booksRepository.findById(OTHER_BOOK_ID));
|
||||
verify(redisTemplate, times(1)).opsForValue();
|
||||
verify(valueOperations, times(1)).get(OTHER_BOOK_ID);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user