Skip to content

Instantly share code, notes, and snippets.

@PRASANTHRAJENDRAN
Created April 14, 2020 11:06
Show Gist options
  • Select an option

  • Save PRASANTHRAJENDRAN/aa5198e327d47e146959c82e38a318fe to your computer and use it in GitHub Desktop.

Select an option

Save PRASANTHRAJENDRAN/aa5198e327d47e146959c82e38a318fe to your computer and use it in GitHub Desktop.
Test case to validate the crud operations with caching
import com.demo.spring.data.redis.cache.demo.SpringBootStarterDataRedisDemoApplication;
import com.demo.spring.data.redis.cache.demo.domain.Photo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
@ActiveProfiles("test")
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = SpringBootStarterDataRedisDemoApplication.class)
class PhotoServiceITest {
@Autowired
private PhotoService photoService = null;
@Test
void testCache() {
Photo photoDto = setup();
Photo photo = photoService.createPhoto(photoDto);
Photo photoDtoGet = photoService.findById(photo.getId());
assertNotNull(photoDtoGet);
assertEquals(photoDto.getId(), photoDtoGet.getId());
String title = photoDto.getTitle();
photoDto.setTitle(UUID.randomUUID().toString());
photoService.updatePhoto(photoDto.getId(), photoDto);
photoDtoGet = photoService.findById(photo.getId());
assertNotEquals(title, photoDtoGet.getTitle());
photoService.deletePhoto(photo.getId());
photoDtoGet = photoService.findById(photo.getId());
assertNull(photoDtoGet);
}
private Photo setup() {
Photo photoDto = new Photo();
photoDto.setId(UUID.randomUUID().toString());
photoDto.setThumbnailUrl(UUID.randomUUID().toString());
photoDto.setTitle(UUID.randomUUID().toString());
photoDto.setUrl(UUID.randomUUID().toString());
return photoDto;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment