Annotation:
Annotation defines a type and it can be applied to several Java elements such as Java methods, classes, etc. Annotation adds meta-data facility to Java elements. Some examples of Java built-in annotations are:
- @Override
- @Deprecated
- @SuppressWarnings
Each annotation will instruct the compiler to do something. For example, @Override tells the compiler to check whether the parent class contains the same method (also checks whether the overriding method follows all the rules correctly).
Let us have a look at TestNG annotations.
- @BeforeSuite
- @AfterSuite
- @BeforeTest
- @AfterTest
- @BeforeGroups
- @AfterGroups
- @BeforeClass
- @AfterClass
- @BeforeMethod
- @AfterMethod
- @DataProvider
- @Factory
- @Listeners
- @Parameters
- @Test
Out of these annotations, we will try to understand only “@Test, @BeforeClass, @AfterClass, @BeforeMethod, @AfterMethod”.
Every Java application should contain “main” method and the execution of the entire project starts from “main” method. With using TestNG as a testing framework, there is no need to use “main” method. The execution will be handled by the TestNG (testing framework).
@Test
If a Java method marked with @Test, then the compiler understands that the method is a “Test Method”.
@BeforeClass
A Java method marked with @BeforeClass annotation will be executed before executing all the methods in the class. This annotation can be used to make sure that the configuration or some precondition for entire set of “test methods” inside the class will be executed before all the methods.
@AfterClass
A Java method marked with @AfterClass annotation will be executed after executing all the methods in the class. Hence, this annotation helps in executing a particular set of actions that needs to be done after executing all the “test methods” in the class.
@BeforeMethod – Execute the method just before executing any test method in the class.
@AfterMethod – Execute the method after executing every test method in the class.
Now, open Eclipse IDE and create a .Java file and use the following code.
package com.selftechy.testng; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class tngAnnotations { @BeforeClass public void beforeClass(){ System.out.println("In BeforeClass"); } @BeforeMethod public void beforeMethod(){ System.out.println("In BeforeMethod"); } @Test public void testOne(){ System.out.println("In TestOne"); } @Test public void testTwo(){ System.out.println("In TestTwo"); } @AfterMethod public void afterMethod(){ System.out.println("In AfterMethod"); } @AfterClass public void afterClass(){ System.out.println("In AfterClass"); } }
Execute the code as below:
Click Run –> Run As –> TestNG Test
Output of the executing should be as follows:
[TestNG] Running: C:\Documents and Settings\parwathi\Local Settings\Temp\testng-eclipse--966152916\testng-customsuite.xml In BeforeClass In BeforeMethod In TestOne In AfterMethod In BeforeMethod In TestTwo In AfterMethod In AfterClass PASSED: testOne PASSED: testTwo =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Failures: 0, Skips: 0 =============================================== [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1a679b7: 15 ms [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@1430b5c: 16 ms [TestNG] Time taken by org.testng.reporters.EmailableReporter@e102dc: 16 ms [TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 0 ms [TestNG] Time taken by org.testng.reporters.XMLReporter@a1807c: 0 ms
This execution shows how exactly the annotations instruct the java to execute the test methods. The below figure shows the order of execution.
In the upcoming posts, will try to explain the rest of the annotations and other features of the TestNG. Also, we will try to understand how the TestNG is used with Selenium for Test Automation.
Hi seetharam,
I got below output after running the above code please explain….
[Parser] Running:
C:\Users\suresh\sample1\temp-testng-customsuite.xml
In BeforeClass
In BeforeMethod
In TestTwo
In AfterMethod
In BeforeMethod
In TestOne
In AfterMethod
In AfterClass
PASSED: testTwo
PASSED: testOne
===============================================
com.selftechy.testng.tngAnnotations
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
sample1
Total tests run: 2, Failures: 0, Skips: 0
===============================================
how they appear inside the java class?? first testTwo then testOne??
here in my case testTwo executed first and then testOne please explain this…
Hi Pls check that in the Name of the methods wether u had the keyword Test if u not give this the execution order by default is 2 and then one …. give a try
Hi Seetharam,
Could you please Explain the following Annotations.
@DataProvider
@Factory
@Listeners
@Parameters
How to use them.
Hi seetharam,
Not sure if this query is related to current page, but still very much related to Running tests in TestNG.
I had exported selenium IDe testcase as “Java/JUnit4/Webdriver. Next I created a Java project in eclipse (Eclipse Juno – 4.2 and TestNG 6.8.6; attached selenium jar files and added TestNG library) and copied that testcase. Now, I converted that testcase to TestNG and tried to run/debug in TestNG. I am getting below error:
com.thoughtworks.selenium.SeleniumException: Connection refused: connect
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:121)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:101)
at com.thoughtworks.selenium.DefaultSelenium.captureScreenshot(DefaultSelenium.java:751)
at com.thoughtworks.selenium.ScreenshotListener.onTestFailure(ScreenshotListener.java:45)
at com.thoughtworks.selenium.ScreenshotListener.onConfigurationFailure(ScreenshotListener.java:59)
at org.testng.internal.Invoker.runConfigurationListeners(Invoker.java:1868)
at org.testng.internal.Invoker.handleConfigurationFailure(Invoker.java:334)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:237)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at com.thoughtworks.selenium.HttpCommandProcessor.getOutputStreamWriter(HttpCommandProcessor.java:149)
at com.thoughtworks.selenium.HttpCommandProcessor.getCommandResponseAsString(HttpCommandProcessor.java:176)
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:118)
… 22 more
com.thoughtworks.selenium.SeleniumException: Connection refused: connect
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:121)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:101)
at com.thoughtworks.selenium.DefaultSelenium.captureScreenshot(DefaultSelenium.java:751)
at com.thoughtworks.selenium.ScreenshotListener.onTestFailure(ScreenshotListener.java:45)
at com.thoughtworks.selenium.ScreenshotListener.onConfigurationFailure(ScreenshotListener.java:59)
at org.testng.internal.Invoker.runConfigurationListeners(Invoker.java:1868)
at org.testng.internal.Invoker.handleConfigurationFailure(Invoker.java:334)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:237)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at com.thoughtworks.selenium.HttpCommandProcessor.getOutputStreamWriter(HttpCommandProcessor.java:149)
at com.thoughtworks.selenium.HttpCommandProcessor.getCommandResponseAsString(HttpCommandProcessor.java:176)
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:118)
… 26 more
FAILED CONFIGURATION: @BeforeTest setUp(null, null)
java.lang.RuntimeException: Could not contact Selenium Server; have you started it on ‘localhost:4444’ ?
Read more at http://seleniumhq.org/projects/remote-control/not-started.html
Connection refused: connect
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:104)
at com.thoughtworks.selenium.SeleneseTestBase.setUp(SeleneseTestBase.java:134)
at com.thoughtworks.selenium.SeleneseTestBase.setUp(SeleneseTestBase.java:103)
at com.thoughtworks.selenium.SeleneseTestNgHelper.setUp(SeleneseTestNgHelper.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
FAILED CONFIGURATION: @AfterMethod selectDefaultWindow
com.thoughtworks.selenium.SeleniumException: Connection refused: connect
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:121)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:101)
at com.thoughtworks.selenium.DefaultSelenium.selectWindow(DefaultSelenium.java:377)
at com.thoughtworks.selenium.SeleneseTestNgHelper.selectDefaultWindow(SeleneseTestNgHelper.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at com.thoughtworks.selenium.HttpCommandProcessor.getOutputStreamWriter(HttpCommandProcessor.java:149)
at com.thoughtworks.selenium.HttpCommandProcessor.getCommandResponseAsString(HttpCommandProcessor.java:176)
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:118)
… 29 more
SKIPPED CONFIGURATION: @BeforeClass getSelenium
SKIPPED CONFIGURATION: @BeforeMethod setTestContext
SKIPPED CONFIGURATION: @AfterMethod checkForVerificationErrors
SKIPPED: testLogin_Red
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 2, Skips: 3
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 2, Skips: 3
===============================================
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@1f12af8: 9 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@1fc5c47: 9 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1f622fa: 36 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@bb473b: 17 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 4 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1816032: 2 ms
I run/debug the same testcase successfully in JUnit4. I don’t understand why I get Connection Refused error.
Can u help me on this?
Hi Seetaram, Really great stuff to learn on our.. Thank you for sharing…
Can you please elaborate on the below annotations as well.. that will really great…
thank you in advance….
@DataProvider
@Factory
@Listeners
@Parameters
Sure.. Thanks…
Is it possible to have a For Loop outside the @Test
Please see below sample:
for(int x=1; x<5; x++){
@Test
public void testOne(){
System.out.println("In TestOne");
}
@Test
public void testTwo(){
System.out.println("In TestTwo");
}
}
Why dont you improve upon your java skills first?
Hi.Thank you a lot for the new features. Unfortunately they don’t seem to work that well with me.The meothds page only works if the editor has focus . For example, if by any chance the package view has focus, and then I invoke the wizard, the meothds page will be empty.When it works, the meothds list is filled with the public meothds of all the editors.An alternative to this, that I think would be more intuitive, would be adding a new option, to the TestNG context menu, to generate a test to from the selected java file.Not sure if it’s because I’m in Linux, but the directory src/test/java isn’t detected in a maven project.Sorry for the crappy bug report and thank you for the new features.