diff --git a/non-reactive/src/main/java/com/example/car/CarsApplication.java b/non-reactive/src/main/java/com/example/car/CarsApplication.java index e507158..01068da 100644 --- a/non-reactive/src/main/java/com/example/car/CarsApplication.java +++ b/non-reactive/src/main/java/com/example/car/CarsApplication.java @@ -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) { diff --git a/non-reactive/src/main/java/com/example/car/domain/Car.java b/non-reactive/src/main/java/com/example/car/domain/Car.java index d738af3..067b42c 100644 --- a/non-reactive/src/main/java/com/example/car/domain/Car.java +++ b/non-reactive/src/main/java/com/example/car/domain/Car.java @@ -15,9 +15,6 @@ public class Car { private String type; - public Car() { - } - public Car(String name, String type) { this.name = name; this.type = type; diff --git a/non-reactive/src/main/java/com/example/car/service/CarService.java b/non-reactive/src/main/java/com/example/car/service/CarService.java index ba277f9..762a955 100644 --- a/non-reactive/src/main/java/com/example/car/service/CarService.java +++ b/non-reactive/src/main/java/com/example/car/service/CarService.java @@ -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 @@ -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) { diff --git a/non-reactive/src/test/java/com/example/car/CachingTest.java b/non-reactive/src/test/java/com/example/car/CachingTest.java new file mode 100644 index 0000000..aa05a92 --- /dev/null +++ b/non-reactive/src/test/java/com/example/car/CachingTest.java @@ -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"); + } +}