Troubles with Jenkins: unable to run jUnit tests. - java

All tests, that creates or removes files and dirs fails. I try to use env var to point root directory from surefire plugin(${WORKSPACE}), manually at pom.xml (e.g. ${env.WORKSPACE}). I even set those workspace dir directly in test code (String WORKSPACE="/ud/rpt_trunk_b"). But tests fails anyway. Does anyone uses jUnit with Jenkins?
Thanks.

Related

How does one compile and run a standalone executable JAR so that it can invoke Maven to run the parallel tests set up by Cucumber-JVM-parallel-plugin?

I am trying to execute parallel tests through the use of Maven InvocationRequest, Invoker, and InvocationResult components which are a part of maven-invoker dependency added into the pom.
Ultimately, I want Jenkins to run an executable jar which in turn executes the parallel tests. The code to programmatically invoke Maven to run the tests looks like this:
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File("pom.xml") ); //pom is in the root level of the project
request.setShowErrors(true);
List<String> goalList = Arrays.asList("clean","install");
request.setGoals( goalList );
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(System.getenv("MAVEN_HOME")));
InvocationResult result = null;
try {
result = invoker.execute( request );
} catch (MavenInvocationException e) {
e.printStackTrace();
}
if ( result.getExitCode() != 0 )
{
throw new IllegalStateException( "Build failed." );
}
This code lives in the main class (i.e., public static void main(String[] args) ) which serves as the entry point when running the compiled executable JAR. The idea is that Jenkins will fire off something akin to this -->> "java -jar MyCompiledTests.jar" ... and this would run the code above which allows the tests to run in parallel.
This works so long as I use the pom location where the original project files reside. But, I am needing to compile a JAR which will live on an integration server and would be run by Jenkins daily (I do this with the current battery of tests but currently they all run in series). It seems that when I build the executable JAR (using maven-assembly-plugin), the pom file is not added to it.
So, the code above which needs to access the pom will return an error saying it cannot find the pom. To remedy that, I manually added the pom file to sit alongside the JAR in the given remote folder, but then when running this arrangement, I receive an error concerning the absence of the Features folder.
This error happens when the Cucumber-JVM-parallel plugin is reached during pom execution. Receiving this error stems from the fact that the pom file is now being read but the various path references within this plugin are not recognized because they are relative to the location of the original project folders in Eclipse and not the new remote folder location alongside the executable JAR.
Again, I can run things in Eclipse just fine and can even run the compiled executable JAR as long as I can point to the pom file which lives at the original project folder.
The Question:
How does one compile and run a standalone executable JAR so that it
can invoke Maven to run the parallel tests set up by
Cucumber-JVM-parallel-plugin?
How should the pom be referenced in the code above differently?
Can the pom be wrapped into the executable JAR when it is compiled...or should it be?
Is there a more elegant way to compile and run the executable JAR
I have searched far and wide, but either I am not seeing the answer right in front of me, or my intended goals for this executable JAR are unachievable given the current state of maven technologies.

Build failing in intellij

Current scenario : Two file in same package but in different module one in main and other in Test.
Reason : Because when I run my unit test it automatically access file in test module,where as when I run the main module it access the file in main module.
Problem : Now when I am running build task it is failing. As now my unit test are failing as they are not able to pick the right file of the two.Due to which my build is failing.
Is there anyway I can overcome this issue.

Creating a Jar for a Automation project Java+Gradle+Selenium

Ihave a gradle project that has an automated test cases for a web application using selenium. All the tests and test cases are with in the package src/test/java. The tear down scripts (to clean up the entries in DB are written in src/main/java after test case execution). I tried to build a JAR using gradle for the same. I could get 2 different JAR's one that contains just the classes under src/main/java and the other containing src/test/java. Is there a way to build a single JAR that contains both? Let me know if there is an alternate way of doing the same.
task testJar(type: Jar) {
classifier = 'tests'
from sourceSets.test.output
}
gradle.taskGraph.afterTask { Task task, TaskState state ->
testJar.execute()
}
Thank you!

Resource files not found when running JUnit test

I'm having trouble running my JUnit test, it fails because it is not able to find the projects resources.
My test starts a server which loads all the needed resources using an URLClassLoader. I have no problem with the resources not being found when running my project otherwise. This problem only occurs when I try to run my JUnit test.
I have tried adding the file paths as arguments in my runtime configuration for the test like this, ex:
-cp .:/path/to/the/config/file
But it makes no difference.
Help please!
Project structure:
Default Maven test path is src/test/java, so if your tests are not in that directory, you have to define your test path in pom.xml with tag <testSourceDirectory>, inside <build>. You have to include maven-surefire-plugin, too.
The convention in Maven is to have all Java test code in src/test/java. This is to prevent accidental inclusion of test code in the final product. So I suggest you move your tests.
Since you don't show the code which you use to load the resource, I can't tell you whether there are any bugs in that. But /path/to/the/config/file is most likely wrong; is must be /path/to/the/config so you can say in Java getClass().getClassLoader().getResource("file").
But I suggest to use a command line argument to specify the config file:
main(String[] args) {
File configFile = new File(args[0]).getAbsoluteFile();
if(!configFile.exists()) throw new IllegalArgumentException("Unable to find config file: " + configFile);
...
}
That makes error handling much more simple. Also, it allows to use different config files in tests and later in production.

Java/Eclipse: How to configure Run Configuration's Classpath for JUnit test?

I have an Eclipse project with the following directory structure:
MyProj/
src/main/java/
com.me.myproject.widgets
Widget.java
src/main/config
widget-config.xml
src/test/java
com.me.myproject.widgets
WidgetTest.java
src/test/config
widget-test-config.xml
The Widget class reads its config (XML) file in from anywhere on the classpath and uses it to configure its properties.
I am trying to just get WidgetTest's test cases (all written with JUnit) to run inside Eclipse when I right-click the file and go to Run As >> JUnit Test. I assume I'll have to actually run it as a customized Run Configuration with its own configured classpath, but I'm not sure about that as I've never done this before.
Does anybody know how I can get a custom Run Configuration to run WidgetTest.java as a JUnit test, and successfully place src/test/config/widget-test-config.xml on the classpath? Thanks in advance!
Please note, this question is not about how to read a resource from the runtime classpath, its about how to get it on Eclipse's JUnit Run Config classpath in the first place!
I was under the impression that as long as you have src/test/config/widget-test-config.xml inside what Eclipse considers to be a source folder, it should already be on the classpath.
Is src/test a source folder for Eclipse ? If it is and you still get the problem, try out the following experiment :
If you copy widget-test-config.xml to the src root can Widget class read it ?
If Yes
then it's a problem of the test folder not being on the classpath and you may wanna try adding it manually like so.
Right click WidgetTest and select Run As -> Junit Test. This should automatically create a Junit Run Configuration accessible at Run -> Run Configurations. You modify it's Classpath entry to add the project containing the .xml file like so :
If No
If, even after moving the .xml file to the src root (i.e. default package), your widget class cannot read it then there is something else wrong. In that case, it would be great if you could furnish the snippet of code in WidgetTest which is trying to read the .xml file.
Working Code
Here is a bit of working code :
public class A {
#Test
public void test() {
InputStream stream = A.class.getResourceAsStream("/SomeTextFile.txt");
System.out.println(stream != null);
stream = Test.class.getClassLoader()
.getResourceAsStream("SomeTextFile.txt");
System.out.println(stream != null);
}
}
The above works for me in a simple JAVA project and runs fine. (Running fine means getting
'true' printed on the console)
I am in the process of creating a GITHub repo for you to try out this code painlessly.
GIT Hub Repo with Test project
You should be able to import the project in this zip and see the code working. Right click on the Test class A and click Run As -> Junit Test, and you should see two true in the Console.
If your WidgetTest class is written as a JUnit test, eclipse will try to run it as a Junit test automatically. If it doesn't, you should right click on the class in the package explorer, choose Run As >> Run Configuration >> choose Junit
To run a Junit test:
in JUnit3, the class should implement TestCase and all the method names should start "test"
in JUnit4, all the methods should be preceded by a #Test annotation
To place that config file in the classpath: when setting the Run Configuration as above, go to the Arguments tab in the upper right pane and in the VM arguments specify the classpath:
-cp .:/path/to/the/config/file
However, if that file is in a package in the source directory, it should automatically be included in the classpath.

Categories