Skip to content

Commit

Permalink
Run all integration tests using a MySQL test container instead of HSQL
Browse files Browse the repository at this point in the history
Replace HSQL with MySQL for higher fidelity integration tests and to
be able to also test native queries and platform-specific functionality.
  • Loading branch information
garionpin committed Jun 30, 2022
1 parent 6ef8f44 commit 7e7f5bc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cli/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
info.build.version=@project.version@

spring.flyway.enabled=true
spring.datasource.url=jdbc:tc:mysql:5.7://localhost:3306/mojito?characterEncoding=UTF-8&useUnicode=true&TC_INITSCRIPT=db/mysql/test_init.sql

l10n.console-writer.ansi-code-enabled=false
l10n.console-writer.output-type=ANSI_LOGGER
15 changes: 14 additions & 1 deletion docs/_docs/guides/open-source-contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,20 @@ For example to create Demo data, you can now run: `mojito demo-create -n DemoCLI
Alternatively, install the CLI using the [install scripts]({{ site.url }}/docs/guides/install/#cli-install-script).

## Run Unit Tests



### Testcontainers
By default, if no datasource is configured in the application.properties file, integration tests rely on
[Testcontainers](https://www.testcontainers.org/)
to
provide a configured
MySql instance in a test-scoped container.

For that to work, you will need Docker installed and running on the local machine.

See [general Docker requirements](https://www.testcontainers.org/supported_docker_environment/) for Testcontainers.

### Test command
```sh
cd ${PROJECT_DIR}
mvn test
Expand Down
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.17.1</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.17.1</version>
</dependency>

<!-- TODO(spring2) remove -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
1 change: 0 additions & 1 deletion webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
1 change: 1 addition & 0 deletions webapp/src/main/resources/db/mysql/test_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER DATABASE `mojito` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.box.l10n.mojito.db;

import com.box.l10n.mojito.aws.s3.AmazonS3Configuration;
import com.box.l10n.mojito.aws.s3.AmazonS3ConfigurationProperties;
import com.box.l10n.mojito.service.DBUtils;
import com.box.l10n.mojito.service.blobstorage.s3.S3BlobStorageConfigurationProperties;
import com.box.l10n.mojito.service.blobstorage.s3.S3BlobStorageTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {DBUtils.class})
public class MySQLIntegrationTest {
@Autowired
DBUtils dbUtils;

@Test
public void testIntegrationTestsRunOnMysql() {
Assert.assertTrue(dbUtils.isMysql());
}
}
3 changes: 3 additions & 0 deletions webapp/src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
spring.profiles.include=disablescheduling

spring.flyway.enabled=true
spring.datasource.url=jdbc:tc:mysql:5.7://localhost:3306/mojito?characterEncoding=UTF-8&useUnicode=true&TC_INITSCRIPT=db/mysql/test_init.sql

l10n.boxclient.useConfigsFromProperties=true

# we use LDAP to test the user creation from UserDetailsContextMapperImpl flow
Expand Down

0 comments on commit 7e7f5bc

Please sign in to comment.