Search code examples
javadockertestingjunittestcontainers

Using Testcontainers without Spring


I started to understand Docker and Testcontainers for homework, where the use of Spring is forbidden. The project uses a database - there are different interactions with it. I have deployed the database in Docker, but I don't understand how to write tests through Testcontainers. For example, how to test adding data to the database, or changing data in the database, or just output from the database? I have connected the container in the test class via:

 private static final PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres:15.2");

But what to do next? All the examples, youtube videos, etc use Spring... Thanks in advance!

Below I present examples of functions

Add to DB function

    @Override
    public void addTransaction(TypeOfTransaction type, int amount) {
        String sql = """
                INSERT INTO entities."Transaction" (type, amount)
                VALUES (?, ?)
                """;
        try (Connection connection = ConnectionManager.open();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, String.valueOf(type));
            preparedStatement.setInt(2, amount);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

Update DB function

    @Override
    public void minusBalance(String login, int amount) {
        String sql = """
                UPDATE entities."Client"
                SET balance = balance - ?
                WHERE login = ?
                """;
        try (Connection connection = ConnectionManager.open();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, amount);
            preparedStatement.setString(2, login);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

Print data from DB function

    @Override
    public void printAudit(String login) {
        String sql = """
                SELECT action
                FROM "Client_Audit"
                WHERE client_login = ?
                """;
        try (Connection connection = ConnectionManager.open();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, login);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getString("action"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

I tried to find an example of using testcontainers without Spring on YouTube, in various articles, but no luck :(


Solution

  • I think you should read first this article in order to understand what Testcontainers does. Then, look at other article which use PostgreSQLContainer without Spring.

    In summary, Testcontainers will create and run the container in a random port, when using PostgreSQLContainer you can get the JDBC URL with getJdbcUrl() that should be used in DriverManager.getConnection(postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword());