Tech-Today
How to use TestNG with eclipse
This tutorial will teach you how to setup and run TestNG on eclipse.
What you need:
1.) Download and setup eclipse-java-helios
2.) Download and extract TestNG (http://testng.org/doc/download.html) in c:\java\testng-version
2.) Install TestNG on eclipse using this link (http://testng.org/doc/download.html)
a.) In eclipse select Help->Install New Software and enter the update url
3.) TestNG documentation here: http://testng.org/doc/documentation-main.html
Working on eclipse:
1.) Create a new java project JavaHelpers.
2.) Create a new file build.xml (ant file)
3.) Create a new class TestNGDemo.
4.) Add Testng.jar to the project's build path by: right click project->Build Path->Configure Build Path->Select Libraries Tab->Add External Jar->Locate TestNg.jar to where we extract earlier (c:\java\testng.jar)
5.) This class, test add and sub mathematical operations.
package com.kbs.testng;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestNGDemo {
@Test
public void sum() {
Assert.assertEquals(7, 4 + 4);
}
@Test
public void sub() {
Assert.assertEquals(0, 4 - 4);
}
/*
* I am not a test.
*/
public void sumNotTest() {
Assert.assertEquals(7, 4 + 4);
}
/*
* I am not a test.
*/
public void subNotTest() {
Assert.assertEquals(0, 4 - 4);
}
}
The most important thing to notice is the @Test annotation, it tells us that, that method is a test. You can put the @Test annotation in the class level, which means all the method inside the class are test. Also note that if there's no @Test annotation on class level and on a method, it means it's not a test.
5.) To create a file which will tell the system of all the class which has test, right click the class->TestNG->Convert to TestNG->Next->Finish. And you will have a file testng.xml created in your project, which contains:
<suite name="Suite" parallel="none">
<test name="Test" preserve-order="false">
<classes>
<class name="com.kbs.testng.TestNGDemo">
</class></classes>
</test>
</suite>
Note: You can add test classes here.
6.) To test a class simply: Right click the class->Run As->TestNG Test.
7.) To test the whole project: Right click testng.xml->Run As->TestNG Suite.
8.) And this is how it should look like:
9.) If you want to do it the ant way, here's the build script:
<project name="JavaHelpers" default="" basedir=".">
<!-- set global properties for this build -->
<property name="program_name" value="JavaHelpers" />
<property name="package_name" value="JavaHelpers" />
<property name="src" value="src" />
<property name="build" value="build" />
<property name="testng.home" value="C:/java/testng-6.2" />
<property name="testng.jar" value="testng-6.2.jar" />
<property name="testng.report.dir" value="testng-output" />
<path id="compile.boot.path">
<fileset dir="${testng.home}">
<include name="${testng.jar}" />
</fileset>
<fileset dir="${java.home}/lib">
<include name="rt.jar" />
</fileset>
</path>
<target name="testng">
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath location="${testng.home}/${testng.jar}" />
</taskdef>
<mkdir dir="${testng.report.dir}" />
<testng outputdir="${testng.report.dir}" classpath="${basedir}/${build}/classes">
<xmlfileset dir="${basedir}" includes="testng.xml" />
</testng>
</target>
<!-- Compile Source -->
<target name="compile">
<mkdir dir="${build}/classes" />
<javac destdir="build/classes" target="1.6" source="1.6">
<bootclasspath refid="compile.boot.path" />
<src path="${src}" />
<include name="**/*.java" />
</javac>
</target>
</project>
After a successful run it will create a folder: testng-output in your project's root directory.
-
How To Setup Arquillian Testing With Wildfly
This tutorial requires: Knowledge with GITKnowledge with archetypeRequirements:WildflyeclipseWhat to do:In eclipse create a new maven project: File->New->Other, enter maven in the filter. Select Maven Project. Click next, then next. In the filter...
-
A Maven Project That Have Multiple Property Files Via Maven Antrun Plugin
This tutorial will answer several questions (below) within a maven project that has several configuration files (dataset, property, persistence). 1.) How to have multiple persistence, datasource and property files depending on the profile or environment...
-
Useful Tools When Building A J2ee Project
Below are the tools I commonly use when setting up my eclipse and before creating a new project. JBoss Tools - http://download.jboss.org/jbosstools/updates/nightly/trunk/ TestNG - http://beust.com/eclipse FindBugs - http://findbugs.cs.umd.edu/eclipse/...
-
Creating And Testing A Web Service Using Soap Ui
I just want to share a simple example on how to create and test a web service on java. Download and install the following: 1.) eclipse java (client) 2.) eclipse jee (web service) 3.) jboss 5.1 AS 4.) soap ui, get the stand alone (http://www.soapui.org/)...
-
Junit 4.7 Expected Exception Failed
I recently downloaded the new version of JUnit which is 4.7 and linked it to my project. Then all the test cases that should be ok if an exception is encountered failed. My immediate solution was to remove the extends TestCase to each test classes. With...
Tech-Today