Project is Java and Maven.
I am required to integrate the selenium test which is under different project following multi module approach to integrate with single module spring project under one folder. Suggested information was to add the selenium test in src/test folder, but given this multi module looks difficult to follow that approach. Any best practices to follow ?
In pom there are scope field where you can mention test , this will make sure the dependencies are installed only for mvm test
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
test
This scope indicates that the dependency is not required for normal
use of the application, and is only available for the test compilation
and execution phases. This scope is not transitive. Typically this
scope is used for test libraries such as JUnit and Mockito. It is also
used for non-test libraries such as Apache Commons IO if those
libraries are used in unit tests (src/test/java) but not in the model
code (src/main/java).
https://www.baeldung.com/maven-dependency-scopes
3.4. Test
This scope is used to indicate that dependency isn't required at
standard runtime of the application, but is used only for test
purposes. Test dependencies aren't transitive and are only present for
test and execution classpaths.
The standard use case for this scope is adding test library like JUnit
to our application:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Related
I'm trying to create BDD tests for a spring boot application that is written in java11, and using junit5 for tests.
I'm trying version 6.9.1 of cucumber and my dependencies for this part are:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
Without a single example I could find in the cucumber documentation I tried fuse some snippets I found online that show this dependency list to be sufficient however, when I try their samples, there I can't find any of the following:
CucumberExtension - instead there is an annotation #Cucumber which seems to be doing that.
#CucumberOptions - there is an interface CucmberOptions which I can implement and return all required properties, but this feels like a step back from the parameterized annotation.
Can anyone provide a guide, documentation, or any other way to achieve my goal?
Thanks
It's worth reading the introduction to JUnit 5. You are making a common mistake in thinking that JUnit 5 is a monolith. Rather:
https://junit.org/junit5/docs/current/user-guide/#overview-what-is-junit-5
Unlike previous versions of JUnit, JUnit 5 is composed of several different modules from three different sub-projects.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
Like JUnit Jupiter, Cucumber is a test engine using the JUnit Platform. Extensions are a concept from JUnit Jupiter and don't carry over to Cucumber.
Without a single example I could find in the cucumber documentation.
You'll not find anything in the main documentation until the JUnit 5 integration is feature complete. Until that time it is perfectly possible to use Cucumbers JUnit 4 integration with JUnit Vintage.
However if you're more experimentally minded you can use the documentation with the source code:
https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine
#CucumberOptions - there is an interface CucmberOptions which I can implement and return all required properties, but this feels like a step back from the parameterized annotation.
The Cucumber JUnit Platform integration defaults to scanning for glue and features on the class path root. If you follow the conventional maven/gradle/java project layout no additional configuration is needed.
So step definitions go in src/test/java/com/example and features in src/test/resources/com/example.
If you have a more complicated setup you should either wait and use JUnit4/Junit Vintage or simplify your setup.
In my current project that runs on a JBoss server it is required to add all dependencies as providedif they are provided by JBoss. However for my unit-tests I do need those libraries without the JBoss server providing them (i.E. javaee-api), but I cannot add the same dependency twice with different scopes.
How can I add a dependency as provided for runtime, but as test for unit tests?
A provided dependency is available on the test classpath, so setting the scope to provided should be fine. See also the definition of provided:
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
fair warning: I am new to Spring and maven.
The project:
The client has provided us with a web project that uses jsp to render front ends and uses spring-context only to manage some of their classes as Spring Beans. This project uses xml configuration. Here is the maven section that includes the spring-context:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
Our job is to create an api layer for this project to open up access to the project's resources. The plan is to create a modern Spring Boot api using the spring-boot-web starter.
The problem:
The jsp project is split up into sub-modules. We are trying to take their business logic submodule as a maven dependency in our api project (it defines it's own spring context). The first problem that we have faced occurs when we simply add the module from the jsp project as a dependency in our api project. The spring boot initialization process provides no log feedback and hangs but does not exit (It runs fine when the dependency is not included). When crawling through with a debugger I have found that it is loading configuration files that are in the business module. My thinking is that one of the api's configuration files are getting overwritten which is breaking spring's logging and other behaviors.
The other more general problem is that I don't know if I can use the beans provided in the business logic module in my modern version of spring.
So the question is:
Is there a proper way to use beans defined in a dependent spring project?
I want to write unit tests for my Resource classes in my Restful Web application.
I am using jersey version 1.17 and using maven 3.0.2. junit 4.8.1.
(Just a background, my web application will be deployed on a Jboss server packaged with other components as an ear.)
Now, I read the Jersey Test framework wiki, but I am unable to find the right set of dependencies to get it working.
There are many versions and artifact and groupIds that are confusing.
Some blogs say
<groupId>com.sun.jersey.test.framework</groupId>
Whereas others say,
<groupId>com.sun.jersey.jersey-test-framework</groupId>
while few say, its changed to
<groupId>com.sun.jersey</groupId>
I am totally confused.
Please help me figure out the right set dependencies required to write a unit test for Resource Classes. I want to use grizzly container. Is it possible?
Thanks in advance.
The Jersey docs for 1.18 say:
Maven developers require a dependency on the
jersey-test-framework-grizzly module. The following dependency needs
to be added to the pom:
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly</artifactId>
<version>1.18</version>
<scope>test</scope>
</dependency>
which looks like it brings in all the dependencies you'll need. EG jersey-test-framework-core
In pom.xml , we will provide compiletime and runtime as scope in the dependency ? whats the significance of that ? Please provide some applicable example for understanding this.
The following is taken from the maven documentation
compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
runtime
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
So for example if we have the following two dependencies in our POM:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1.3</version>
<scope>compile</scope> <!-- can be ommitted as it is the default -->
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
<scope>runtime</scope>
</dependency>
Then the classes from commons-logging-api would be on the classpath during compilation of my module, whereas classes from commons-logging would not be available - if by accident I had a direct reference to a class from commons-logging in one of my project's classes then the build would fail.
However during runtime or test compilation & execution the classes from commons-logging would be on the classpath so could be used (i.e. by classes from commons-logging-api, or directly in tests of the project).
Both compile and runtime dependencies are included transitively (under the same scope) by Maven when your project is referenced as a dependency in another project.
p.s. As mentioned by kostja there is also the provided scope
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
Basically the difference between provided and compile is that provided dependencies are not transitive.
Imagine you are deploying your application to a Java EE compliant server. The server provides all libraries implementing the Java EE standard, so you don't need to deploy them with your application.
During development, you will need the Java EE libraries with the compile time scope, since you need to compile the classes.
During the runtime however the dependencies are provided by the application server. Maven uses the 'provided' scope for such cases.