Go to content
The Codest
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
2022-08-16
Software Development

Quick Guide on How to Run Containers from Tests

Bartlomiej Kuczyński

Quick Guide on How to Run Containers from Tests - Image

Learn how to run containers form tests in our Java related article where our senior java developer shows all the magic.

I use Spring Boot in the project to reduce boilerplate. Test containers are independent of Spring Framework and you can use them without that.

I use Testcontainers version 1.17.3, but feel free to use the newest one. Tests with Postgres container.

First define container:

public class Postgres13TC extends PostgreSQLContainer<Postgres13TC> {
  private static final Postgres13TC TC = new Postgres13TC();

  private Postgres13TC() {
    super("postgres:13.2");
  }

  public static Postgres13TC getInstance() {
    return TC;
  }

  @Override
  public void start() {
    super.start();
    System.setProperty("DB_URL", TC.getJdbcUrl());
    System.setProperty("DB_USERNAME", TC.getUsername());
    System.setProperty("DB_PASSWORD", TC.getPassword());
  }

  @Override
  public void stop() {
    // do nothing. This is shared instance. Let JVM handle this operation.
  }
}

Then initialize Spring application context. We get all data source configuration from container instance and set them as Spring configuration:

public class ContainerInit implements ApplicationContextInitializer<ConfigurableApplicationContext> {

  public static Postgres13TC postgres13TC;

  static {
    postgres13TC = Postgres13TC.getInstance();
    postgres13TC.start();
  }

  @Override
  public void initialize(ConfigurableApplicationContext applicationContext) {
    TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
        applicationContext,
        "spring.datasource.url=" + postgres13TC.getJdbcUrl(),
        "spring.datasource.username=" + postgres13TC.getUsername(),
        "spring.datasource.password=" + postgres13TC.getPassword(),
        "db.host=" + postgres13TC.getHost(),
        "db.port=" + postgres13TC.getMappedPort(postgres13TC.POSTGRESQL_PORT),
        "db.name=" + postgres13TC.getDatabaseName(),
        "db.username=" + postgres13TC.getUsername(),
        "db.password=" + postgres13TC.getPassword()
    );
  }
}

Finally, we can run our test, and everything will be started under the hood:

@SpringBootTest(webEnvironment = RANDOM_PORT)
@AutoConfigureTestDatabase(replace = NONE)
@ContextConfiguration(initializers = ContainerInit.class)
@Testcontainers
class DummyRepositoryTest {

  @Autowired
  private DummyRepository dummyRepository;

  @Test
  void shouldReturnDummy() {
    var byId = dummyRepository.getById(10L);
    var expected = new Dummy();
    expected.setId(10L);
    assertThat(byId).completes().emitsCount(1).emits(expected);
  }
}

Or if we want to run Spring independent test, we can use container directly:

@Testcontainers
class SimpleDbTest {

  @Container
  private static final Postgres13TC postgres13TC = Postgres13TC.getInstance();

  @Test
  void testConnection() {
    assumeThat(postgres13TC.isRunning());
    var connectionProps = new Properties();
    connectionProps.put("user", postgres13TC.getUsername());
    connectionProps.put("password", postgres13TC.getPassword());
    try (Connection connection = DriverManager.getConnection(postgres13TC.getJdbcUrl(),
        connectionProps)) {
    var resultSet = connection.prepareStatement("Select 1").executeQuery();
    resultSet.next();
    assertThat(resultSet.getInt(1)).isEqualTo(1);
    } catch (SQLException sqlException) {
    assertThat((Exception) sqlException).doesNotThrowAnyException();
    }
  }
}

Summing up

Testcontainers are very easy-to-use tools that help us to create integration tests that use Docker containers. That gives us more flexibility and increases development speed. Proper setup of test configuration reduces the time needed to board new developers. They don't need to set up all dependencies, just run the written tests with selected configuration files.

cooperation banner

Related articles

Technology news

7 Predictions for the IT Industry in 2023

What trends will dominate the IT industry in 2023? These include the outsourcing of IT professionals, the development of cloud-based tools and platforms, the growing popularity of solutions in the spirit of a composable approach...

Greg Polec
Software Development

9 Mistakes to Avoid While Programming in Java

What mistakes should be avoided while programming in Java? In the following piece we answers this question.

Rafal Sawicki
Startups

How Java Can Support Your Business?

Before we start, I would like to remind you about one important thing. Java is not only a programming language.

Bartlomiej Kuczynski
Startups

The Right Way to Find Top Java Developers

Finding the perfect Java developer can be a daunting task. As the market demand for such professionals grows at an astonishing pace, available sources for talent search can sometimes seem limited.

Grzegorz Rozmus

Subscribe to our knowledge base and stay up to date on the expertise from industry.

About us

Tech company specializing in scaling tech teams for clients and partners thanks to top-class development engineers.

    United Kingdom - Headquarters

  • Office 303B, 182-184 High Street North E6 2JA London, England

    Poland - Local Tech Hubs

  • Business Link High5ive, Pawia 9, 31-154 Kraków, Poland
  • Brain Embassy, Konstruktorska 11, 02-673 Warsaw, Poland
  • Aleja Grunwaldzka 472B, 80-309 Gdańsk, Poland

    The Codest

  • Home
  • About us
  • Services
  • Case studies
  • Know how
  • Careers

    Services

  • PHP development
  • Java development
  • Python development
  • Ruby on Rails development
  • React Developers
  • Vue Developers
  • TypeScript Developers
  • DevOps
  • QA Engineers

    Resources

  • What are top CTOs and CIOs Challenges? [2022 updated]
  • Facts and Myths about Cooperating with External Software Development Partner
  • From the USA to Europe: Why do American startups decide to relocate to Europe
  • Privacy policy
  • Website terms of use

Copyright © 2022 by The Codest. All rights reserved.

We use cookies on the site for marketing, analytical and statistical purposes. By continuing to use, without changing your privacy settings, our site, you consent to the storage of cookies in your browser. You can always change the cookie settings in your browser. You can find more information in our Privacy Policy.