Skip to content

Instantly share code, notes, and snippets.

@artpar
Last active November 10, 2023 06:28
Show Gist options
  • Select an option

  • Save artpar/b4ae240e4fb69554e3e602fd4d822b53 to your computer and use it in GitHub Desktop.

Select an option

Save artpar/b4ae240e4fb69554e3e602fd4d822b53 to your computer and use it in GitHub Desktop.
Unlogged test runner setup

Unlogged Test Runner

Make sure you are using unlogged-sdk version 0.1.15

<dependency>
    <groupId>video.bug</groupId>
    <artifactId>unlogged-sdk</artifactId>
    <version>0.1.15</version>
</dependency>
implementation group: 'video.bug', name: 'unlogged-sdk', version: '0.1.15'

Unit testing

unlogged-sdk test runner uses the JUnit Platform test runner to run tests created using the unlogged plugin. To execute unlogged tests as mvn test/gradle test/intellij run test.

Start by creating the following test class file UnloggedTest.java in your src/test/java directory

import io.unlogged.runner.UnloggedTestRunner;
import org.junit.runner.RunWith;

@RunWith(UnloggedTestRunner.class)
public class UnloggedRunnerWithoutSpringTest {

}

Yes this class has no methods as the tests will be based on src/test/resources/unlogged/<ClassName>.json files.

Now you can execute mvn test or gradle test to execute the tests from CLI.

Integration testing on Springboot application

For Springboot applications customize the above test as follows

@RunWith(UnloggedTestRunner.class)
@ComponentScan("<spring.application.package.name>")                  // update the package name here
@EnableAutoConfiguration
@PropertySource(
        value = {"config/application.yml", "config/application-dev.yml"},       // update the config files you want to use
        factory = UnloggedRunnerTest.YamlPropertySourceFactory.class)           // this is for supporting yml files
@EnableConfigurationProperties({ApplicationProperties.class})                   // specify your test application properties 
@TestConfiguration
public class UnloggedRunnerTest {

    public static class YamlPropertySourceFactory implements PropertySourceFactory {
        public YamlPropertySourceFactory() {
        }

        @Override
        public PropertiesPropertySource createPropertySource(String name, EncodedResource encodedResource) throws IOException {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(encodedResource.getResource());
            return new PropertiesPropertySource(encodedResource.getResource().getFilename(), factory.getObject());
        }
    }
}

With this, unlogged test runner will create as instance of the spring application context and execute the tests based on the beans created by spring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment