Is there any way I can create a "jar" file to run my cucumber tests? - cucumber-jvm

My Feature files resides in src/test/resources and testrunner class resides in src/test/java as per default cucumber-jvm set up.
I want to package this project in a jar file so that when I execute the jar, my tests should run.
How should I do it?

After a lot of tinkering around I think I may have a solution.
You would have two files (Sorry if my jargon is a bit off, I'm new to this too)
A Step Definition file and a Test Runner file. Make sure in your Test Runner file you have a main method
public static void main(String[] args) throws Exception {
JUnitCore.main(
"PackageName.Filename");
}
So that is ends up looking something like this:
public class TestRunner {
public static void main(String[] args) throws Exception {
JUnitCore.main(
"cucumberTest.TestRunner");
}
}
Run it as a Java Application.
Once that is done, you can now click it and export it as a Runnable Jar File. In the Launch configuration after selecting the Runnable Jar File option, choose your TestRunner from the drop down if it hasn't been selected and then select the appropriate Library handling. (I chose Extract required libraries to generate JAR) And an appropriate file destination.
Then click finish.
In my Test Runner java file I also added this:
#CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
,plugin={"pretty",
"json:C:/EclipseWorkspace/ProjectName/cucumber.json",
"junit:C:/EclipseWorkspace/ProjectName/cucumber.xml"}
)
This dumps the test into two usable files, JSON and XML, where you can view the details of your test.
I hope this helps.

Related

Error: Could not find or load main class in Maven converted project

I just created a java project and ran it. It ran fine.
Then I converted it into a maven project and resolved all errors in POM.XMl file as well. When I try to run the class file , Am getting the error
Error: Could not find or load main class jhj
Caused by: java.lang.ClassNotFoundException: jhj
My class looks like this:
public class jhj {
public static void main(String[] args)
{
System.out.println("as");
}
}
And my Environment variables are properly set.
I have set the M2_home, MAVEN_HOME and JAVA_HOME as well.
I tried removing the project from the workspace and re-importing them.
Also, I tried with refresh and Maven>>update the project options as well
Check your project build-path and enable specific output folders for each folder. Go one by one though each source-folder of your project and set the output folder that maven would use.
For example, your web project's src/main/java should have target/classes under the web project, test classes should have target/test-classes also under the web project and so.
Using this configuration will allow you to execute unit tests in eclipse.
Just one more advice, if your web project's tests require some configuration files that are under the resources, be sure to include that folder as a source folder and to make the proper build-path configuration.
Hope it helps.

How to execute code when running Entity Framework Core code-first Add-Migration command?

I have the need in a .NET Core 2.2 project to run a small bit of code (load environment variables from a .env file) when I run an Add-Migration command. Is there anywhere in my project I can put this code so that it runs before the migration is created? I read somewhere that Startup.Configure gets executed but that's not the case. The fact that I need to set the startup project when creating the migration implies that something is getting executed.
To clarify the reasoning, here's what I'm trying to accomplish:
In my Visual Studio solution I also have a docker-compose project. I use a .env file (so it can be added to .gitignore) to populate the environment. I want to use the same .env file to setup the environment for the .NET projects to avoid duplicating entries in, for example, an appsettings file. I found a Nuget package that works really well for loading .env files at startup so the code side is covered. The problem is that when I try and add a migration, I can't run this package to import the .env file which contains the connection string data so the migration add fails.
Thanks to Fabio's comments above I was able to figure out the solution from the Microsoft Docs article that I somehow missed. In my case I am creating the app using IWebHostBuilder. In this situation, Design-time tools like migrations make a call to CreateWebHostBuilder in Program.cs. In order to load my environment variables from the file, I had to chain a call to ConfigureAppConfiguration, where I then load the environment variables from my .env file. And yay, now I have one place for environment variables that's used by Docker Compose and the .NET project.
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args).
ConfigureAppConfiguration((hostingContext, config) =>
{
// Load the contents of the .env file into the environment variables
// Necessary to do this in ConfigureAppConfiguration so design-time processes (eg. migrations) have the environment settings.
DotNetEnv.Env.Load("../../.env");
}).
ConfigureLogging((hostingContext, logging) =>
{
...
}).UseStartup<Startup>();
}

Error while creating a jar through artifacts in Intellij

First of all, i searched for similar questions and i found something, but nothing helps me out.
I'm trying to create a jar file in intellij using the artifact, but everytime i build i have the error: Error: Could not find or load main class com.test.wms.test
This is my test.java file
package com.test.wms;
public class test {
public static void main(String[] args){
// empty
}
}
Then i went into Project Structure -> Artifacts and added a new artifact, selected the type (jar), selected the name (test.jar) and generated the manifest.mf (autogenerated from the artifact page on intelij)
Manifest-Version: 1.0
Main-Class: com.test.wms.test
And this is the artifact edit page
Then i build the project and try to lunch in konsole with java -jar test.jar and the error is always the same: Error: Could not find or load main class com.test.wms.test
This is my project structure:
And this is my artifact edit page
Where is the mistake?
Thanks!
Fixed by myself, when i was creating the jar from the artifact i was selecting empty instead of From modules with dependancy (the output is the same but with this option works, maybe the IDE does something particular)

Multiple feature files execution(include and exclude of tags) using command line arguments in selenium cucumber script

I am able to execute selenium cucumber tests using command line like
mvn clean test -DCucumber.options"--tags=#Addversion,~#Import"
am excluding #Import tag. But it is executing both the features.
I implemented my test class like this
#RunWith(Cucumber.class)
#Cucumber.Options(features = "src/test/resources/featureFiles",//path to the features
format ={"json:target/integration_cucumber.json"}//what formatters to use
)
public class RunCucumberTest {
}
src/test/resources/featureFiles is my feature file location.
In this location I added two feature files using #Addversion and #Import
If I include tags option in my test class I am able to exclude(like ~#Import) particular feature file using eclipse.
But am not able to exclude particular feature file using command line.
Can any one give me any suggestions about this.
I resolved my problem to run feature file by excluding some of the tags
I changed my test class
#RunWith(Cucumber.class)
public class RunCucumberTest {
}
And in command line argument specified feature files as option-less arguments like
mvn install -Dcucumber.options="src/test/resources/featureFiles --tags #Addversion,#~Import"

Is there a maven archtype for creating a console app?

Is it possible to generate a maven stub project via mvn archetype:generate that has a main function, and part of the build process includes making a App.cmd/App.sh to run the app from a shell?
It seems that I need the maven-shade-plugin to create a console app jar with wrapper scripts for command line startup. However, I don't know if their is an archtype to configure a maven project to use it and create an empty console app.
The "Simple" archetype creates a maven project with a class that has a "main" method. Use that with your maven-shade-plugin to create an executable jar and you are good to go!
"org.apache.maven.archetypes" "maven-archetype-quickstart" (version 1.1) worked for me.
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}