{"id":621,"date":"2023-04-30T22:27:08","date_gmt":"2023-04-30T13:27:08","guid":{"rendered":"https:\/\/hirabay.net\/?p=621"},"modified":"2023-04-30T22:27:12","modified_gmt":"2023-04-30T13:27:12","slug":"testcontainers%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e3%80%80%e3%80%9credis%e7%b7%a8%e3%80%9c","status":"publish","type":"post","link":"https:\/\/hirabay.net\/?p=621","title":{"rendered":"testcontainers\u306e\u4f7f\u3044\u65b9\u3000\u301cRedis\u7de8\u301c"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Redis\u306b\u4f9d\u5b58\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\u3092\u3001<br>\u5b9f\u969b\u306bRedis\u3068\u63a5\u7d9a\u3057\u3066\u30c6\u30b9\u30c8\u3057\u305f\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u305d\u3093\u306a\u3068\u304d\u306b\u3001\u300ctestcontainers\u300d\u3092\u4f7f\u3046\u3068\u30c6\u30b9\u30c8\u6642\u306b\u4e00\u6642\u7684\u306b\u8d77\u52d5\u3059\u308bRedis\u3092\u7528\u3044\u3066\u30c6\u30b9\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001<strong><span class=\"keiko_yellow\">testcontainers\u3092\u4f7f\u3063\u3066Redis\u5468\u308a\u306e\u5358\u4f53\u30c6\u30b9\u30c8\u30fb\u7d50\u5408\u30c6\u30b9\u30c8\u3092\u5b9f\u88c5<\/span><\/strong>\u3059\u308b\u65b9\u6cd5\u3092\u307e\u3068\u3081\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"toc\"><br \/>\n<b>Warning<\/b>:  Undefined array key \"is_admin\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>110<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_category_top\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>115<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_top\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>122<\/b><br \/>\n    <div id=\"toc_container\" class=\"sgb-toc--bullets js-smooth-scroll\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">\n  <li class=\"first\">\n    <a href=\"#i-0\">\u5b9f\u884c\u74b0\u5883<\/a>\n  <\/li>\n  <li>\n    <a href=\"#i-1\">\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u30af\u30e9\u30b9<\/a>\n  <\/li>\n  <li>\n    <a href=\"#i-2\">\u5358\u4f53\u30c6\u30b9\u30c8<\/a>\n  <\/li>\n  <li class=\"last\">\n    <a href=\"#i-3\">\u7d50\u5408\u30c6\u30b9\u30c8<\/a>\n  <\/li>\n<\/ul>\n\n      \n    <\/div><\/div><h2 class=\"wp-block-heading\" id=\"i-0\">\u5b9f\u884c\u74b0\u5883<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u30d3\u30eb\u30c9\u30c4\u30fc\u30eb\u306fGradle\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">build.gradle\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-groovy\" data-file=\"build.gradle\" data-lang=\"Groovy\"><code>plugins {\n\tid 'java'\n\tid 'org.springframework.boot' version '3.0.6'\n\tid 'io.spring.dependency-management' version '1.1.0'\n}\n\ngroup = 'hirabay'\nversion = '0.0.1'\nsourceCompatibility = '17'\n\nrepositories {\n\tmavenCentral()\n}\n\next {\n\tset('testcontainersVersion', \"1.18.0\")\n}\n\ndependencies {\n\timplementation 'org.springframework.boot:spring-boot-starter-data-redis'\n\timplementation 'org.springframework.boot:spring-boot-starter-web'\n\n\t\/\/ https:\/\/mvnrepository.com\/artifact\/redis.clients\/jedis\n\ttestImplementation 'redis.clients:jedis:4.3.2'\n\ttestImplementation 'org.springframework.boot:spring-boot-starter-test'\n\ttestImplementation 'org.testcontainers:testcontainers'\n\ttestImplementation 'org.testcontainers:junit-jupiter'\n}\n\ndependencyManagement {\n\timports {\n\t\tmavenBom \"org.testcontainers:testcontainers-bom:${testcontainersVersion}\"\n\t}\n}\n\ntasks.named('test') {\n\tuseJUnitPlatform()\n}<\/code><\/pre><\/div>\n\n\n<p><\/p><div class=\"memo \"><div class=\"memo_ttl dfont\"> \u30e1\u30e2<\/div>start.spring.io \u3067\u4ee5\u4e0b\u306e\u4f9d\u5b58\u3092\u9078\u629e\u3059\u308b\u3068\u540c\u69d8\u306ebuild.gradle\u304c\u751f\u6210\u3055\u308c\u307e\u3059\uff01<br>\n\u30fbTestcontainers<br>\n\u30fbSpring Data Redis (Access+Driver) <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-1\">\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u30af\u30e9\u30b9<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u307e\u305a\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u30af\u30e9\u30b9\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>@Component\n@RequiredArgsConstructor\npublic class RedisClient {\n    private final RedisTemplate&lt;String, String&gt; redisTemplate;\n\n    public String get(String key) {\n        return redisTemplate.opsForValue().get(key);\n    }\n\n    public void set(String key, String value) {\n        redisTemplate.opsForValue().set(key, value);\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-2\">\u5358\u4f53\u30c6\u30b9\u30c8<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u3067\u306f\u5148\u307b\u3069\u306e<code>RedisClient<\/code>\u306e\u5358\u4f53\u30c6\u30b9\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>@Testcontainers\nclass RedisClientTest {\n    \/\/ \u30c6\u30b9\u30c8\u30b3\u30f3\u30c6\u30ca\u3092\u751f\u6210\uff08\u88cf\u3067Docker\u30b3\u30f3\u30c6\u30ca\u304c\u8d77\u52d5\u3059\u308b\uff09\n    @Container\n    public static GenericContainer redis = new GenericContainer(DockerImageName.parse(\"redis:5.0.3-alpine\"))\n            .withExposedPorts(6379);\n\n\n    private RedisClient redisClient;\n\n    @BeforeEach\n    void setup() {\n        \/\/ \u8d77\u52d5\u3057\u305f\u30b3\u30f3\u30c6\u30ca\u306e\u60c5\u5831\u3092\u5143\u306bRedisTemplate\u3092\u751f\u6210\n        var redisConfiguration = new RedisStandaloneConfiguration(redis.getHost(), redis.getMappedPort(6379));\n        var jedisConnectionFactory = new JedisConnectionFactory(redisConfiguration);\n        jedisConnectionFactory.afterPropertiesSet();\n\n        RedisTemplate&lt;String, String&gt; redisTemplate = new RedisTemplate&lt;&gt;();\n        redisTemplate.setConnectionFactory(jedisConnectionFactory);\n        redisTemplate.afterPropertiesSet();\n\n        redisClient = new RedisClient(redisTemplate);\n    }\n\n    @Test\n    void test() {\n        redisClient.set(\"sample\", \"sample\");\n        var actual = redisClient.get(\"sample\");\n        var expected = \"sample\";\n\n        assertThat(actual).isEqualTo(expected);\n    }\n}<\/code><\/pre><\/div>\n\n\n<p><\/p><div class=\"memo \"><div class=\"memo_ttl dfont\"> \u30dd\u30a4\u30f3\u30c8<\/div>\u30fb@Testcontainers\u304c@BeforeAll, @AfterAll\u3067\u30b3\u30f3\u30c6\u30ca\u306e\u958b\u59cb\u3068\u7d42\u4e86\u3092\u7ba1\u7406\u3057\u3066\u304f\u308c\u308b<br>\n\u30fb@Container\u306f@Testcontainers\u306b\u7ba1\u7406\u5bfe\u8c61\u306e\u30b3\u30f3\u30c6\u30ca\u3092\u77e5\u3089\u305b\u308b<br>\n\u30fb\u63a5\u7d9a\u60c5\u5831\u3082GenericContainer\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304b\u3089\u53d6\u5f97<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-3\">\u7d50\u5408\u30c6\u30b9\u30c8<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b21\u306b\u7d50\u5408\u30c6\u30b9\u30c8\u3067\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u5f53\u306fController\u3084Service\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066End to End\u3067\u3084\u308a\u305f\u3044\u306e\u3067\u3059\u304c\u3001<code>RedisClient<\/code>\u3092Autowired\u3057\u3066\u4f7f\u3046\u65b9\u91dd\u3067\u7c21\u7d20\u5316\u3002\u3002\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>@Testcontainers\n@SpringBootTest\nclass ApplicationTest {\n\t@Autowired\n\tprivate RedisClient redisClient;\n\n\n\t\/\/ \u30c6\u30b9\u30c8\u30b3\u30f3\u30c6\u30ca\u3092\u751f\u6210\uff08\u88cf\u3067Docker\u30b3\u30f3\u30c6\u30ca\u304c\u8d77\u52d5\u3059\u308b\uff09\n\t@Container\n\tpublic static  GenericContainer redis = new GenericContainer(DockerImageName.parse(\"redis:5.0.3-alpine\"))\n\t\t\t.withExposedPorts(6379);\n\n\t\/\/ properties\u3092\u66f4\u65b0\n\t@DynamicPropertySource\n\tstatic void properties(DynamicPropertyRegistry registry) {\n\t\tregistry.add(\"spring.data.redis.host\", () -&gt; redis.getHost());\n\t\tregistry.add(\"spring.data.redis.port\", () -&gt; redis.getMappedPort(6379));\n\t}\n\n\t@Test\n\tvoid test() {\n\t\tredisClient.set(\"sample\", \"sample\");\n\t\tvar actual = redisClient.get(\"sample\");\n\t\tvar expected = \"sample\";\n\n\t\tassertThat(actual).isEqualTo(expected);\n\t}\n}<\/code><\/pre><\/div>\n\n\n<p><\/p><div class=\"memo \"><div class=\"memo_ttl dfont\"> \u30dd\u30a4\u30f3\u30c8<\/div>\u30fb@DynamicPropertySource\u3092\u4ed8\u4e0e\u3057\u305f\u30e1\u30bd\u30c3\u30c9\u3067\u8d77\u52d5\u3057\u305f\u30b3\u30f3\u30c6\u30ca\u306e\u60c5\u5831\u3092\u3082\u3068\u306bproperty\u3092\u66f4\u65b0\u3059\u308b<br>\n\u30fb\u4ed6\u306f\u5358\u4f53\u30c6\u30b9\u30c8\u3068\u540c\u3058<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u306b\u4f9d\u5b58\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\u3092\u3001\u5b9f\u969b\u306bRedis\u3068\u63a5\u7d9a\u3057\u3066\u30c6\u30b9\u30c8\u3057\u305f\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002 \u305d\u3093\u306a\u3068\u304d\u306b\u3001\u300ctestcontainers\u300d\u3092\u4f7f\u3046\u3068\u30c6\u30b9\u30c8\u6642\u306b\u4e00\u6642\u7684\u306b\u8d77\u52d5\u3059\u308bRedis\u3092\u7528\u3044\u3066\u30c6\u30b9\u30c8\u3059 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":623,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[5,103,101],"class_list":["post-621","post","type-post","status-publish","format-standard","has-post-thumbnail","category-6","tag-junit5","tag-redis","tag-testcontainers"],"jetpack_featured_media_url":"https:\/\/hirabay.net\/wp-content\/uploads\/2023\/04\/RestTemplate-1.png","_links":{"self":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=621"}],"version-history":[{"count":1,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/621\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/621\/revisions\/624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/media\/623"}],"wp:attachment":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}