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.
Related
When I try to run a Spring Boot JavaFX project with
mvn spring-boot:run
I get an error "JavaFX runtime components are missing". The JDK version is 17.
I did an online search on the error message. Two solutions I find. One is to make the application modular. I don't see that is a valid solution for the Spring Boot application. And the other one is to add command line arguments for module path and modules names. The Maven command doesn't take arguments "--module-path" nor "--add-modules".
I also try to run the project with JDK only
java -jar app.jar
Although there aren't any errors, the GUI doesn't show up.
How to resolve this issue? A good solution would be that it is easy to run without many downloading, local setup etc.
Since Java 11, JavaFX was removed from the SDK. It is now in its own separate module, and if you want to use it in your application, you will need to specifically include it.
Regarding, "to make the application modular":
Spring 5 and Spring Boot are not friendly with Java Platform Module System (JPMS). It won't really be built for modules until Spring 6/Springboot 3 is released.
I do try the modular approach with Spring Boot 2. However, compilation
failed due to a known Lombok
error.
Regarding, "to add command line arguments for module path and modules names. The Maven command doesn't take arguments "--module-path" nor "--add-modules":
You can use non-modular approach without --module-path and --add-modules.
Maven configuration is pretty straightforward.
Firstly, Add the JavaFX modules you need as maven dependencies. For instance:
<properties>
<java.version>17</java.version>
<javafx.version>17.0.2</javafx.version>
</properties>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
The project can be built and run using Spring Boot Maven Plugin
NOTE: There is a maven plugin available for running JavaFX 11+ applications. So far, it is not required since the application will be packed and run using Spring Boot.
You may find a working example here, showcasing how to bootstrap JavaFX applications within Spring Boot application.
How to bootstrap JavaFX application within Spring Boot application?
The bootstrap process is heavily inspired by Mr. Awesome Josh Long’s
Spring Tips:
JavaFX
installment.
Instead of calling SpringBootApplication.run() use a custom bootstrap class inheriting from JavaFX Application. This is needed to initialize JavaFX correctly.
JavaFxApplication class does the heavy lifting for creating a proper JavaFX application with initialized Spring Context. It's responsible for:
Set Spring Boot web server type to NONE.
Programmatically create a Spring Boot context in the Application#init() method.
Kick off application logic by sending a StageReadyEvent containing the primary Stage as payload.
Support graceful shutdown for both Spring context and JavaFX platform.
Im literally searching for the same...
But I found there are some mods to do in your run-config to get this problem solved
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>
I am trying to learn Spring AOP programming using Spring 5. I am going through online materials.
I came to know that AOP is a concept, similar to OOP; and with AOP,OOPs becomes more powerful.
Now, I am trying to do some hands-on coding for AOP with Spring framework, version 5.
I am going to use maven as the build tool.
I am not clear on what are the various dependencies that we have to use in pom.xml, for example do we need to use: spring-aop , spring-aspects, aspectjetc.
Can anyone guide me what are the various maven dependencies that we have to add in pom.xml to be able to write using maven tool, Spring 5 AOP programs?
Thanks in advance
Its very simple, in order to work spring With AOP, You need aspectjweaver Library existe on the classpath of your application (version 1.6.8 or later).use this dependency to achieve that
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
As they mention in docs
To enable #AspectJ support with Java #Configuration add the
#EnableAspectJAutoProxy annotation:
You can find more info here
The only spring dependency to make something work with AOP is the famous spring-context :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
If you want a working example check my project in github which contains basic Maven AOP example based on spring
If you are trying to use CGLIB proxies in Spring without Spring Boot (and any starter dependencies of Spring Boot) then you need aspectjweaver besides spring-context, spring-aop dependencies.
Be careful not to add any of the dependencies if they already exist among your Maven dependencies - adding the same dependency with multiple versions may lead to a failing build.
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
My goal is understanding the J2EE lifecycle at a high-level with Spring, Hibernate, and Maven. From much research, I understand that Spring provides dependency injection and Hibernate provides object-relation mapping with databases. Maven is a tool to improve the build/deployment process from my understanding. With that said, everywhere I search I get more and more lost on configuration files (i.e. pom.xml, server.xml, etc.), terminology, and alternatives such as Gradle. I just want to build and launch the application and be able to see via http://localhost:8080 in tomcat.
At first, I couldn't get the default project (picture attached) built, but after further research found that I needed to Maven clean and Maven install.
I also modified settings in pom.xml changing version numbers and the database to use MySQL.
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.1.1.RELEASE</spring.framework.version>
</properties>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
The next issue I had was in OrderPersistenceTests.java it used org.hibernate.classic.Session which is no longer the correct class path and found that it needed to be org.hibernate.Session.
Finally, I was able to get it to build but cannot figure out how to deploy to Tomcat from Spring Tool Suite.
I have put together a simple example using Maven, Spring, Hibernate, and ExtJS for the front end at the following link:
https://github.com/drembert/java-webapp-sample
If you are using Spring Source Tool Suite which it looks like you are in the screen shot, you should be able to import everything using the "Import Maven Projects" option. The example uses Hypersonic as the in-memory DB to allow easier deployment. Keep in mind this example generates two different .war files (one is presentation-layer and the other is service-layer) to emulate a simple RESTful service so both will need to be deployed to the Spring tcServer (STS's version of Tomcat), but once they are you should be able to view the GUI at http://localhost:8080/presentation-layer. Another thing to make note of is that this example currently doesn't have a security layer which would normally be implemented using Spring security, but I am working on adding that in the near future.