How to create a jar file? - unit-testing

How to create an executable jar project for Geb-Groovy based project in eclipse.
The following is the directory structure:
the pages package contains the groovy files
the testclasses package contains the test cases groovy files
the utils package contains the groovy files to read data of excel sheets.
Detailed instructions for creating the jar file would be highly appreciated.

If the project you are working with is a gradle project I would recommend looking at a task called "shadowJar" https://github.com/johnrengelman/shadow
in build.gradle would have something like this:
apply plugin: "com.github.johnrengelman.shadow"
mainClassName = '<Name of your Main Class>' //This will act as the jar's main point of entry the 'Main' method found in this class will be executed when the jar is executed
shadowJar {
manifest {
attributes 'Main-Class': mainClassName
}
}
Then you simply make a run the shadowJar task and a jar file is generated in your build folder. It should also contain all your dependencies as well.

Related

take liquibase scripts not from resource folder during unit tests

I have a following project structure
bin
start.sh
db
liquibase_scripts
...
schema.json
main
java
...
test
resources
liquibase_scripts
...
schema.json
So than I build my project, folder db with liquibase scripts added to distributive.
In unit tests I use H2 database and want to load schema from db/liquibase. I create bean
#Bean
public SpringLiquibase springLiquibase() {
SpringLiquibase springLiquibase = new SpringLiquibase();
springLiquibase.setDataSource(dataSource());
springLiquibase.setChangeLog("classpath:/liquibase/sam.json");
return springLiquibase;
}
The problem is that method setChangeLog look at resource folder in test folder.
So to solve the problem I copied all liquibase scripts to the test/resources directory.
But this is not ok becouse now I have 2 copies of scripts in different folders.
Is there a way to force springLiquibase.setChangeLog find scripts in any folder not only in test/resources?
In Maven build configuration you can define testResources, which may be directories and files. It looks like this:
<build>
<testResources>
<testResource>
<directory>${project.basedir}/db/liquibase_scripts</directory>
</testResource>
</testResources>
</build>
With such configuration Maven copies the files into the target/test-classes directory. Thanks to that those files can be used as test resources in the code, but there's no duplication in the project files.
Im using such configuration (Liquibase + testResources) in one of my projects. To better reproduce your problem, I've created a separate branch, where you can find the configuration described above - see: this commit diff. All test pass after the change.

Payload contains two or more files with the same destination path for Default.rd.xml file

I was trying to do unit testing for UWP App. When I added target application reference in Test project, the following error is giving while running the test method
Payload contains two or more files with the same destination path'Properties\Default.rd.xml'.Source files: D:\Test\MyAppTesting\Properties\Default.rd.xml D:\Test\MyApp\Properties\Default.rd.xml
No error code was given.
adding refernce
public class MyAppTesting
{
[Fact]
public async void PassingWinAppServiceTest()
{
AgendaService agendaService = new AgendaService();
var memberModel = await agendaService.GetMemberDetailsById("234");
Assert.False(String.IsNullOrEmpty(memberModel.MemberId.ToString()));
}
The only workaround I found is to remove the Default.rd.xml file from the test project.
Comments from the developer community for similar issue.
From the documentation of Runtime Directive Configuration file,
A runtime directives (.rd.xml) file is an XML configuration file that
specifies whether designated program elements are available for
reflection.
So, if reflection is not used in the test project then I believe, Default.rd.xml file can be safely removed from the test project.
After installing the NuGet, you need to add an existing project reference into the unit test project.
[right click test project -> Add -> Reference-> select an existing project}
Then check your test project dependencies, Your existing project should be there.

Libgdx test from gradle command line assets not found

I have a LibGDX project with some tests. The structure is as follow:
core/src -> for my java sources code
core/test -> for my tests source code
core/assets -> for all my assets
When I run the tests from eclipse, they all go green but whenever I try to run them from the gradle command line (./gradlew test) I get an error with the assets folder. I believe this is because the tests are not launched from the assets folder as it is from eclipse.
How can I solve this problem ? Is there a way to tell gradle to use core/assets as the workspace directory when running the tests ?
Here is the error I get:
com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: myasset.png
I found a way to achieve what I wanted. I post my solution here for anyone that might need it. There is a property named workingDir for the test task in gradle. So I just needed to set it to the right folder. Inside the build.gradle file of your project (the root folder) just add the following section:
project(":core") {
apply plugin: "java"
// Add the following test section
test{
workingDir= new File("/assets")
}
// Rest of the file
}
That's it! My tests are running green from the command line now.

calling gradle "build" task in another project

I am only a couple of days into using gradle
In my current build.gradle script I have a task which I would like to call the build task in another project (ie. defined in a different build.gradle somewhere else) after each time it is executed
My question is how do I call a task from another project?
I guess I want to do something like tasks.build.execute() but it doesn't seem to work. I tried this:
commandLine "${rootDir}\\gradle", 'build', 'eclipse'
it at least executed the build and eclipse for my current project just not the master project. Hope my question is clear
Adjust the buildFile path, etc.:
task buildSomethingElse(type: GradleBuild) {
buildFile = '../someOtherDirectory/build.gradle'
tasks = ['build']
}
build.finalizedBy('buildsomethingElse')
Reference: Gradle organizing build logic guide, paragraph 59.5.
You can apply this to another ant project as well, by adding a one line build.gradle file in your ant project that just calls ant, like so:
ant.importBuild 'build.xml'
First read this:
http://gradle.org/docs/current/userguide/multi_project_builds.html
If you have a multi-project build
You need a root project that contains settings.gradle file with something like:
include 'myproject1'
include 'myproject2'
Then you can just make a dependency from one project to another like this:
myproject1/gradle.build
task someOtherTask() << {
println 'Hello'
}
myproject2/gradle.build
task sometask(dependsOn: ':myproject1:someOtherTask') << {
//do something here
}
Or if you want to call a task:
project(':myproject1').tasks.build.execute()
Notice: You have to apply java plugin to make build task available.

How can I not include a build task when I include a project in my settings.gradle file?

My settings.gradle file looks like:
include "serverTest", "shared"
And the serverTest build.gradle file looks like:
group = 'gradle'
version = '1.0'
defaultTasks 'build'
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = JavaVersion.VERSION_1_6
dependencies
{
compile project(':shared')
}
The directory structure is: The top level holds the settings.gradle file and it has folders shared and serverTest in it. Then in the serverTest directory there is the build.gradle file.
When I run gradle at the top level it outputs:
:shared:compileJava UP-TO-DATE
:shared:processResources UP-TO-DATE
:shared:classes UP-TO-DATE
:shared:jar UP-TO-DATE
:serverTest:compileJava UP-TO-DATE
:serverTest:processResources UP-TO-DATE
:serverTest:classes UP-TO-DATE
:serverTest:jar UP-TO-DATE
:serverTest:assemble UP-TO-DATE
:serverTest:compileTestJava
:serverTest:processTestResources UP-TO-DATE
:serverTest:testClasses
:serverTest:test
I don't want it to execute the task :serverTest:test though. I tried changing my defaultTasks to just compileJava but that didn't work, does anyone else have any ideas?
Well this question has been asked in different ways , the main theme of the question is;
How to exclude sub tasks of build task?
1 . gradle build -x test
The above instruction is helpful while executing gradle build command from CLI; this exludes test task, but we want to exclude test task programmatically in build.gradle file. Look below the answer for the respective question.
2 . check.dependsOn -= test
Copy this small script in your build.gradle file.
Now when you execute gradle build from CLI, your tests won't run at all.
Cheers !
You could try to disable the task only if a build task is present... Something like:
project(":serverTest").test.onlyIf { !gradle.taskGraph.hasTask(":shared:build") }