How to print exceptions on console? I am using TestNG and Maven - java

I want to see the exceptions on console. I am using TestNG with Maven as a build tool. I have defined my testng.xml in the Maven surefire plugin.

https://www.javadoc.io/doc/org.testng/testng/latest/org/testng/reporters/VerboseReporter.html
you should use above reporter , but the constructor expects a string so you cannot initialize it using testng.xml ( if any one knows how to pass string argument to listener in testng.xml please do add it here )
So the work around is to add the listener through script and initiate testng through a java entry file.
public static void main(String[] args) {
TestNG testng = new TestNG();
// Create a list of String
List<String> suitefiles = new ArrayList<String>();
// Add xml file which you have to execute
suitefiles.add(prop.getProperty("path_to_your_existing_testngxml\testng.xml"));
// now set xml file for execution
testng.setTestSuites(suitefiles);
testng.addListener(new VerboseReporter("[TestNG] "));
// finally execute the runner using run method
testng.run();
}
Output:
Note
As this reporter constructor expects a string you should not provide it in your testng.xml you will get initialization error

If you use any kind of logging framework you should probably stick with that.
Otherwise you can always print Exceptions using https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Throwable.html#printStackTrace() (and siblings) such as
...
} catch (Exception e) {
e.printStackTrace(System.out);
e.printStackTrace(System.err);
}
...

Related

How to generate Allure Report programmatically - after end of test suite

How to generate Allure report from Java code?
Right now I'm using terminal together with allure-comandline as allure generate build/allure-results --clean
I'd like to automatically generate it after end of test suite. I did some research and found this https://mvnrepository.com/artifact/io.qameta.allure/allure-generator but unfortunately this doesn't work or I don't know how to use it properly.
Also tried this:
new AllureReportBuilder("1.5.4", new File("build/allure-report")).unpackFace();
new AllureReportBuilder("1.5.4", new File("build/allure-report")).processResults(new File("build/allure-results"));
Anyone have working code? Thanks!
I assume that you are using Maven/Gradle to run tests. Why not use Maven/Gradle plugin to generate reports?
Allure Maven /
Allure Gradle
new AllureReportBuilder("1.5.4", new File("build/allure-report")).unpackFace();
new AllureReportBuilder("1.5.4", new File("build/allure-report")).processResults(new File("build/allure-results"));
If above code you written actually build report, then you can use it in something like this.
// This will run only once, just before program is exiting
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
new AllureReportBuilder("1.5.4", new File("build/allure-report")).unpackFace();
new AllureReportBuilder("1.5.4", new File("build/allure-report")).processResults(new File("build/allure-results"));
}));
}
Add above code to your base test class (if you don't have base test class, just add it to the existing test class). It will be executed after all tests ran.

How to run mvn cucumber test using jar file?

I have created a test automation framework using maven and cucumber.
1) I want to create a jar file which includes everything (all project files)
2) Then I want to run a test from the command line using above created jar like using the command
(mvn clean test -Dcucumber.options='--tags #all')
I don't want to use the main method or anything.
java -Dcucumber.options="--tags #all" -jar your-test-jar.jar
Try this. Although I am not sure why you don't want to use the main method. If you don't use the main method it will just become too complicated.
Update:
Write a main method and run Cucumber main method from it. The arguments are what you would pass in as your Cucumber command line arguments.
public static void main(String[] args) throws Throwable {
String[] arguments = {"a", "b"};
cucumber.api.cli.Main.main(arguments);
}
If I have understood your question clearly, this might do your work.
This should help you run Cucumber from your executable.
The below code worked for me to execute the cucumber tests from runnable jar with test frame work as TestNG.
Executing jar: java -jar ProductsAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar
import io.cucumber.core.cli.Main;
public static void main(String args[]) throws Throwable {
try {
Main.main(new String[] {
"-g","com.sadakar.cucumber.common",
"-g","com.sadakar.cucumber.runner",
"classpath:features",
"-t","#SmokeTest",
"-p", "pretty",
"-p", "json:target/cucumber-reports/cucumber.json",
"-p", "html:target/cucumber-reports/cucumberreport.html",
"-m"
}
);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Main method exception : " + e);
}
}

Build jar with tess4j

I create project in Intellij Idea, add tess4j 2.0 from maven, write test application. When I start debug all works fine. When I click "build artifacts" and launch jar file I haven't any result, no errors, nothing.
public class MainApp {
static String fileName = "C:\\Users\\Alex\\Google Drive\\TW\\LIB\\Tess4J\\eurotext.png";
public static void main(String[] args) {
try {
System.setOut(new PrintStream(new File("output-file.txt")));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Start");
ITesseract instance = new Tesseract1(); // JNA Direct Mapping
try {
String result = instance.doOCR(new File(fileName));
System.out.println(result);
} catch (TesseractException e) {
System.out.println("Error");
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("End");
}
}
Output when debug
Start
The (quick) [brown] {fox} jumps!
Over the $43,456.78 #90 dog
& duck/goose, as 12.5% of E-mail.........
End
Output when launch jar
Start
No "Error", no "End". How it's possible?
I had the same issue. Sometimes it doesn't load classes correctly for some reason. here's how i was able to get it fixed for some far.
Build your artifact and then Remote debug your jar with intellij.
Find out in which line your application breaks.
Then wrap it with try{... line where application breaks..} catch(Error e){e.getMessage(); }
You can see the error message in variable "e" in debug output. So you can determine the issue.
May be this is not an appropriate way to do it. But this is how i was able to track down the errors in my jar file.
How ever it is best to use this as a temporary way to track the errors in your jar and to find out why it breaks and then find out correct way (Which is i'm not currently aware about.. but however got to heard about some library called One-Jar) to build jar without getting any errors on deployment.

Using JunitCore for tests in different projects

Background:
I am currently working on a project in eclipse that programatically executes JUnit tests that are pushed to a server.
So far everything works but I would like to know the results of the tests (specifically any failures) so I can push them out to an email. Right now the tests just output to the console but that doesn't seem to give me much output to actually use.
Right now I use the Runtime class to call the tests but that doesn't seem to have the functionality I need for getting results.
I have looked into the JUnitCore class but can't call any tests outside of the current java project.
So my main question would be how can I use JUnitCore to run junit tests in a specific JAR file? Or is there an easier way to approach this problem using a different class?
This is the only thing I've been able to get to work:
RunTests()
{
junitCore = new JUnitCore();
junitCore.run(AllTests.class);
}
But I would like to do something along the lines of this:
RunTests()
{
junitCore = new JUnitCore();
junitCore.run("C:\\$batch\\test\\hil research\\201507071307\\CommsTestRunner\\plugins\\TestSuite\\US35644.class");
}
I would appreciate any suggestions to this problem I am having. I'm an EE and was just introduced to java last month so this has been quite the challenge for me.
JUnitCore expects to read loaded classes, not class files in a JAR. Your real question is likely how to load the JAR (or directory of .class files) so it can be run.
Poke around with URLClassLoader; once you've amended the classpath appropriately, you can get a Class out of findClass and pass it into the JUnitCore methods you've found.
Since the tests might have classes that are also used by your server (but not necessarily at the same version) I would suggest not having your server directly run the tests. Instead, you can have your server start a new JVM that runs the tests. This is how IDEs like Eclipse run tests. You simply need to write a main class that has JUnit run the tests, and serializes the results on disk.
Your main class would look something like this:
public class MyRunner {
public static void main(String... args) throws IOException {
String path = System.getProperty("resultPath");
if (path == null) {
throw new NullPointerException("must specify resultPath property");
}
// Possibly install a security manager to prevent calls to System.exit()
Result result = new JUnitCore().runMain(new RealSystem(), args);
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path)) {
out.writeObject(result);
}
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
Then your server simply needs to construct a java command line with the jars that include the tests, the JUnit jar file, and a jar that contains MyRunner.

Failing a jenkins job

I have a .Jar file that will just load data into database when it's run. I have scheduled to run this job via Jenkins. When I execute the job in Jenkins it runs the .JAR successfully. However say if there is a null pointer exception in the job and it did not complete successfully. Even then Jenkins says that job has "Passed". How do I fail the job if there is an issue during the job execution?
#Corey's solution is good. And if you don't want to write a JUnit test and support it in Jenkins, you can just do what he alluded to earlier: catch the null-pointer exception (really, just have a top-level catch in your app), and call the API to exit with a return code:
try {
myCode.call();
catch (Exception e) {
System.out.println("An exception was caught at the top level:" + e);
System.exit(-1);
}
Last time I had this problem, I decided to take a different tack and changed the program call into a junit test. Jenkins was quite happy then.
Steps I took:
1. create an empty (maven) project
2. added a single java class SmokeTest.java
3. Added test that called the method I was testing via a script
4. Create a (maven) Jenkins job to run the project
Contents of my test:
public class SmokeTest
{
private static final String OK = "OK"; //$NON-NLS-1$
#Test
public void test()
{
// Create a new instance of the Firefox driver
final WebDriver driver = new HtmlUnitDriver();
final String url = PropertyManager.getInstance().getString(PropertyManager.SMOKE_TEST_URL_BASE) + "smoke/smoketest"; //$NON-NLS-1$
AuditLog.registerEvent("Smoke test url is: " + url, this.getClass(), AuditLog.INFO); //$NON-NLS-1$
driver.get(url);
// Find the text element by its id
final WebElement databaseElement = driver.findElement(By.id("database")); //$NON-NLS-1$
final String databaseResult = databaseElement.getText();
Assert.assertEquals(SmokeTest.OK, databaseResult);
//Close the browser
driver.quit();
}
}
The most important part here is the "Assert.assertEquals" line. The result of this is pickup by jUnit and therefore jenkins
Jenkins jobs fails if the exit code is anything but zero.
System.exit(1);
Should work (or fail, to be more precise :-)

Categories