|
| 1 | +package org.spring.springboot; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.spring.springboot.domain.City; |
| 8 | +import org.spring.springboot.service.CityService; |
| 9 | +import org.spring.springboot.service.impl.CityServiceImpl; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.test.context.junit4.SpringRunner; |
| 13 | + |
| 14 | +/** |
| 15 | + * Created by bysocket on 05/06/2017. |
| 16 | + */ |
| 17 | +@RunWith(SpringRunner.class) |
| 18 | +@SpringBootTest |
| 19 | +public class ApplicationTests { |
| 20 | + |
| 21 | + private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class); |
| 22 | + |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private CityService cityService; |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testRedis() { |
| 29 | + City city = getShanghai(); |
| 30 | + // 向 redis 中存入数据 |
| 31 | + cityService.saveCity(city); |
| 32 | + |
| 33 | + // 从 redis 中取数据 |
| 34 | + City cityInfo = cityService.getCityByName("上海"); |
| 35 | + |
| 36 | + LOGGER.info(cityInfo.toString()); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testRedisCache() { |
| 42 | + City city = getBeijing(); |
| 43 | + // 向 redis 中存入数据 |
| 44 | + cityService.saveCity(city); |
| 45 | + |
| 46 | + // 从 redis 中取数据, 第一次查询 |
| 47 | + City cityInfo = cityService.getCityByName("北京"); |
| 48 | + LOGGER.info("第一次查询:" + cityInfo.toString()); |
| 49 | + |
| 50 | + // 从 redis 中取数据, 第二次查询 |
| 51 | + cityInfo = cityService.getCityByName("北京"); |
| 52 | + LOGGER.info("第二次查询:" + cityInfo.toString()); |
| 53 | + |
| 54 | + // 更新 city 的描述信息后查询 |
| 55 | + cityService.updateCityDescription("北京", "想不想去北京玩玩呢?"); |
| 56 | + cityInfo = cityService.getCityByName("北京"); |
| 57 | + LOGGER.info("更新描述后查询:" + cityInfo.toString()); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + private City getShanghai(){ |
| 64 | + return new City(1L, 10L, "上海", "人称魔都的地方"); |
| 65 | + } |
| 66 | + |
| 67 | + private City getBeijing(){ |
| 68 | + return new City(2L, 20L, "北京", "中国帝都"); |
| 69 | + } |
| 70 | +} |
0 commit comments