Skip to content

Maven Surefire Plugin isn't run, breaking tests #131

Closed

Description

@parkertomatoes

Tests that pass via "mvn test" fail when run from VS Code, seemingly because it isn't executing the Maven Surefire plugin to remove some run-time dependencies from the test classpath.

Background:
The SLF4J folks officially recommend using this testing SLF4J implementation for unit tests:
http://projects.lidalia.org.uk/slf4j-test/
Because SLF4J doesn't like multiple implementations, the test library asks you to use the Maven Surefire plugin to remove logback (or whatever SLF4J implementation) from the test classpath (see link).

This works fine when I run tests from the Maven CLI ("mvn test"), but I get a "Class path contains multiple SLF4J bindings" error when trying to run the tests from VS Code.

Activity

  1. ansyral commented on Mar 2, 2018

    @ansyral
    Collaborator

    parkertomatoes it is "mvn test" that executes the maven plugin to remove logback from the test classpath. If I understand it correctly, you want to the ability to configure some preprocess action before runner running the tests? Currently we don't support this.

  2. IzakMarais commented on Mar 9, 2018

    @IzakMarais

    I also have some similar problems. We use protocolbuffer definitions that generate java source files that are included in the tests. They are not picked up when using this plugin.

    Source files are generated like so in the pom:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
            <execution>
                <id>unpack-proto-dependencies</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>unpack-dependencies</goal>
                </goals>
                <configuration>
                    <includeArtifactIds>artifact-name-here</includeArtifactIds>
                    <excludeTransitive>true</excludeTransitive>
                    <overWrite>true</overWrite>
                    <outputDirectory>${project.build.directory}/generated-sources/proto</outputDirectory>                            
                    <overWriteReleases>true</overWriteReleases>
                    <overWriteSnapshots>true</overWriteSnapshots>
                </configuration>
            </execution>
        </executions>
    </plugin>

    we event try to add it to the test class path like so (this is not needed for mvn test to work).

    <plugin>
        <!-- a hint for IDE's to add the java sources to the classpath -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
            <execution>
                <id>01-add-test-sources</id>
                <phase>generate-test-sources</phase>
                <goals>
                    <goal>add-test-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>${project.build.directory}/generated-sources/protobuf/java</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>    
  3. ansyral commented on Mar 22, 2018

    @ansyral
    Collaborator

    parkertomatoes Izak Marais (@IzakMarais) we'll support test configuration this release, where you can configure prelanchTask to execute maven goals, what do you think about this?

  4. added this to the 0.5.0 milestone on Mar 27, 2018
  5. ansyral commented on Apr 3, 2018

    @ansyral
    Collaborator

    parkertomatoes Izak Marais (@IzakMarais) we've supported test configuration this release and you can configure the preLaunchTask to add maven goal script

  6. dannormond commented on Apr 11, 2018

    @dannormond

    I'm not sure the preLaunchTask is what is wanted for this. I've put in a preLaunchTask to call 'mvn test' but that just runs all my tests through maven before attempting to run one test through Code.

  7. ansyral commented on Apr 13, 2018

    @ansyral
    Collaborator

    Dann Ormond (@dannormond) does your pom.xml contain customized goal execution in or before "test" phase? If so, you could add script like mvn groupID:artifactID:version:goal in the preLaunchTask to achieve the same effect with maven test.

  8. goldmann commented on Aug 4, 2023

    @goldmann

    Can we consider reopening this? What do we see here is the same as in #1436, but for Maven.

    The original request was about being able to exclude some depednencies from the test classpath. In case of Maven it is done like this:

    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>${surefire-plugin.version}</version>
      <configuration>
        <classpathDependencyExcludes>
          <classpathDependencyExclude>org.jboss.slf4j:slf4j-jboss-logmanager</classpathDependencyExclude>
        </classpathDependencyExcludes>
      </configuration>
    </plugin>

    Running tests via Maven CLI works perfectly fine, but when executed in vscode, these fail with messages similar to this:

    SLF4J: Class path contains multiple SLF4J providers.
    SLF4J: Found provider [org.slf4j.impl.JBossSlf4jServiceProvider@696da30b]
    SLF4J: Found provider [ch.qos.logback.classic.spi.LogbackServiceProvider@4e7912d8]
    SLF4J: See https://www.slf4j.org/codes.html#multiple_bindings for an explanation.
    SLF4J: Actual provider is of type [org.slf4j.impl.JBossSlf4jServiceProvider@696da30b]
    

    I don't see currently a way to do this with preLaunchTask.

  9. clement-socgen commented on Feb 17, 2026

    @clement-socgen

    Just as a quick fix for this particular issue regarding slf4j (so not a global fix for the surefire exclusion option), the config can be set to select a specific provider:

      "java.test.config": [
        {
          "workingDirectory": "${workspaceFolder}",
          "vmArgs": [
            "-Dslf4j.provider=ch.qos.logback.classic.spi.LogbackServiceProvider" // Replace with the appropriate provider classpath
          ]
        }
      ]

    In case people stumble on this issue like I did, might save some time :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions