Cucumber-JVM Runner class in different folder - java

I am new to Cucumber and learning and trying to automate application. I created a new Test Project. I want to keep the feature files and Cucumber runner class in a different folder and all the glue code in another folder.
How do I specify the feature file path under #CucumberOptions?
The project folder structure is as follows:
Project-2
|-src
|-test
|-java
|-mypackage
|-CucucmberRunner.java
|-resources
|-MyFeature.feature
Project-1
|-src
|-test
|-java
|-mypackage
|-MyFeatureTest.java
I tried giving "#CucumberOptions(features = "Project-2/resources/MyFeature.feature" ) but it is throwing NoSuchMethodError.
Can somebody please help!!

Add Project-1 dependency on Project-2 i.e. at runtime the test classes of Project-1 should be available in the classpath when you execute the cucumber tests.
Check mvn dependency:tree output and look for cucumber dependencies. All the dependencies are of same version i.e. cucumber-core, cucumber-junit, etc.

Bhushan Please click the link for Project Folders Structure

Related

Applying 1 junit-platform.properties file to entire project (multiple modules)

How can I apply a single junit-platform.properties to all modules in my project.
My project has 3 separate modules, each with its own test + resources.
Currently I'm duplicating and placing them in each resources directory. How can I reduce it to just 1 centralized junit-platform.properties and where should I place it?
The junit5 guide 4.5. Configuration Parameters describes
The JUnit Platform configuration file: a file named
junit-platform.properties in the root of the class path that follows
the syntax rules for a Java Properties file.
Therefore you could create a common project with your junit-platform.properties and add this project into the classpath for each of your projects.
for Eclipse workspace
Create baseproject with junit-platform.properties file.
junit-platform.properties with custom conmfiguration e.g. junit.jupiter.displayname.generator.default
junit.jupiter.displayname.generator.default = org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores
Other project project2 without any own junit-platform.properties
add baseproject to java build path of project2
Launching Junit test added baseproject into the classpath
Result of Junit test shows test display name with replaced underscores
for IntelliJ workspace with gradle
Using IntelliJ workspace , e.g. junitconfig.
Add module baseproject for junit-platform.properties (as resource)
Add module project2 for your Java files and Junit test files.
Add module baseproject as dependency for module project2 in build.gradle
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation project(':baseproject')
}
rebuild module project2 and run junit test

How to validate JSONs from another maven modules from same parent

I want to write a unit test that validates all JSON files from all dependent modules,
My Maven project structure is as follow,
Parent A -
module 1 - /resources/a.json
module 2 - /resources/a.json
.
.
.
module test - which reads the a.json from all parallel modules and validates.
Also, Ideally, my test code should not update after the new module added to the parent. only I should add a new dependency (of the newly added module) in the test module.
Can it be possible with some JUnit or Maven feature or plugin?
Thanks in advance.

Gradle inject root project class path to dependencies

I am building a library, which has separate entry points (like server, gui), with different dependencies. Each entry point is in a separate sub project with its own dependencies.
In the root project, where I start the build from, I want to select the entry point, and only build with that dependencies. That is working.
But I want to instantiate a class (eg MainClass) of the root project from the library entry point and I cant add the root projects class path to the dependency. (Diagram)
Root projects build file looks like this now:
dependencies{
implementation project(':server')
}
It seems to me that it would be easier to understand and clearer if the entry point projects depended on the core API instead of vice versa.
You could have project structure like:
settings.gradle
core/
build.gradle
src/
server/
build.gradle
src/
gui
build.gradle
src/
server and gui project build.gradle files should contain:
dependencies {
implementation project(':core')
}
The project that uses the library could depend on Server and/or GUI projects and instantiate the necessary class (ServerEntry or GuiEntry) directly.
If you want to be able to switch between different entry point implementations without changing the code in the project that uses the entry point instance I'd suggest using a dependency injection framework (Spring, Guice, Dagger). Dependency injection would help to separate configuration (binding interfaces to classes) from the actual application.
I solved my problem with Composite build. I added includeBuild '../path-to-lib' in the settings.gradle, Than I created a subproject Project to the library with the proper package and class name. On run it throws an error, that the developer should create this class.
Its also important to add all the subprojects to the same group:
allprojects{
group = 'library-group'
}
In the host project, I can depend on the library:
dependencies{
implementation module('library-group:suproject')
}
Now Gradle automatically overwrites the Classes on the same route as the documentation suggest, and I can finally inject my host project into the lib, and compile it as a whole.

Access file present in /test/resources - classpath issue

I'm working on a Spring multi-module project. One of the child modules has some files under /test/resources/certs/ and a property file under /test/resources/test-ssl.properties.
───resources
│ test-ssl.properties
├───certs
│ test-keystore.p12
test-ssl.properties has a property that points to /certs/test-keystore.p12file.
server.ssl.trust-store=/certs/test-keystore.p12
In child modules pom.xml I'm using Maven plugin test-jar and in parent pom I've added this module as a dependency.
With this structure integration test present in parent module is able to successfully read classpath:test-ssl.properties but it fails to resolve its property value.
Spring throws FileNotFoundException: \certs\test-keystore.p12. What change we can do to make Spring read a file present in test jar?
Also tried the following patterns,
server.ssl.trust-store=classpath:/certs/test-keystore.p12
server.ssl.trust-store=classpath:certs/test-keystore.p12
server.ssl.trust-store=classpath*:/certs/test-keystore.p12
Please note that this test property doesn't try to load any certificate. It is there because property placeholder can find some value for the property during build.
Issue is resolved by changing integration-test phase to process-test-resources.
Credit goes to the following answer of Pascal Thivent:
The content of the test output directory (target/test-classes) is on the class path, not src/test/resources. But resources under src/test/resources are copied to the test output directory by the resources:testResources goal (which is bound by default to the process-test-resources phase).

How to test not finding files on classpath? java (spring #Configuration)

I would like to test that a spring
#Configuration class
can handle missing files on the classpath. E.g. when using PropertySourcesPlaceholderConfigurer. But this is just a specific example, the question is really about how to test classes that interact with the classpath (e.g. read a file located in src/main/resources in a maven project).
So in essence I would like to create a spring context where I control the classpath in the test set up code.
The test needs to be a JUnit test.
Hope below may help you
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath*:/testApplicationContext.xml"})
public class YourTestClass{
you have to create a spring context for your test and you can include the production one into it. you can replace classpath*: with a absolute location.
Regards, Rajib.
This work if it's a maven project:
move the classpath file that you want to test the absence from to a separate pom jar module, and include it wherever needed.
move the classpath test to a separate pom jar module named missing-classpath-file-test, but don't include the module with the file that you want to simulate as missing. I will be missing from the classpath only for that test.
When running missing-classpath-file-test, the file will not be on the classpath, and the error you need to reproduce is achieved.
Concerning the question on the comment bellow, with the class loaders that come with application servers and the one used on a junit test it's not possible to programmatically change the classpath.

Categories

Resources