Skip to content

Commit

Permalink
Caching test
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhave committed Dec 6, 2017
1 parent 2177b78 commit ae88ef6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class CarsApplication {

public static void main(String[] args) {
Expand Down
3 changes: 0 additions & 3 deletions non-reactive/src/main/java/com/example/car/domain/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ public class Car {

private String type;

public Car() {
}

public Car(String name, String type) {
this.name = name;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.car.domain.CarRepository;
import com.example.car.web.CarNotFoundException;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -15,6 +16,7 @@ public CarService(CarRepository carRepository) {
this.carRepository = carRepository;
}

@Cacheable("cars")
public Car getCarDetails(String name) {
Car car = carRepository.findByName(name);
if(car == null) {
Expand Down
36 changes: 36 additions & 0 deletions non-reactive/src/test/java/com/example/car/CachingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.car;

import com.example.car.domain.Car;
import com.example.car.domain.CarRepository;
import com.example.car.service.CarService;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class CachingTest {

@Autowired
private CarService service;

@MockBean
private CarRepository repository;

@Test
public void getCar_ReturnsCachedValue() throws Exception {
given(repository.findByName(anyString())).willReturn(new Car("prius", "hybrid"));
service.getCarDetails("prius");
service.getCarDetails("prius");
verify(repository, times(1)).findByName("prius");
}
}

0 comments on commit ae88ef6

Please sign in to comment.