Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test resources not found because in wrong class folder #120

Open
pitschr opened this issue May 10, 2019 · 1 comment
Open

Test resources not found because in wrong class folder #120

pitschr opened this issue May 10, 2019 · 1 comment

Comments

@pitschr
Copy link

pitschr commented May 10, 2019

Expected vs Actual Behaviour

Hello.

I am facing the similiar issue in #4 but it is a single project (not a multi-project) and I am in a different path. The test resouces are subject to be called using WithMockedApplication.class.getResource(".... file name ...");

When running through SBT terminal it is failing to find the resources because it is in the path:
target/scala-2.12/jacoco/instrumented-classes

while it is expected to be in the usual test-classes path which contains class files and the test resources as well:
target/scala-2.12/test-classes

Actual workaround is to detect if test is running in SBT and then using a hardcoded path to obtain the resources from the test-classes folder. I would like to get rid off this hardcoded logic.

Any idea how to resolve it and how to tell SBT to use the correct path instead of the JaCoCo path?

Thank you, Christoph

Steps to Reproduce

> sbt clean test

Environment

  • Play Framework: 2.7
  • SBT version: 1.2.7
  • Plugin version: 3.1.0
  • Scala version(s): 2.11.12", "2.12.7
  • Java version: 8
@pitschr
Copy link
Author

pitschr commented Apr 6, 2021

In case someone is interested how I resolved it. I am fetching the correct path using this method from an utility class called TestResources .

    @Nonnull
    public static Path asPath(final @Nonnull String path) {
        Preconditions.checkNotNull(path);

        try {
            final String currentPath = TestResources.class.getResource("/").toURI().toString();
            final String correctPath;
            if (currentPath.contains("/jacoco/")) {
                // dirty workaround for SBT because it is in wrong path
                correctPath = StringUtils.substringBefore(currentPath, "/jacoco/") + "/test-classes/";
                log.debug("Path changed from '{}' to '{}'", currentPath, correctPath);
            } else {
                // for the rest it is same
                correctPath = currentPath;
            }

            log.debug("Look up for test resource '{}' in path: {}", path, correctPath);
            return Paths.get(new URI(correctPath + path));
        } catch (final URISyntaxException e) {
            throw new AssertionError(String.format("Could not get resource for path: %s", path), e);
        }
    }

Have not found a cleaner solution yet, but the method above seems be reliable more than 1 year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant