Unable to run testng annotations in Eclipse - java

I was trying to understand the working of testng framework by implementing a simple program in Eclipse editor(in ubuntu os). My source code and pom xml are defined as follows:
TestExample.java
package test;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
#Test(suiteName = "test suite for annotation")
public class TestExample {
#BeforeSuite
public void setup() {
System.out.println("before test");
}
#AfterSuite
public void finish() {
System.out.println("After test");
}
#Test(description = "Test to save student", priority = 0)
public void testSaveStudent() {
System.out.println();
System.out.println("Testing.....");
System.out.println();
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nithin.exmple</groupId>
<artifactId>TestingExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
But on execution my Eclipse editor fails to give expected output (it is rendering an output of a program which I have run recently). I have executed the same program on IntelliJ IDE which in turn gives expected results.

Install TestNg plugin - testng.org/doc/eclipse.html

Related

Compatibility java 18 and 8 versions

I am using the blade library in a java 18 project. See code below.
public class Application {
public static void main(String[] args) {
String path = switch (args[0]) {
case "test" -> "/test";
default -> "/default";
};
Blade.create()
.listen(8081)
.get(path, new MyHandler())
.start();
}
private static class MyHandler implements RouteHandler {
#Override
public void handle(RouteContext context) {
context.text("hello");
}
}
}
This code is throwing an exception:
java.lang.IllegalArgumentException: Unsupported class file major version 62
at org.objectweb.asm.ClassReader.(ClassReader.java:176)
at org.objectweb.asm.ClassReader.(ClassReader.java:158)
at org.objectweb.asm.ClassReader.(ClassReader.java:146)
at org.objectweb.asm.ClassReader.(ClassReader.java:284)
at com.hellokaton.blade.asm.ASMUtils.findMethodParmeterNames(ASMUtils.java:30)
at com.hellokaton.blade.mvc.handler.RouteActionArguments.getRouteActionParameters(RouteActionArguments.java:39)
at com.hellokaton.blade.mvc.RouteContext.injectParameters(RouteContext.java:551)
at com.hellokaton.blade.server.RouteMethodHandler.handle(RouteMethodHandler.java:75)
at com.hellokaton.blade.server.HttpServerHandler.executeLogic(HttpServerHandler.java:149)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)
at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:482)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at java.base/java.lang.Thread.run(Thread.java:833)
This library is written in java 8. I thought that using such switch pattern with enums is incompatible with java 8. However, if I replace one line like in the example below, then it will be executed successfully. Switch-enum pattern hasn't been removed.
// Blade.get() signature:
// public Blade get(String path, RouteHandler handler);
Blade.create()
.listen(8081)
.get(path, new MyHandler()) // <- old (exception)
.start();
Blade.create()
.listen(8081)
.get(path, context -> new MyHandler().handle(context)) // <- new (why works?)
.start();
Using lambda expression instead of object solved the problem. Why didn't the new switch-enum pattern break the code, and what is the reason for the exception then?
Interface RouteHandler looks like this:
#FunctionalInterface
public interface RouteHandler {
void handle(RouteContext var1);
}
My pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github</groupId>
<artifactId>test</artifactId>
<version>1.0-snapshot</version>
<packaging>var</packaging>
<name>test Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>18</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>com.hellokaton</groupId>
<artifactId>blade-core</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>test</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

using POM to skip mvn integration tests in Eclipse

I have created Tests and IntegrationTests (IT) defined in the POM and classNames which I run from the commandline; I now wish them to run them from Eclipse. Here is the skeleton:
test:
package org.xmlcml.it;
import org.junit.Assert;
import org.junit.Test;
public class SimpleTest {
#Test
public void simpleTest() {
Assert.assertTrue("simpleTest", true);
System.out.println("RAN simpleTest");
}
}
run with:
mvn clean test
and the integration tests:
package org.xmlcml.it;
import org.junit.Test;
import junit.framework.Assert;
public class SimpleIT {
#Test
public void integrationTest() {
Assert.assertTrue("integrationTest", true);
System.out.println("RAN integrationTest");
}
}
run with
mvn clean integration-test
My POM is:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<junit.version>4.8.2</junit.version>
<!-- if this is uncommented the tests are all skipped
<skipTests>true</skipTests>
-->
<!--
<skipITs>true</skipITs>
-->
</properties>
<groupId>org.contentmine</groupId>
<artifactId>simple</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Simple</name>
<description>Test integration test</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
<!-- http://maven.apache.org/surefire/maven-failsafe-plugin/usage.html -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When I run this from Eclipse (4.5.2) the IT tests are run. How can I alter the POM so they are skipped by default (and how can I switch them back on)?
NOTE: I have used
http://maven.apache.org/surefire/maven-failsafe-plugin/usage.html
for guidance but still have problems with the multiple ways of switching tests on and off (#Test, *Test.java, method public void testFoo(), ${skipTests} ...) and some of the SO answers are labelled as obsolete. I am not clear which take precedence - is there an up-to-date cheat sheet?

Junit with maven program is not working

When i run this i am getting Initialization error and there is no console error.
I have attached my junit test code and pom.xml code. please help me with the solution. And its a Maven project.
Junit program:
import static org.junit.Assert.*;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class JunitTest {
#Test
public void test() {
// fail("Not yet implemented");
System.out.println("Junit code");
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.in/");
Select s = new Select(driver.findElement(By.id("searchDropdownBox")));
s.selectByVisibleText("Books");
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("harry potter");
}
}
Pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testmaven</groupId>
<artifactId>mavenDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>
</project>
You need to define a build phase in POM for doing the compilation of integrated unit tests.
add this code snippet in pom.xml for running the test classes.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can find more information on:
http://maven.apache.org/surefire/maven-failsafe-plugin/usage.html

How to run junit tests on a non java project

I have a project folder but it is not a java project. It is a maven project. I have written a junit test and it runs perfectly when running in the eclipse IDE but when I run the maven command mvn install, it seems to skip my junit tests. I have included the test file in src/test/java/ (the name of my test is AppTest.java) and the main .java file (with the main method) is in src/main/java/. I have noticed that the project I am currently working on is a maven project and not a maven java project. I have included a screen of my current folder structure:
folder structure
Maven test output <- should not build as I have a deliberate test that should fail
This is the POM. I have deleted/commented out some sensitive parts so the pom file may be syntactically wrong but the main plugins I use are there; tap4j, junit and surefire.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>integration-api-parent</artifactId>
<groupId>uk.gov.dwp</groupId>
<version>1.0.2</version>
</parent>
<artifactId>aa</artifactId>
<version>1.0.6</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>aa</finalName>
<plugins>
<!-- plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin>
<plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin-->
<plugin>
<!-- Plug-in utilised for the execution of the JMeter Integration Tests -->
<!-- These tests are executed against the nominated integration server where as -->
<!-- instance of AA exists -->
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>1.9.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreResultErrors>false</ignoreResultErrors>
<suppressJMeterOutput>false</suppressJMeterOutput>
<overrideRootLogLevel>INFO</overrideRootLogLevel>
</configuration>
</plugin>
<plugin>
<!-- Step to copy the latest plug-ins that form this build to the integration server -->
<!-- This is done using the SCP command via the ANT plug-in thus allowing it to execute on all platforms -->
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tap4j/tap4j -->
<dependency>
<groupId>org.tap4j</groupId>
<artifactId>tap4j</artifactId>
<version>4.2.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<!-- plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/cassandra-assembly.xml</descriptor>
<descriptor>src/main/assembly/devenv-assembly.xml</descriptor>
<descriptor>src/main/assembly/main-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<!-- plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<configuration>
<skipPoms>false</skipPoms>
<sourceDirectory>${project.basedir}/src/main/latex-templates</sourceDirectory>
<outputDirectory>${project.build.directory}/latex</outputDirectory>
</configuration>
</plugin-->
</plugins>
</build>
</project>
AppTest:
package AccessGateway;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
import org.tap4j.consumer.TapConsumer;
import org.tap4j.consumer.TapConsumerFactory;
import org.tap4j.model.TestSet;
public class AppTest {
Practise prac;
final String DIRECTORY = "C:\\Users\\Hello\\Desktop\\";
#Test
public void testHeaderProcessor() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderProcessor.txt")));
}
#Test
public void testHeaderPortForward() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderPortForward.txt")));
}
#Test
public void catunittest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catunittest.txt")));
}
#Test
public void catunitcrowstest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catcrowd.txt")));
}
#Test
public void testCrowd() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPCrowd.txt")));
}
#Test
public void testADFS() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPADFSformat.txt")));
}
}
The problem is the packaging of your project which is pom
You can't execute Surefire on this kind of project.
Try adding surefire plugin. When i have tests in my app i always include it (works for junit as well as testng). Based on your logs i can see that you dont have it declared.
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>

How to import other project in Maven

I have two projects com.mvnworkstation.common and com.mvnworkstation.test i have created base class in project common and all tests in project test.
i have created pom as below for project com.mvnworkstation.common
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mvnworkstation.common</groupId>
<artifactId>com.mvnworkstation.common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
and have created pom as below for project com.mvnworkstation.test
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mvnworkstation.test</groupId>
<artifactId>com.mvnworkstation.test</artifactId>
<version>0.0.1-SNAPSHOTA</version>
<dependencies>
<dependency>
<groupId>com.mvnworkstation.common</groupId>
<artifactId>com.mvnworkstation.common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>/suite/Login.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
and my Baseclass in project com.mvnworkstation.common
package com.mvnworkstation.common.base;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;
public class Baseclass {
public WebDriver driver = null;
#BeforeClass
public void setup(){
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
#AfterClass
public void teardown(){
driver.close();
}
}
test class in project com.mvnworkstation.test
package com.mvnworkstation.test.sourcecode;
import org.testng.annotations.Test;
import com.mvnworkstation.common.base.Baseclass;
public class Login extends Baseclass{
#Test
public void login(){
driver.get("https://www.google.co.in");
}
}
if i run my test i am getting compilation error as follows,
RROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[3,30] package org.testng.annotations does not exist
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[5,38] package com.mvnworkstation.common.base does not exist
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[7,28] cannot find symbol
symbol: class Baseclass
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[9,10] cannot find symbol
symbol: class Test
location: class com.mvnworkstation.test.sourcecode.Login
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[11,17] cannot find symbol
i have added necessary dependency project to import base class and testng jar not sure why i am getting compilation? can some one correct me?
Test scope dependencies are not transitive.
Your project com.mvnworkstation.test does not declare a dependency on org.testng:testng.

Categories