Call .Java files, based on the test case name - java

Environemnt - Java + Junit +WebDriver
My requirement is this:
I have 120 XLS test cases, they are in xls.
(Example ->
1st test case -Discussion_DOcuOne.xls
2nd test case -Discussion_DOcuTwo.xls
etc...)
I have relevant Java files (one .java file for each xls test case).
(Example ->
1st test case -Discussion_DOcuOne.java
2nd test case -Discussion_DOcuTwo.java
etc...)
Through Java code, I am reading one by one xls test case.
And I need to call the related .Java file.
if testCaseName=Discussion_DOcuOne,then,
---- I need to call the Discussion_DOcuOne.java file
I have tried switch+case (by assigning numbers to all xls test cases).
But I need to write 120 case statements, which is not at all practical.
-----Here I stuck. I don't know how to RUN/CALL the specific Java file.
//For example the testCaseName is "Discussion_DOcuOne", I need to call/run the Discussion_DOcuOne.java.
I don't get any idea how to link these two.
Please find the example Java class (nothing but Java code for each manual test case, we call it TEST SCRIPT).
Every .Java file has one method runTestCase(), and I need to call that specific method which belongs to that specific test script.
---------------------- Here is the Discussion_DOcuOne.Java file---------------
public class Discussion_DOcuOne(){
String varOne="abc";
String varTwo="efg";
public void runTestCase(){
//do some thing using the variables above
}
}

just get name of test:
String fileName = "Discussion_DOcuOne";
then load class
Class clazz = Class.forName(fileName);
And create instance of Discussion_DOcuOne
clazz.newInstance();
then cast object to your interface and call your method

Related

JUnit 5 test case for code that write files to file system. (Files.write)

I am fairly new with Junit and I need to write JUnit 5 test case for a method which has code statement:
public void method() {
// some code
Files.write(filepath)
// some more code
}
Will this code create new files every time when unit test is run ?
Is there a way to write a unit test for this scenario where files are not being created, or get deleted automatically once the test method execution is complete ?
Solution needs to be JUnit 5 compatible.
I have used JUnit 5's Temporary Directory(#TempDir) functionality to create a temporary directory and then pass the directory path to Files.write() method (along with the file name of course). The #TempDir will delete the files and temporary folder, it created, once the test execution is done. This way, I was able to execute the test case.

How to change method name at runtime in Java?

I have following test method which takes parameters from excel sheet. Let say I have 5 test cases, so this method will execute 5 times. But when I execute first test case (TC01) the Test() method name should change at runtime according test scrips like Test_TC01(),Test_TC02() etc.
#Test
public void Test() throws Exception {
ExcelUtils.setExcelFile(System.getProperty("user.dir") + "\\src\\data_engine\\DataEngine.xlsx");
DOMConfigurator.configure("log4j.xml");
String Path_OR = System.getProperty("user.dir") + "\\src\\config\\OR.properties";
FileInputStream fs = new FileInputStream(Path_OR);
OR = new Properties(System.getProperties());
OR.load(fs);
DriverScriptTest startEngine = new DriverScriptTest();
startEngine.execute_TestCase();
}
Please share your comments
In short, you can't.
What you can do is create a new class (at runtime!), compile it and run it.
Yes, what I'm talking about is that you write code to:
Create the class (in a temp file)
Use the Java Compiler API to compile the class.
Call the methods on the compiled class instance.
Good luck! I've worked with this code and it's very interesting, but almost always overkill unless you really need it.

How to get the file name from .properties file in java JUnit?

I have 8 test cases in which each test case makes use of a different file. How do I get the specific file from the .properties file which contains the path for the file(s). Some of the test cases are as shown below:
#Test
public void testIfColDataReadIsCorrect() throws FileNotFoundException{
obj.readExcelToGetData("D:/ExcelTestFiles/testExcelWithAllColData.xlsx");
rowObj= obj.getRowRecord();
assertEquals(rowObj.getName(), TEST_NAME);
assertEquals(rowObj.getId(), TEST_id);
assertEquals(rowObj.getDate(), TEST_DATE);
assertEquals(rowObj.getMessage(), TEST_MSG);
assertEquals(rowObj.getPage(), TEST_PAGE);
assertEquals(rowObj.getType(), TEST_TYPE);
assertEquals(rowObj.getLikeCount(),TEST_LIKECOUNT);
assertEquals(rowObj.getShareCount(), TEST_SHARECOUNT);
assertEquals(rowObj.getCommentCount(), TEST_COMMENTCOUNT);
}
#Test
public void testWhenNameColDoesNotExists() throws FileNotFoundException{
//FacebookDataExtraction obj= new FacebookDataExtraction();
//FacebookFields rowObj=new FacebookFields();
obj.readExcelToGetData("D:/ExcelTestFiles/testExcelWithNoNameCol.xlsx");
rowObj= obj.getRowRecord();
assertEquals(rowObj.getName(), null);
assertEquals(rowObj.getId(), TEST_id);
assertEquals(rowObj.getDate(), TEST_DATE);
assertEquals(rowObj.getMessage(), TEST_MSG);
assertEquals(rowObj.getPage(), TEST_PAGE);
assertEquals(rowObj.getType(), TEST_TYPE);
assertEquals(rowObj.getLikeCount(),TEST_LIKECOUNT);
assertEquals(rowObj.getShareCount(), TEST_SHARECOUNT);
assertEquals(rowObj.getCommentCount(), TEST_COMMENTCOUNT);
}
I think this is not the best practice to directly input the file path to the method readExcelToGetData(). After going through certain posts I found that the files can the put in .properties file and can be read from it. How do I get the specific file path in each test case?
You can load files from the classpath via a ClassLoader. E.g. : this.getClass().getClassLoader().getResourceAsStream("myFiles.properties");
So depending on your IDE, you might put the properties file into your source- or resources folder.
Running the JUnit test with #Parameterized runner allows you to run multiple iterations of the same test with different inputs.
You can read the test parameters from whatever source you wish and use them in the parameterized test without the need to copy the test method for every input.
You'll need at least JUnit 4.11 to use Parameterized.

Can i rename package paths of an existing jar packages?

i have a ready to use .jar file and want to know if its possible to extract and rename the packages?
so when usually i start the .jar file with:
java -cp myFile.jar com.codehelper.demo.Main
i want to rename the "codehelper" in it to something different that i can run it by
java -cp myFile.jar com.NEW_NAME.demo.Main
i tried to decompile all files, add it to the folderstructure with renamed "codehelper" path and compile it again but it didnt work. i also renamed all the package includes in each file like
import com.codehelper...
so is my goal unreachable or can i do this? and if someone can explain me how to do, it will be very nice.
thank you and sory for my poor english
edit: it seems the only file i cant compile is a file containing this switch case.
private int priotiryLevel(DiscoveryInfoBehave info)
{
int ret = 0;
switch (1.$SwitchMap$com$peerialism$natcracker$common$GatewayDevice$GatewayType[info.getNatDevice().getGatewayType().ordinal()])
{
case 1:
ret = 0;
break;
case 2:
ret = 4;
break;
case 3:
ret = 5;
break;
}
return ret;
}
i tried also to rename the specific word inside this switch case but no effort.
Write a new wrapper class:
package com.NEW_NAME.demo;
public class Main
{
public static void main(String[] args)
{
com.codehelper.demo.Main.main(args);
}
}
compile it and add it to the jar. You can now invoke it as:
java -cp myFile.jar com.NEW_NAME.demo.Main
and it will silently dispatch to the real implementation.
This is not possible generally. If you rename some files, Java wont be able to find them (public class name must be same as file name). You can rename file with main class and call it as #Joe suggested in his answer. But if you rename somthing else, it will stop working. There could be cals to those classes in code. Same goes for the "codehelper" name. You can not remove it from code. Even if you remove it from one file, anyone will still be able to see this somewhere else in the code.
Rename directory is the same as rename file. You destroy namespace (package) and code will no longer work, because inside classes, this package is used. Plus there is no need to have import in the code, since you can call directly com.package-name.class from the executive code. By renaming package, you will destroy this and program will crash. It may run for a while, but once the program reach to this call, it will crash.
So this
import com.codehelper...
is not mandatory in the code, even if the code is using the package. You can write directly
com.codehelper.*** xy = new com.codehelper.***();
Even if you rename everything in the code, you still dont have guaranted functionality. Code may be using reflection and create class instances from sting code. For example see this:
Java how to instantiate a class from string
Under the line comment:
Doing some decompile -> compile work, is seems like code stealing, if you are not willing to pay licence and you want to hide it.
Plus doing something like this, it is ALWAYS a bad practice. I dont see any real use for this.

How to embed version strings in java classes

We'd like to have the version of the java source code, in our case the svn $Id$ string, embedded in the generated class file.
We'd like to be able to determine this information from a static inspection of the class file, preferably by running the strings or what command.
A naive attempt to declare a private final static String variable set to this value inside each class didn't result in a legible string embedded in the class file.
You said ... preferably by running the strings or what command ...
Let's assume you have something like this in your code:
private static final String SVN_ID =
"$Id: SvnIdDemo.java 1081 2008-09-30 19:03:23Z john $";
The following Perl one-liner ...
$ perl -nwe 'print "$1\n" if /.*(\$Id:[^\$]+\$).*/' SvnIdDemo.class
... prints the SVN ID string to STDOUT.
$Id: SvnIdDemo.java 1081 2008-09-30 19:03:23Z john $
You could add a method to the bottom of each class with a predefined name. Say you used:
public String extractChaimGeretzVersionNumber() {
return "$Id$";
}
Then you find the version with a program that loads the class and, via reflection, calls the magic method and retrieves the version.
You would either have to have code that inserted the method to the .java files before building the .class and .jar files OR you could have a build step that checked that the magic method was already in every one of them. Fail the build if not found.

Categories

Resources