Skip to content

[FEATURE] Refactor ExtensionsRunner test code out of production source tree #171

@dbwiddis

Description

@dbwiddis

Is your feature request related to a problem?

We shouldn't have exclusively test-related code in the main source tree, e.g.,

/**
* Instantiates a new Extensions Runner using test settings.
*
* @throws IOException if the runner failed to read settings or API.
*/
public ExtensionsRunner() throws IOException {
ExtensionSettings extensionSettings = readExtensionSettings();

which calls here, only by tests:

private static ExtensionSettings readExtensionSettings() throws IOException {
File file = new File(ExtensionSettings.EXTENSION_DESCRIPTOR);
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
return objectMapper.readValue(file, ExtensionSettings.class);
}

which uses this constant pointing to the test source tree:

/**
* Path to extension.yml file for testing. Internal use only.
*/
static final String EXTENSION_DESCRIPTOR = "src/test/resources/extension.yml";

What solution would you like?

  1. Remove the no-arg constructor above from ExtensionsRunner
  2. Make the constructor with an Extension argument either package-private (probably) or protected (maybe) so it's accessible by tests
  3. Create a new subclass of ExtensionsRunner in the src/test/java tree to be used as an ExtensionsRunner object when needed for tests, such as the linked comment in the context section below. Move the above code snippets there with modifications as noted below.
  4. Update all tests which use the existing no-arg constructor to use the new class.

One would think TestExtensionsRunner would be a good name but it's already in use for our test class. Picking a new name will probably be the hardest part. It can extend ExtensionsRunner like so:

public class ExtensionsRunnerForTesting extends ExtensionsRunner {

    public ExtensionsRunnerForTesting() throws IOException {
        super(new Extension () {

            @Override
            public ExtensionSettings getExtensionSettings() {
                // Move the result of current readExtensionSettings() method of ExtensionsRunner here
                // except get rid of yaml file reading, and just instantiate the object with the values in the yaml
                return new ExtensionSettings( ... );
            }

            @Override
            public List<ExtensionRestHandler> getExtensionRestHandlers() {
                return Collections.emptyList();
            }
        });
    }
}

What alternatives have you considered?

We could use the "sample" extensions for this purpose, or even create a test extension in the sample branch. This seems reasonable for an IT-based approach, but I think this approach is better for unit testing requiring an object with minimal settings.

Do you have any additional context?

I wonder if there's value in creating a "Test" version of the ExtensionsRunner. The no-arg constructor here is used exclusively for testing and I'd like to see it removed and replaced with some TestExtensionsRunner class in the test suite.

Originally posted by @dbwiddis in #170 (comment)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestgood first issueGood for newcomershacktoberfestGlobal event that encourages people to contribute to open-source.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions