spring boot starter test 1.4.1 - use assertj-core 3.5.x - maven

I want to use the latest assertj-core for Java8 (for example to assert on optionals).
I use spring-boot-starter-test 1.4.1 it comes with assertj 2.5.0 preconfigured.
I cannot use a spring boot parent pom.
How can I set up my maven project, so I exclude or overwrite version 2.5.0 with 3.5.2? I tried
setting just the property assertj.version
adding exclusions on the starter-test dependency
adding exclusion on the spring-boot-dependencies
Update:
I am setting up a custom "test" module for all modules in my multi module project. I do not just need spring-test-starter but also some other dependencies and some test classes and rules.
This is what my project looks like:
my-module-root
|-my-module-a (using test)
|-my-module-b (using test)
\-test (including starter-test, ... - in COMPILE scope (because this is a test library))
Now, when I exclude assertj-core on the test module (and include 3.5.2), the dependencies for the test module are ok.
But when I check the dependencies on the root level, I have both, assertj-core-2.5.0 and assertj-core-3.5.2 on the classpath.

You can exclude it from spring-boot-starter-test and then add manually different version:
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
To verify which version is included:
$ mvn dependency:tree | grep assertj
[INFO] \- org.assertj:assertj-core:jar:3.5.2:test

Related

Why do test scope dependencies pull compile scope dependencies in Maven?

Currently my project uses spring boot starter test as so:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.8.RELEASE</version>
<scope>test</scope>
</dependency>
However, despite the test scope, it pulls in spring-core (which is a vulnerable tpl in this version) as a compile scope transitive dependency and it appears inside my compiled binary.
I'm aware that I can fix this by pulling spring-core explicitly with test scope:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.12.RELEASE</version>
<scope>test</scope>
</dependency>
However this shouldn't be necessary. Why is a dependency that's only available in tests pulling dependencies into the compile scope?
I double checked after the comment from J Fabian Meyer. While spring core was appearing under spring-boot-starter-test in the dependency tree, it was being pulled into the compile scope by spring-boot-starter-web.
My guess is spring-boot-starter-test pulls a later version of spring-core which is why it appeared in the tree as so

WebDriverManager The import io.github cannot be resolved

I added the below WebDriverManager maven dependency in pom.xml
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
In my java class I am unable to import io.github.bonigarcia.wdm.WebDriverManager; automatically. If manually write the import, I get error at io.github which says: The import io.github cannot be resolved.
What is the issue here? I tried clean, restart and different versions of webdrivermanager in pom.xml.
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
The dependency you used is reduced to <scope>test</scope> but what that actually means?
It indicates that the dependency is NOT required for the compilation but only for execution.
It appears during the runtime and test but not during compilation.
The default scope is compile. Compile dependencies are available in all classpaths of the project.
EDIT:
<scope>test</scope> makes the dependency available for execution but not for compilation. What does it mean?
It means that the classpath is available for src/test folder in your project.
Default scope makes classpath available for src/main AND src/test. So if you make any classes manage WebDriver and you put them under source folder, you should use a scope which allows the dependency to be available at compilation time.
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.2.2</version>
<scope>compile</scope>
</dependency>
In the scope replace with compile instead of test, it will import.*
You also can not specify the scope, it will work too :
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.3</version>
</dependency>
Windows 10
Eclipse IDE for Enterprise Java Developers - 2020-12
Java JDK 15.0.2
Maven 3.6.3

In Maven, what is the difference between `package:artifact:jar:version` and package:artifact:jar:tests:version`?

Using Maven 3.0.5
I'm trying to get spark-testing-base from com.holdenkarau to work with Hadoop 3.1. holdenkarau's dependency tree includes Hadoop 2.8.3; which is why I think I'm getting errors.
From my mvn dependency:tree I see the following lines:
[INFO] +- org.apache.hadoop:hadoop-common:jar:3.1.0:provided
...
[INFO] | +- org.apache.hadoop:hadoop-common:jar:tests:2.8.3:test
These lines come from these two lines in the pom.xml file:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.holdenkarau</groupId>
<artifactId>spark-testing-base_${scala.compat.version}</artifactId>
<version>${spark.version}_0.12.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
</exclusion>
</exclusions>
</dependency>
I basically have two related questions:
What is the difference between org.apache.hadoop:hadoop-common:jar:3.1.0 and org.apache.hadoop:hadoop-common:jar:tests:2.8.3. What is that extra tests in there for; where does it come from and what does it mean?
If I have a dependency that uses an older version of a package in the test scope, how do I force it to use a newer version; i.e., how do I force spark-testing-base to use Hadoop 3.1 in the test scope.
tests is called a classifier, and it contains code that's really only useful in the context of actually testing, such as an embedded HDFS system
You could explicitly try pulling in a new version like so, assuming it exists
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.0</version>
<scope>test</scope>
<classifier>test</classifier>
</dependency>
You may also want to exclude the same within the other dependency, however you might then run into build issues since that library is only written to test against 2.8.3

Excluding transitive dependency from Maven pom

I have a Maven dependency added where type is test-jar and scope is test. I want to remove a transitive dependency from this (because in the Nexus prod repo this dependency is missing which leads to Jenkins build failure). I have added a exclusion tag but still the dependency is not removed i.e. Jenkins build still fails and complains about this dependency. Any clue why?
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxx</artifactId>
<type>test-jar</type>
<version>${xxx.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>jamon</artifactId>
<groupId>com.jamonapi</groupId>
<!-- <scope>compile</scope> -->
</exclusion>
</exclusions>
</dependency>
You can use the following command to find out the transitive dependency details and then you can exclude the dependency:
mvn dependency:tree -Dverbose -Dincludes=<artifact_name>

why is the scope of RestEasy compile in the pom.xml when the container (JBOSS) provides it?

Here is the relevant portion of pom.xml
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-all-5.0</artifactId>
<scope>provided</scope>
</dependency>
Why is the scope of resteasy compile (which is default, when none is provided) but that of javax.servlet is provided. I am deploying this on Jboss which ships with resteasy. so shouldn't the scope of resteasy be provided as well?
and btw, I do not see any version mentioned. so what is the default version that gets picked up?
If you are using jboss 7, resteasy-jackson-provider is included, so it would be correct to use a provided scope.
I guess default version is being picked up from a bom declared in the dependencyManagement section of your pom, could that be right?
For older jboss versions, resteasy is not included, so you will have to add the jars to your WEB-INF/lib directory.
Necessary jars can be obtained using maven (compile scope) or check out this link http://www.mastertheboss.com/jboss-frameworks/resteasy/resteasy-tutorial
The RESTEasy API and runtime is provided by newer versions of JBoss. Usually you import a Java EE-spec pom in the dependencyManagaement section and add the needed APIs in the dependency section, e.g for JBoss AS7:
<dependencyManagement>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>3.0.2.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
The runtime will use the JSON-Provider which is found on the classpath. So it makes sense to add them with scope compile to your project. If you want to use Jettison you'd add following to your pom:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
</dependency>
If you don't add one your application server may provide a default one. JBoss AS7 / Wildfly for instance will use resteasy-jackson-provider if you don't add a provider to the classpath.
JBoss 5 does not provide the JAX-RS libs as far as I know so there it makes sense to add the resteasy-jackson-provider with scope compile.

Resources