Search code examples
javaspring-bootmavenjunit

How to solve Maven clean install errors?


The problem is the tests are running without any problem when only the tests are run. However, when I execute Maven clean install then the tests are failing and the below stack trace is generated. I have tried a few of the solutions in the stackoverflow especially related to the multiple SLF4J bindings but could not completely run it error free.

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running BatchTest
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Ablage/DEV/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Ablage/DEV/.m2/repository/org/slf4j/slf4j-reload4j/1.7.36/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
10:12:50.390 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
10:12:50.456 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
10:12:50.609 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [BatchTest] from class [org.springframework.test.context.support.DefaultTestContextBootstrapper]
10:12:50.761 [main] DEBUG org.springframework.test.context.support.AbstractDelegatingSmartContextLoader - Delegating to AnnotationConfigContextLoader to process context configuration [ContextConfigurationAttributes@703fa45 declaringClass = 'BatchTest', classes = '{class xx.xx.xx.xxx.xx.xxxConfiguration, class xx.xxx.xxx.abcApp.steps.xxxConfig, class xx.xx.xx.xxx.xx.xxxConfig, class xx.xxx.xx.xxx.steps.xxxConfig, class xx.xx.xx.xxx.steps.xxConfig, class TestConfig}', locations = '{}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.ContextLoader'].
10:12:51.106 [main] DEBUG org.springframework.test.context.support.DefaultTestContextBootstrapper - Merging default listeners with listeners configured via @TestExecutionListeners for class [BatchTest].
10:12:51.106 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
10:12:51.141 [main] DEBUG org.springframework.test.context.support.DefaultTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/web/context/request/RequestAttributes]
10:12:51.199 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@69ce14e6, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@699d96bc, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@b5de9ac, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4ecd8ab1, org.springframework.test.context.support.DirtiesContextTestExecutionListener@57bdceaa, org.springframework.test.context.transaction.TransactionalTestExecutionListener@52909a97, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@36e409e0, org.springframework.test.context.event.EventPublishingTestExecutionListener@145e958f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@2d2fc130, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@7bbdb220, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@6d001fbe, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@d120d7c, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@5ee77baf, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@485deee1, org.springframework.batch.test.StepScopeTestExecutionListener@25cde5bb, org.springframework.batch.test.JobScopeTestExecutionListener@38a96593]
10:12:51.208 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@3dfe59d7 testClass = BatchTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1be8c122 testClass = BatchTest, locations = '{}', classes = '{class xx.xxx.xx.xxxx.xx.xxxConfiguration, class xx.xxx.xxx.xxx.xxx.xxxConfig, class xx.xxx.xx.xxx.xx.xxxConfig, class xxConfig.class}', contextInitializerClasses = '[]', activeProfiles = '{localtest}', propertySourceLocations = '{classpath:/src/test/resources/application-localtest.properties}', propertySourceProperties = '{}', contextCustomizers = set[[ImportsContextCustomizer@5d08976a key = [@org.springframework.batch.test.context.SpringBatchTest(), @org.springframework.test.context.junit.jupiter.SpringJUnitConfig(inheritInitializers=true, initializers={}, classes={}, name="", locations={}, value={xx.xxx.xx.xxx.xxx.xxxConfiguration.class}, inheritLocations=true), @org.springframework.test.context.TestExecutionListeners(inheritListeners=true, mergeMode=MERGE_WITH_DEFAULTS, listeners={org.springframework.batch.test.StepScopeTestExecutionListener.class, org.springframework.batch.test.JobScopeTestExecutionListener.class}, value={}), @org.springframework.test.context.ContextConfiguration(inheritInitializers=true, initializers={}, loader=org.springframework.test.context.ContextLoader.class, classes={}, name="", locations={}, value={}, inheritLocations=true), @org.springframework.test.context.TestPropertySource(locations={"/src/test/resources/application-localtest.properties"}, inheritProperties=true, value={}, properties={}, inheritLocations=true), @org.springframework.data.jpa.repository.config.EnableJpaRepositories(excludeFilters={}, enableDefaultTransactions=true, repositoryBaseClass=org.springframework.data.repository.config.DefaultRepositoryBaseClass.class, basePackageClasses={}, escapeCharacter='\', includeFilters={}, bootstrapMode=DEFAULT, repositoryFactoryBeanClass=org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.class, entityManagerFactoryRef="entityManagerFactory", queryLookupStrategy=CREATE_IF_NOT_FOUND, namedQueriesLocation="", transactionManagerRef="transactionManager", basePackages={}, repositoryImplementationPostfix="Impl", considerNestedRepositories=false, value={"de.xxx.xxx.xxx.db"}), @org.springframework.test.context.ActiveProfiles(profiles={}, resolver=org.springframework.test.context.ActiveProfilesResolver.class, value={"localtest"}, inheritProfiles=true), @org.springframework.context.annotation.Import(value={org.springframework.data.jpa.repository.config.JpaRepositoriesRegistrar.class})]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@70091872, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2b441e56, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@c60ccbaf, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6a7a1a0d, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@766b6d02, org.springframework.batch.test.context.BatchTestContextCustomizer@5e98032e], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null].
10:12:51.261 [main] DEBUG org.springframework.test.context.support.AbstractDelegatingSmartContextLoader - Delegating to AnnotationConfigContextLoader to load context from [MergedContextConfiguration@1be8c122 testClass = BatchTest, locations = '{}', classes = '{class xxx.xxxConfiguration, class xx.xx.xxx.xxx.xxx.Config}', contextInitializerClasses = '[]', activeProfiles = '{localtest}', propertySourceLocations = '{classpath:/src/test/resources/application-localtest.properties}', propertySourceProperties = '{}', contextCustomizers = set[[ImportsContextCustomizer@5d08976a key = [@org.springframework.batch.test.context.SpringBatchTest(), @org.springframework.test.context.junit.jupiter.SpringJUnitConfig(inheritInitializers=true, initializers={}, classes={}, name="", locations={}, value={xx.xxx.xx.xxx.xxx.Configuration.class}, inheritLocations=true), @org.springframework.test.context.TestExecutionListeners(inheritListeners=true, mergeMode=MERGE_WITH_DEFAULTS, listeners={org.springframework.batch.test.StepScopeTestExecutionListener.class, org.springframework.batch.test.JobScopeTestExecutionListener.class}, value={}), @org.springframework.test.context.ContextConfiguration(inheritInitializers=true, initializers={}, loader=org.springframework.test.context.ContextLoader.class, classes={}, name="", locations={}, value={}, inheritLocations=true), @org.springframework.test.context.TestPropertySource(locations={"/src/test/resources/application-localtest.properties"}, inheritProperties=true, value={}, properties={}, inheritLocations=true)

My pom.xml looks like the following -

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>xx.xxx</groupId>
        <artifactId>parent-pom</artifactId>
        <version>1.1</version>
    </parent>
    <groupId>xx.xxx.Appxx.xxxx</groupId>
    <artifactId>xxxx</artifactId>
    <version>2.0.2-SNAPSHOT</version>
    <name>AbcApplication</name>
    <packaging>jar</packaging>
    <modelVersion>4.0.0</modelVersion>
    <properties>

        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <version.maven.compiler>3.8.1</version.maven.compiler>
        <version.maven-assembly-plugin>3.3.0</version.maven-assembly-plugin>
        <version.spring-boot-maven-plugin>2.6.4</version.spring-boot-maven-plugin>
        <version.maven-jar-plugin>3.2.2</version.maven-jar-plugin>

        <spring-boot-dependencies.version>2.7.9</spring-boot-dependencies.version>
        <db2jcc4.version>4.26.14</db2jcc4.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hibernate.core.version>5.6.7.Final</hibernate.core.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-settings</artifactId>
                <version>3.8.7</version>
            </dependency>
            <dependency>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-core</artifactId>
                <version>3.8.7</version>
            </dependency>
            <dependency>
                <groupId>org.yaml</groupId>
                <artifactId>snakeyaml</artifactId>
                <version>2.0</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-utils</artifactId>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.maven.shared</groupId>
                <artifactId>maven-shared-utils</artifactId>
                <version>3.3.3</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>5.3.26</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.15.0-rc1</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20230227</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-core</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>snakeyaml</artifactId>
                    <groupId>org.yaml</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-core</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.core.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.ibm.db2.jdbc.type4</groupId>
            <artifactId>db2jcc4</artifactId>
            <version>${db2jcc4.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ibm.db2.jdbc.type4</groupId>
            <artifactId>db2jcc_license_cisuz</artifactId>
            <version>${db2jcc4.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>
    <!-- build plugins -->


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.maven.compiler}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${version.maven-assembly-plugin}</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-dist</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <!-- Configures the used assembly descriptor -->
                            <descriptors>
                                <descriptor>xxxx/xxx.xml</descriptor>
                            </descriptors>
                            <finalName>distribution</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <!-- Configures the created archive -->
                    <archive>
                        <!-- Configures the content of the created manifest -->
                        <manifest>
                            <!-- Adds the classpath to the created manifest -->
                            <addClasspath>true</addClasspath>
                            <!-- Workaround for Maven bug #MJAR-15 -->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!-- Specifies that all dependencies of our application are found from
                                the lib directory. -->
                            <classpathPrefix>../lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.8</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <directory>P:/JavaBatch/properties</directory>
            </testResource>
        </testResources>
    </build>
</project>


The eror that I am getting is -

java.lang.ExceptionInInitializerError Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j


Solution

  • Class path contains multiple SLF4J bindings. StaticLoggerBinder this is the root cause of the problem, to solve it you should to choice what library do you need to use for SLF4J logback-classic or slf4j-reload4j:

    To set it can help you:

    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId> or ch.qos.logback
            <artifactId>slf4j-reload4j</artifactId> or logback-classic
        </exclusion>
    </exclusions> 
    

    UPDATE

    On spring-boot-started dependency exclude spring-boot-starter-logging, something like this.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>