{"id":21588,"date":"2023-04-04T17:55:37","date_gmt":"2023-04-04T10:55:37","guid":{"rendered":"https:\/\/huongdanjava.com\/?p=21588"},"modified":"2026-02-15T07:06:36","modified_gmt":"2026-02-15T00:06:36","slug":"introduction-about-testcontainers","status":"publish","type":"post","link":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html","title":{"rendered":"Introduction about Testcontainers"},"content":{"rendered":"<p><a href=\"https:\/\/www.testcontainers.org\/\" target=\"_blank\" rel=\"noopener\">Testcontainers<\/a> is a library that helps us write unit tests for applications and databases running on a Docker container. Testcontainers will help us run applications or databases using Docker Images, then we will use the plugins it supports to implement the unit test code we want. Testcontainers supports many different languages, in addition to Java, it also has versions for Go, .Net, Node.js, Python, Rust, Haskell. In this tutorial, I will show you how to use Testcontainers for Java to write unit tests for the MySQL database manipulation part of a simple Java application.<\/p>\n<p>First, I will create a new Maven project as an example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21590 aligncenter\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/introduction-about-testcontainers-1.png\" alt=\"\" width=\"700\" height=\"461\" \/><\/p>\n<p>I will declare Testcontainers with dependency management, JUnit Jupiter and MySQL JDBC driver dependency for this project as follows:<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;dependencyManagement&gt;\r\n  &lt;dependencies&gt;\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.testcontainers&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;testcontainers-bom&lt;\/artifactId&gt;\r\n      &lt;version&gt;${testcontainers.version}&lt;\/version&gt;\r\n      &lt;type&gt;pom&lt;\/type&gt;\r\n      &lt;scope&gt;import&lt;\/scope&gt;\r\n    &lt;\/dependency&gt;\r\n  &lt;\/dependencies&gt;\r\n&lt;\/dependencyManagement&gt;\r\n\r\n&lt;dependencies&gt;\r\n  &lt;dependency&gt;\r\n    &lt;groupId&gt;com.mysql&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;mysql-connector-j&lt;\/artifactId&gt;\r\n    &lt;version&gt;${mysql-connector-j.version}&lt;\/version&gt;\r\n  &lt;\/dependency&gt;\r\n\r\n  &lt;dependency&gt;\r\n    &lt;groupId&gt;org.testcontainers&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;testcontainers&lt;\/artifactId&gt;\r\n    &lt;scope&gt;test&lt;\/scope&gt;\r\n  &lt;\/dependency&gt;\r\n  &lt;dependency&gt;\r\n    &lt;groupId&gt;org.testcontainers&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;testcontainers-junit-jupiter&lt;\/artifactId&gt;\r\n    &lt;scope&gt;test&lt;\/scope&gt;\r\n  &lt;\/dependency&gt;\r\n  &lt;dependency&gt;\r\n    &lt;groupId&gt;org.testcontainers&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;testcontainers-mysql&lt;\/artifactId&gt;\r\n    &lt;scope&gt;test&lt;\/scope&gt;\r\n  &lt;\/dependency&gt;\r\n\r\n  &lt;dependency&gt;\r\n    &lt;groupId&gt;org.junit.jupiter&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;junit-jupiter&lt;\/artifactId&gt;\r\n    &lt;version&gt;${junit-jupiter.version}&lt;\/version&gt;\r\n    &lt;scope&gt;test&lt;\/scope&gt;\r\n  &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;<\/pre>\n<p>with:<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;properties&gt;\r\n  &lt;maven.compiler.source&gt;25&lt;\/maven.compiler.source&gt;\r\n  &lt;maven.compiler.target&gt;25&lt;\/maven.compiler.target&gt;\r\n  &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\r\n\r\n  &lt;testcontainers.version&gt;2.0.3&lt;\/testcontainers.version&gt;\r\n  &lt;mysql-connector-j.version&gt;9.6.0&lt;\/mysql-connector-j.version&gt;\r\n  &lt;junit-jupiter.version&gt;6.0.2&lt;\/junit-jupiter.version&gt;\r\n&lt;\/properties&gt;<\/pre>\n<p>As an example, I will define a student table in MySQL database with the following simple structure:<\/p>\n<pre class=\"lang:mysql decode:true \">CREATE TABLE `students` (\r\n  `id` int(11) NOT NULL AUTO_INCREMENT,\r\n  `name` varchar(45) NOT NULL,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;<\/pre>\n<p>The data in the student table is as follows:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21591 aligncenter\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/introduction-about-testcontainers-2.png\" alt=\"\" width=\"700\" height=\"291\" \/><\/p>\n<p>To get information in this student table, I will write JDBC code as follows:<\/p>\n<pre class=\"lang:java decode:true\">package com.huongdanjava.testcontainers;\r\n\r\nimport java.sql.Connection;\r\nimport java.sql.ResultSet;\r\nimport java.sql.SQLException;\r\nimport java.sql.Statement;\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\npublic class StudentService {\r\n\r\n  public List&lt;Student&gt; getAllStudents(Connection connection) throws SQLException {\r\n    List&lt;Student&gt; students = new ArrayList&lt;&gt;();\r\n    Statement statement = connection.createStatement();\r\n\r\n    ResultSet rs = statement.executeQuery(\"SELECT * FROM students\");\r\n    while (rs.next()) {\r\n      Student student = new Student(rs.getInt(\"id\"), rs.getString(\"name\"));\r\n      students.add(student);\r\n    }\r\n\r\n    return students;\r\n  }\r\n}\r\n<\/pre>\n<p>with the Student class with the following content:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.testcontainers;\r\n\r\npublic record Student(int id, String name) {\r\n\r\n}\r\n<\/pre>\n<p>The main class to run this application is as follows:<\/p>\n<pre class=\"lang:java decode:true\">package com.huongdanjava.testcontainers;\r\n\r\nimport java.sql.Connection;\r\nimport java.sql.DriverManager;\r\nimport java.sql.SQLException;\r\nimport java.util.List;\r\n\r\npublic class Application {\r\n\r\n  public static void main(String[] args) throws SQLException {\r\n    Connection connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/example\",\r\n        \"root\", \"123456\");\r\n    StudentService studentService = new StudentService();\r\n    List&lt;Student&gt; students = studentService.getAllStudents(connection);\r\n\r\n    students.forEach(s -&gt; {\r\n      System.out.println(\"ID: \" + s.id() + \", name: \" + s.name());\r\n    });\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>Result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21592 aligncenter\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/introduction-about-testcontainers-3.png\" alt=\"\" width=\"700\" height=\"422\" \/><\/p>\n<p>To write unit tests for the getAllStudents() method to get student information from the database, in the past, you could use the H2 in-memory database. With this approach, our unit test code doesn&#8217;t actually run against the database we are using for the application. And so we can run into issues when running the application that the unit test code fails to detect.<\/p>\n<p>Now you can use Testcontainers to solve this shortcoming. Testcontainer will help us to run unit test code against the real database that our application is using.<\/p>\n<p><strong>The disadvantage of this solution is that you must have Docker installed on the machine running the unit test<\/strong>, but this inconvenience is not a big problem, right?<\/p>\n<p>To write unit tests for the StudentService class using Testcontainers, I will create a new class and annotate this class with the @Testcontainers annotation of the Testcontainer as follows:<\/p>\n<pre class=\"lang:java decode:true\">package com.huongdanjava.testcontainers;\r\n\r\nimport org.testcontainers.junit.jupiter.Testcontainers;\r\n\r\n@Testcontainers\r\npublic class StudentServiceTest {\r\n\r\n}\r\n<\/pre>\n<p>This @Testcontainers annotation has a disabledWithoutDocker attribute that helps our unit test code with Testcontainers will automatically disable if the machine running the unit test does not have that Docker installed! The default value of this property is false which means that our unit test code will not be disabled if the machine running the unit test does not have Docker installed. Depending on the situation, you can add this attribute if you want. My machine is installing Docker, so I don&#8217;t need to declare this attribute.<\/p>\n<p>Now we will declare the container that we will need to run before the unit test code is executed with the @Container annotation. My example will be like this:<\/p>\n<pre class=\"lang:java decode:true\">@Container\r\npublic static MySQLContainer&lt;?&gt; container = new MySQLContainer&lt;&gt;(\"mysql:latest\")\r\n    .withDatabaseName(\"example\")\r\n    .withPassword(\"123456\")\r\n    .withInitScript(\"db.sql\")\r\n    .withReuse(true);<\/pre>\n<p>By default, <a href=\"https:\/\/www.testcontainers.org\/modules\/databases\/mysql\/\" target=\"_blank\" rel=\"noopener\">the MySQL container with Testcontainer<\/a> will run with the user &#8220;root&#8221; and the password &#8220;test&#8221;. For my example, I changed the password to &#8220;123456&#8221; as you can see. My database name is &#8220;example&#8221;.<\/p>\n<p>I also declare an initial SQL script so that when running the container, Testcontainers will automatically create a new students table and insert data into this table. The contents of the db.sql file located in the src\/test\/resources directory are as follows:<\/p>\n<pre class=\"lang:mysql decode:true \">CREATE TABLE `students` (\r\n  `id` int(11) NOT NULL AUTO_INCREMENT,\r\n  `name` varchar(45) NOT NULL,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n\r\nINSERT INTO students SET name='Khanh';\r\nINSERT INTO students SET name='Thanh'<\/pre>\n<p>The withReuse() method makes it possible to reuse the container we created earlier.<\/p>\n<p>To run the container that we have declared above, you can call the start() method in the annotated method with the @BeforeAll annotation of JUnit 5 as follows:<\/p>\n<pre class=\"lang:java decode:true\">@BeforeAll\r\npublic static void init() throws SQLException {\r\n  container.start();\r\n\r\n  connection = DriverManager.getConnection(container.getJdbcUrl(),\r\n      \"root\", \"123456\");\r\n}<\/pre>\n<p>As you can see, I also initialized the Connection object from the information of the MySQL container that I ran.<\/p>\n<p>Now, you can write unit tests for the getAllStudents() method as follows:<\/p>\n<pre class=\"lang:java decode:true \">@Test\r\npublic void testGetAllStudents() throws SQLException {\r\n  List&lt;Student&gt; allStudents = studentService.getAllStudents(connection);\r\n  Assertions.assertTrue(allStudents.size() == 2);\r\n}<\/pre>\n<p>My entire unit test code for the StudentService class is as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.testcontainers;\r\n\r\nimport java.sql.Connection;\r\nimport java.sql.DriverManager;\r\nimport java.sql.SQLException;\r\nimport java.util.List;\r\nimport org.junit.jupiter.api.Assertions;\r\nimport org.junit.jupiter.api.BeforeAll;\r\nimport org.junit.jupiter.api.Test;\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.testcontainers.containers.MySQLContainer;\r\nimport org.testcontainers.junit.jupiter.Container;\r\nimport org.testcontainers.junit.jupiter.Testcontainers;\r\n\r\n@Testcontainers\r\npublic class StudentServiceTest {\r\n\r\n  private StudentService studentService = new StudentService();\r\n\r\n  private static Connection connection;\r\n\r\n  @Container\r\n  public static MySQLContainer&lt;?&gt; container = new MySQLContainer&lt;&gt;(\"mysql:latest\")\r\n      .withDatabaseName(\"example\")\r\n      .withPassword(\"123456\")\r\n      .withInitScript(\"db.sql\")\r\n      .withReuse(true);\r\n\r\n  @BeforeAll\r\n  public static void init() throws SQLException {\r\n    container.start();\r\n\r\n    connection = DriverManager.getConnection(container.getJdbcUrl(),\r\n        \"root\", \"123456\");\r\n  }\r\n\r\n  @Test\r\n  public void testGetAllStudents() throws SQLException {\r\n    List&lt;Student&gt; allStudents = studentService.getAllStudents(connection);\r\n    Assertions.assertTrue(allStudents.size() == 2);\r\n  }\r\n}\r\n<\/pre>\n<p>Result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-21593 aligncenter\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/introduction-about-testcontainers-4.png\" alt=\"\" width=\"700\" height=\"457\" \/><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-right kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;right&quot;,&quot;id&quot;:&quot;21588&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;4&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Introduction about Testcontainers&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\"><\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Testcontainers is a library that helps us write unit tests for applications and databases running on a Docker container. Testcontainers will help us run applications or databases using Docker Images, then we will use the plugins it supports to implement the unit test code we&hellip; <a href=\"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":21581,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2298],"tags":[],"class_list":["post-21588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-testcontainers-en","clearfix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Introduction about Testcontainers - Huong Dan Java<\/title>\n<meta name=\"description\" content=\"In this tutorial, I introduce with you all about Testcontainers and how to use Testcontainers for Java to write unit test.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction about Testcontainers - Huong Dan Java\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, I introduce with you all about Testcontainers and how to use Testcontainers for Java to write unit test.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html\" \/>\n<meta property=\"og:site_name\" content=\"Huong Dan Java\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/nhkhanh2406\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/nhkhanh2406\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-04T10:55:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-15T00:06:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/testcontainers.png\" \/>\n\t<meta property=\"og:image:width\" content=\"210\" \/>\n\t<meta property=\"og:image:height\" content=\"240\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Khanh Nguyen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/KhanhNguyenJ\" \/>\n<meta name=\"twitter:site\" content=\"@KhanhNguyenJ\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khanh Nguyen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html\"},\"author\":{\"name\":\"Khanh Nguyen\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"headline\":\"Introduction about Testcontainers\",\"datePublished\":\"2023-04-04T10:55:37+00:00\",\"dateModified\":\"2026-02-15T00:06:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html\"},\"wordCount\":614,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"image\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/testcontainers.png\",\"articleSection\":[\"Testcontainers\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html\",\"name\":\"Introduction about Testcontainers - Huong Dan Java\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/testcontainers.png\",\"datePublished\":\"2023-04-04T10:55:37+00:00\",\"dateModified\":\"2026-02-15T00:06:36+00:00\",\"description\":\"In this tutorial, I introduce with you all about Testcontainers and how to use Testcontainers for Java to write unit test.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html#primaryimage\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/testcontainers.png\",\"contentUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2023\\\/04\\\/testcontainers.png\",\"width\":210,\"height\":240},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/introduction-about-testcontainers.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/huongdanjava.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Introduction about Testcontainers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#website\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/\",\"name\":\"Huong Dan Java\",\"description\":\"Java development tutorials\",\"publisher\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/huongdanjava.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\",\"name\":\"Khanh Nguyen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"contentUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"width\":1267,\"height\":1517,\"caption\":\"Khanh Nguyen\"},\"logo\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\"},\"description\":\"I love Java and everything related to Java.\",\"sameAs\":[\"https:\\\/\\\/huongdanjava.com\",\"https:\\\/\\\/www.facebook.com\\\/nhkhanh2406\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/KhanhNguyenJ\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction about Testcontainers - Huong Dan Java","description":"In this tutorial, I introduce with you all about Testcontainers and how to use Testcontainers for Java to write unit test.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html","og_locale":"en_US","og_type":"article","og_title":"Introduction about Testcontainers - Huong Dan Java","og_description":"In this tutorial, I introduce with you all about Testcontainers and how to use Testcontainers for Java to write unit test.","og_url":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html","og_site_name":"Huong Dan Java","article_publisher":"https:\/\/www.facebook.com\/nhkhanh2406","article_author":"https:\/\/www.facebook.com\/nhkhanh2406","article_published_time":"2023-04-04T10:55:37+00:00","article_modified_time":"2026-02-15T00:06:36+00:00","og_image":[{"width":210,"height":240,"url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/testcontainers.png","type":"image\/png"}],"author":"Khanh Nguyen","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/KhanhNguyenJ","twitter_site":"@KhanhNguyenJ","twitter_misc":{"Written by":"Khanh Nguyen","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html#article","isPartOf":{"@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html"},"author":{"name":"Khanh Nguyen","@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"headline":"Introduction about Testcontainers","datePublished":"2023-04-04T10:55:37+00:00","dateModified":"2026-02-15T00:06:36+00:00","mainEntityOfPage":{"@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html"},"wordCount":614,"commentCount":0,"publisher":{"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"image":{"@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html#primaryimage"},"thumbnailUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/testcontainers.png","articleSection":["Testcontainers"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/huongdanjava.com\/introduction-about-testcontainers.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html","url":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html","name":"Introduction about Testcontainers - Huong Dan Java","isPartOf":{"@id":"https:\/\/huongdanjava.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html#primaryimage"},"image":{"@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html#primaryimage"},"thumbnailUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/testcontainers.png","datePublished":"2023-04-04T10:55:37+00:00","dateModified":"2026-02-15T00:06:36+00:00","description":"In this tutorial, I introduce with you all about Testcontainers and how to use Testcontainers for Java to write unit test.","breadcrumb":{"@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/huongdanjava.com\/introduction-about-testcontainers.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html#primaryimage","url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/testcontainers.png","contentUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/04\/testcontainers.png","width":210,"height":240},{"@type":"BreadcrumbList","@id":"https:\/\/huongdanjava.com\/introduction-about-testcontainers.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/huongdanjava.com\/"},{"@type":"ListItem","position":2,"name":"Introduction about Testcontainers"}]},{"@type":"WebSite","@id":"https:\/\/huongdanjava.com\/#website","url":"https:\/\/huongdanjava.com\/","name":"Huong Dan Java","description":"Java development tutorials","publisher":{"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/huongdanjava.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d","name":"Khanh Nguyen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","contentUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","width":1267,"height":1517,"caption":"Khanh Nguyen"},"logo":{"@id":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg"},"description":"I love Java and everything related to Java.","sameAs":["https:\/\/huongdanjava.com","https:\/\/www.facebook.com\/nhkhanh2406","https:\/\/x.com\/https:\/\/twitter.com\/KhanhNguyenJ"]}]}},"_links":{"self":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/21588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/comments?post=21588"}],"version-history":[{"count":6,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/21588\/revisions"}],"predecessor-version":[{"id":25062,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/21588\/revisions\/25062"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/media\/21581"}],"wp:attachment":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/media?parent=21588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/categories?post=21588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/tags?post=21588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}