{"id":615,"date":"2023-04-26T09:14:44","date_gmt":"2023-04-26T00:14:44","guid":{"rendered":"https:\/\/hirabay.net\/?p=615"},"modified":"2023-04-26T09:14:49","modified_gmt":"2023-04-26T00:14:49","slug":"testcontainers%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e3%80%80%e3%80%9ccassandra%e7%b7%a8%e3%80%9c","status":"publish","type":"post","link":"https:\/\/hirabay.net\/?p=615","title":{"rendered":"testcontainers\u306e\u4f7f\u3044\u65b9\u3000\u301cCassandra\u7de8\u301c"},"content":{"rendered":"\n<p>Cassandra\u306b\u4f9d\u5b58\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\u3092\u3001<br>\u5b9f\u969b\u306bCassandra\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>\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\u308bCassandra\u3092\u7528\u3044\u3066\u30c6\u30b9\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff01<\/p>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001<span class=\"keiko_yellow\"><strong>testcontainer<\/strong><\/span><span class=\"keiko_yellow\"><strong>s<\/strong><\/span><span class=\"keiko_yellow\"><strong>\u3092\u4f7f\u3063\u3066Cassandra\u5468\u308a\u306e\u5358\u4f53\u30c6\u30b9\u30c8\u30fb\u7d50\u5408\u30c6\u30b9\u30c8\u3092\u5b9f\u88c5<\/strong><\/span>\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>\u30d3\u30eb\u30c9\u30c4\u30fc\u30eb\u306fGradle\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>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-cassandra'\n\timplementation 'org.springframework.boot:spring-boot-starter-web'\n\n\ttestImplementation 'org.springframework.boot:spring-boot-starter-test'\n\ttestImplementation 'org.testcontainers:cassandra'\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}\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 for Apache Cassandra<\/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>\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-file=\"CassandraClient.java\" data-lang=\"Java\"><code>@Component\n@RequiredArgsConstructor\npublic class CassandraClient {\n    private final CassandraTemplate cassandraTemplate;\n\n    public List&lt;Sample&gt; findAll() {\n        return cassandraTemplate.select(\"\"\"\n            SELECT *\n            FROM sample.t_sample\n            \"\"\", Sample.class);\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-file=\"Sample.java\" data-lang=\"Java\"><code>@Data\n@Table(\"t_sample\")\npublic class Sample {\n    @PrimaryKey\n    private String key;\n    private String value;\n}<\/code><\/pre><\/div>\n\n\n\n<p>CassandraTemplate\u3092\u4f7f\u3063\u3066CQL\u3092\u5b9f\u884c\u3059\u308b\u30b7\u30f3\u30d7\u30eb\u306a\u4f5c\u308a\u3067\u3059\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-2\">\u5358\u4f53\u30c6\u30b9\u30c8<\/h2>\n\n\n\n<p>\u3067\u306f\u5148\u307b\u3069\u306e<code>CassandraClient<\/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>@Slf4j\n@Testcontainers\nclass CassandraClientTest {\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 CassandraContainer&lt;?&gt; cassandra = new CassandraContainer&lt;&gt;(\"cassandra:3.11.2\")\n            .withInitScript(\"initial.cql\"); \/\/ cassandra\u306e\u521d\u671f\u5316\n\n    private CassandraClient cassandraClient;\n\n    @BeforeEach\n    void setup() {\n        \/\/ \u8d77\u52d5\u3057\u305f\u30b3\u30f3\u30c6\u30ca\u306e\u60c5\u5831\u3092\u5143\u306bsession\u3092\u751f\u6210\n        CqlSession cqlSession = CqlSession.builder()\n                .addContactPoint(cassandra.getContactPoint())\n                .withLocalDatacenter(cassandra.getLocalDatacenter())\n                .build();\n\n        var template = new CassandraTemplate(cqlSession);\n        cassandraClient = new CassandraClient(template);\n    }\n\n    @Test\n    void test() {\n        var actual = cassandraClient.findAll();\n        var expected = new Sample();\n        expected.setKey(\"key1\");\n        expected.setValue(\"value1\");\n\n        assertThat(actual).isEqualTo(Collections.singletonList(expected));\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-sql\" data-file=\"resources\/initial.cql\" data-lang=\"SQL\"><code>CREATE KEYSPACE sample\n    WITH REPLICATION = {'class':'SimpleStrategy','replication_factor':1};\n\nCREATE TABLE sample.t_sample (key text PRIMARY KEY, value text);\n\nINSERT INTO sample.t_sample (key, value) VALUES ('key1', 'value1');<\/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\u30fbCassandraContainer#withInitScript\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u521d\u671f\u5316<br>\n\u30fb\u63a5\u7d9a\u60c5\u5831\u3082CassandraContainer\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>\u6b21\u306b\u7d50\u5408\u30c6\u30b9\u30c8\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u672c\u5f53\u306fController\u3084Sservice\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066End to End\u3067\u3084\u308a\u305f\u3044\u306e\u3067\u3059\u304c\u3001<code>CassandraClient<\/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 CassandraClient cassandraClient;\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 CassandraContainer&lt;?&gt; cassandra = new CassandraContainer&lt;&gt;(\"cassandra:3.11.2\")\n\t\t\t.withInitScript(\"initial.cql\"); \/\/ \u521d\u671f\u5316\n\n\t\/\/ properties\u3092\u66f4\u65b0\n\t@DynamicPropertySource\n\tstatic void properties(DynamicPropertyRegistry registry) {\n\t\tregistry.add(\"spring.cassandra.keyspace-name\", () -&gt; \"sample\");\n\t\tvar contactPoint = \"%s:%d\".formatted(cassandra.getContactPoint().getHostName(), cassandra.getContactPoint().getPort());\n\t\tregistry.add(\"spring.cassandra.contact-points\", () -&gt; contactPoint);\n\t\tregistry.add(\"spring.cassandra.local-datacenter\", () -&gt; cassandra.getLocalDatacenter());\n\t}\n\n\t@Test\n\tvoid test() {\n\t\tvar actual = cassandraClient.findAll();\n\t\tvar expected = new Sample();\n\t\texpected.setKey(\"key1\");\n\t\texpected.setValue(\"value1\");\n\n\t\tassertThat(actual).isEqualTo(Collections.singletonList(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>Cassandra\u306b\u4f9d\u5b58\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\u3092\u3001\u5b9f\u969b\u306bCassandra\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\u308bCassa &#8230; <\/p>\n","protected":false},"author":1,"featured_media":618,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[102,5,101],"class_list":{"0":"post-615","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-6","8":"tag-cassandra","9":"tag-junit5","10":"tag-testcontainers"},"jetpack_featured_media_url":"https:\/\/hirabay.net\/wp-content\/uploads\/2023\/04\/RestTemplate.png","_links":{"self":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/615","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=615"}],"version-history":[{"count":4,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":622,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions\/622"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/media\/618"}],"wp:attachment":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}