Tag Archives: Annotation

Skip all testcase for specific Class using Assume.assumeTrue() using jUnit

package myWorkjUnit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class LearnAfterBefore {
 public static boolean checkThisBeforeRunThisClass(){
// return true;
 return false;
 }


 @BeforeClass
 public static void testLoadConfiguration(){
 System.out.println("Load configuration files");
 System.out.println("initilize Excel files object");

 Assume.assumeTrue(checkThisBeforeRunThisClass());
 }

 @Before
 public void testsetUp() {
 System.out.println("Open browser");
 System.out.println("Navigate URL");

 }

 @Ignore
 @Test
 public void testAdvancedSearch() {
 System.out.println("Search on website");
 }


 @Test
 public void testVerifySearchResult(){
 System.out.println("Verify search result");
 }



 @After
 public void testtearDown() {
 System.out.println("Close Browser");
 }

 @AfterClass
 public static void testReportResult(){
 System.out.println("Report result");
 }

}

Skip all testcase for specific Class using Assume.assumeTrue() using jUnit

Skip all testcase for specific Class using Assume.assumeTrue() using jUnit

Reference : Assume (JUnit API)

How To Group Multiple Test Classes into a Suite using @runwith(suite.class) junit?

The “Suite Test” means bundle a few unit test cases and run it together.

Lets consider following 2 classes we want to execute in a Group
firstjunitprogram.java
LearnAfterBefore.java

Make following new class
JUnitSuiteTest.java

and Write following stuff into it and check out the results.

package myWorkjUnit;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ firstjunitprogram.class,LearnAfterBefore.class })
public class JUnitSuiteTest {

}

suite.class

suite.class

firstjunitprogram.java

package myWorkjUnit;

import org.junit.Test;

public class firstjunitprogram {
 @Test
 public void Divide() {
 System.out.println("Result - " + divide(10, 5));

 }

 @Test
 public void substraction(){
 System.out.println("Result - " + substraction(10, 5));
 }

 @Test
 public void Addition(){
 System.out.println("Result - " + addition(10, 5));
 }

 public int divide(int x, int y) {
 return x / y;

 }

 public int addition(int x, int y) {
 return (x+y);

 }

 public int substraction(int x, int y) {
 return (x-y);

 }

}

LearnAfterBefore.java

package myWorkjUnit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class LearnAfterBefore {
 @BeforeClass
 public static void testLoadConfiguration(){
 System.out.println("Load configuration files");
 System.out.println("initilize Excel files object");
 }

 @Before
 public void testsetUp() {
 System.out.println("Open browser");
 System.out.println("Navigate URL");

 }

 @Ignore
 @Test
 public void testAdvancedSearch() {
 System.out.println("Search on website");
 }

 @Test
 public void testVerifySearchResult(){
 System.out.println("Verify search result");
 }

 @After
 public void testtearDown() {
 System.out.println("Close Browser");
 }

 @AfterClass
 public static void testReportResult(){
 System.out.println("Report result");
 }
}

Reference Link 1

how to ignore test cases in junit? / how to use @ignore in junit?

Sometimes it happens that our code is not ready and test case written to test that method/code will fail if run. The @Ignore annotation helps in this regards.

  • A test method annotated with @Ignore will not be executed.
  • If a test class is annotated with @Ignore then none of its test methods will be executed.
    package myWorkjUnit;
    
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Ignore;
    import org.junit.Test;
    
    public class LearnAfterBefore {
     @BeforeClass
     public static void testLoadConfiguration(){
     System.out.println("Load configuration files");
     System.out.println("initilize Excel files object");
     }
    
     @Before
     public void testsetUp() {
     System.out.println("Open browser");
     System.out.println("Navigate URL");
    
     }
    
     @Ignore
     @Test
     public void testAdvancedSearch() {
     System.out.println("Search on website");
     }
    
     @Test
     public void testVerifySearchResult(){
     System.out.println("Verify search result");
     }
    
     @After
     public void testtearDown() {
     System.out.println("Close Browser");
     }
    
     @AfterClass
     public static void testReportResult(){
     System.out.println("Report result");
     }
    }
    
    

     

     

     

    Reference Link 1 Link 2

Example for @BeforeClass – BeforeClass (JUnit API) and @AfterClass – AfterClass (JUnit API)

@BeforeClass
Run before all the tests in a class (Runs once for all of the tests)
Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database.

@AfterClass
Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database.

Run after all the tests in a class (Runs once for all of the tests)

package myWorkjUnit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class LearnAfterBefore {
 @BeforeClass
 public static void testLoadConfiguration(){
 System.out.println("Load configuration files");
 System.out.println("initilize Excel files object");
 }

 @Before
 public void testsetUp() {
 System.out.println("Open browser");
 System.out.println("Navigate URL");

 }

@Test
 public void testAdvancedSearch() {
 System.out.println("Search on website");
 }

 @Test
 public void testVerifySearchResult(){
 System.out.println("Verify search result");
 }

 @After
 public void testtearDown() {
 System.out.println("Close Browser");
 }

 @AfterClass
 public static void testReportResult(){
 System.out.println("Report result");
 }
}

Reference

BeforeClass (JUnit API)

AfterClass (JUnit API)

how to use @before in junit? Before (JUnit API) / how to use @after in junit After (JUnit API)

@Before

Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class).
Run before each test method is run i.e. do a setup

@After
Run after each test method is run i.e. do a teardown
Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults).

package myWorkjUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class LearnAfterBefore {
 @Before
 public void setUp() {
 System.out.println("Open browser");
 System.out.println("Navigate URL");

 }

@Test
 public void testAdvancedSearch() {
 System.out.println("Search on website");
 }


 @Test
 public void testVerifySearchResult(){
 System.out.println("Verify search result");
 }



 @After
 public void tearDown() {
 System.out.println("Close Browser");
 }

}

Reference

Before (JUnit API)

After (JUnit API)

Sample example to learn @Test and junit – Test (JUnit API)

@Test
The annotation @Test identifies that a method is a test method.
Denotes a test method. Can be used with expected to assert expected results on the object under test.

package myWorkjUnit;

import org.junit.Test;

public class firstjunitprogram {
 @Test
 public void Divide() {
 System.out.println("Result - " + divide(10, 5));

 }

 @Test
 public void substraction(){
 System.out.println("Result - " + substraction(10, 5));
 }

 @Test
 public void Addition(){
 System.out.println("Result - " + addition(10, 5));
 }

 public int divide(int x, int y) {
 return x / y;

 }

 public int addition(int x, int y) {
 return (x+y);

 }

 public int substraction(int x, int y) {
 return (x-y);

 }

}

 

Reference Link 1

Lets make first sample program using jUnit ( Lets dive into jUnit)

Lets make first sample program using jUnit ( Lets dive into jUnit)

Lets make first sample program using jUnit ( Lets dive into jUnit)

  1. Go to Eclipse
  2. Make java project
  3. Make package
  4. Make new class without main() method
  5. And write following commands
 import org.junit.Test;

public class firstjunitprogram {


 @Test
 public void testMultiply() {
 System.out.println("Result" + multiply(10, 5));

 }

 public int multiply(int x, int y) {
 return x * y;
 }
}

 

  • Once we write @Test annotation
  • We get error
  • When we mouse hover on that we get option to import junit 4 library
  • Import this and Error will gone

 

  •  Now to run this
  •  Go to class file
  •  Right click on this file and Go to Run As
  •  Here we get option to select run with junit
  •  Once this run have look the result of junit

 

 

 

Junit 4 annotations

@Test
The annotation @Test identifies that a method is a test method.
Denotes a test method. Can be used with expected to assert expected results on the object under test.
@Before

Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class).
Run before each test method is run i.e. do a setup

@After
Run after each test method is run i.e. do a teardown
Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults).

@BeforeClass
Run before all the tests in a class (Runs once for all of the tests)
Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database.

@AfterClass
Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database.

Run after all the tests in a class (Runs once for all of the tests)

@Parameters
Allows you to run the same test with different data by defining the data parameters. @Parameters have to return List[], and the parameter will pass into class constructor as argument.
@RunWith
Exclusively tells the class that the Junit Test Case uses Parameterized runner

When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.

@Rule

 

 

JUnit Annotations

JUnit Annotations

how to use testng annotation @BeforeTest? @BeforeTest (TestNG)

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.

 &lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot;&gt;

&lt;suite name=&quot;Suite1&quot;&gt;

&lt;test name=&quot;testing Test_Base&quot;&gt;
 &lt;classes&gt;
 &lt;class name=&quot;testWordpress.Test_Base&quot; &gt;&lt;/class&gt;
 &lt;/classes&gt;
&lt;/test&gt;


&lt;test name=&quot;testing post&quot;&gt;
 &lt;classes&gt;
 &lt;class name=&quot;testWordpress.Test_post&quot; &gt;&lt;/class&gt;
 &lt;/classes&gt;
&lt;/test&gt;

&lt;test name=&quot;testing Tag&quot;&gt;
 &lt;classes&gt;
 &lt;class name=&quot;testWordpress.Test_tag&quot; &gt;&lt;/class&gt;
 &lt;class name=&quot;testWordpress.Test_Category&quot; &gt;&lt;/class&gt;
 &lt;/classes&gt;
&lt;/test&gt;


&lt;/suite&gt;

package testWordpress;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
public class Test_Base {

@BeforeSuite
 public void LogintoWordPress(){
 System.out.println(&quot;Login to wordpress&quot;);
 System.out.println(&quot;&quot;);
 }

 @AfterSuite
 public void LogoutfromWordPress(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Logout from wordpress&quot;);
 }
}

package testWordpress;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_Category {
 @BeforeClass
 public void NavigateToCategoryPage(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Click on Category page of wordpress-------------&quot;);
 System.out.println(&quot;&quot;);

 }

 @AfterClass
 public void writeStatusToExcel(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Write Pass/Fail/Skip status to Excel / HTML report&quot;);
 }

 @BeforeMethod
 public void VerifyExecutionStatus(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Verify testcase execution status from excel&quot;);
 ;

 }

 @AfterMethod
 public void WriteStatusToExcel(){

 System.out.println(&quot;Write Testcase Pass/Fail/Skip status to Excel / HTML report &quot;);
 System.out.println(&quot;&quot;);
 }

 @BeforeTest
 public void defineVariables(){
 System.out.println(&quot;-----------------------&quot;);
 System.out.println(&quot;@BeforeTest testing&quot;);
 }

 @Test
 public void AddnewCategory(){
 System.out.println(&quot; Testing Add new Category&quot;);
 }

 @Test
 public void EditCategory(){
 System.out.println(&quot; Testing Edit Category&quot;);
 }

 @Test
 public void ViewCategory(){
 System.out.println(&quot; Testing View Category&quot;);

 }

 @Test
 public void DeleteCategory(){
 System.out.println(&quot; Testing Delete Category&quot;);
 }

 @Test
 public void QuickEditCategory(){
 System.out.println(&quot; Testing Quick Edit Category&quot;);
 }
}

package testWordpress;

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 Test_post {

@BeforeClass
 public void NavigateToPostPage(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Click on post page of wordpress-------------&quot;);
 System.out.println(&quot;&quot;);
 }

 @AfterClass
 public void writeStatusToExcel(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Write Pass/Fail/Skip status to Excel / HTML report&quot;);
 }

 @BeforeMethod
 public void VerifyExecutionStatus(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Verify testcase execution status from excel&quot;);

 }

 @AfterMethod
 public void WriteStatusToExcel(){

 System.out.println(&quot;Write Testcase Pass/Fail/Skip status to Excel / HTML report &quot;);
 System.out.println(&quot;&quot;);
 }

 @Test
 public void AddNewPost(){
 System.out.println(&quot; Testing Add New post&quot;);
 }

 @Test(dependsOnMethods = { &quot;AddNewPost&quot; })
 public void EditPost(){
 System.out.println(&quot;Edit post Testing&quot;);
 }

@Test(dependsOnMethods = { &quot;EditPost&quot; })
 public void ViewPost(){
 System.out.println(&quot;View post testing&quot;);
 }

 @Test(dependsOnMethods = { &quot;ViewPost&quot; })
 public void QuickEditPost(){
 System.out.println(&quot;Quick Edit post testing&quot;);
 }

 @Test(dependsOnMethods = { &quot;QuickEditPost&quot; })
 public void TrashPost(){
 System.out.println(&quot;Trash post testing&quot;);
 }
}

package testWordpress;

import org.testng.SkipException;
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 Test_tag {
 @BeforeClass
 public void NavigateTotagPage(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Click on tag page of wordpress-------------&quot;);
 System.out.println(&quot;&quot;);
 }

 @AfterClass
 public void writeStatusToExcel(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Write Pass/Fail/Skip status to Excel / HTML report&quot;);
 }

 @BeforeMethod
 public void VerifyExecutionStatus(){
 System.out.println(&quot;&quot;);
 System.out.println(&quot;Verify testcase execution status from excel&quot;);

 }

 @AfterMethod
 public void WriteStatusToExcel(){

 System.out.println(&quot;Write Testcase Pass/Fail/Skip status to Excel / HTML report &quot;);
 System.out.println(&quot;&quot;);
 }

 @Test
 public void AddnewTag(){
 System.out.println(&quot; Testing Add new Tag&quot;);
 }

@Test
 public void EditTag(){
 System.out.println(&quot; Testing Edit Tag&quot;);
 }

@Test
 public void ViewTag(){
 System.out.println(&quot; Testing View Tag&quot;);
 throw new SkipException(&quot;Skipping - This is not ready for testing &quot;);
 }

@Test
 public void DeleteTag(){
 System.out.println(&quot; Testing Delete Tag&quot;);
 }
}

Difference between BeforeTest and BeforeMethod annotations in testNG ??

Please Refer this blog entry

Please Refer this blog entry

Pupunzi

For a better web

Tech

News and reviews from the world of gadgets, gear, apps and the web

VentureBeat

News About Tech, Money and Innovation

Digital Inspiration

Tips and tutorials around all things tech.

Thinkwareglobal's Blog

Just another WordPress.com weblog

Kenazari's Blog

Just another WordPress.com weblog

Richfowler1's Blog

Just another WordPress.com weblog

Romaicus's Blog

Just another WordPress.com weblog

botlaguduri

Just another WordPress.com site

chandanag89

Just another WordPress.com site

meghshetty

Just another WordPress.com site

digitalmediaexpert

digital media expert blog

Thinking in Silverlight

Silverlight/WPF/Windows 8 Store App

Different Journeys

Let's make life easier!

LAW RESOURCE INDIA

LEGAL RESOURCE CENTRE / COURT JUDGMENTS / LEGAL ARCHIVES

BookConnect

Promoting Indian language Books

Actively Lazy

Software, crafted with passion

SAPonPower

An ongoing discussion about SAP infrastructure

SAP Career

Your Career Mentor

hrsapcertification

Just another WordPress.com site

The secret product manager

Thoughts about product management, SUSE, Linux, ARM processors, Cloud, AIX, Power Systems, and other things that interest me

Sap Certification Material . Com

Quality store for sap software and courses

Simon Les Flex's Blog

SAP Process Integration Specialist - NOW WE CAN DO BETTER!

Oracle Technologies Primer

An Oracle Fusion Middleware and iPaaS blog!

Gilberto Holms

Java, Middleware, SOA Architecture Blog

The Skeleton

Java code skeletons, tips and pointers

J@n van Zoggel

integration, innovation & random technology stuff

The Pragmatic Integrator

Hints and tips from a pragmatic integration specialist

briskwalk

The pace of life..

Optimalbg's Blog

Just another WordPress.com site

IT Jobs in USA

Keep the track of this Blog if you are looking out for some good Opportunities to work in USA....

Referral Jobs | Right Place to Get a Job

Hurry you are one Click away to get a job - Fresher Jobs for 2012, 2013 fresher’s and also high paid jobs for well Experienced IT professionals on Referal jobs.

eccinternational.wordpress.com/

ISO Consulting | ISO 27001 | ISO 20000 | CMMI | BCM | GRI Sustainability Reporting & Assurance | eLearning | Trainings | Philippines

Michael Korn's Blog

Musings on Life, Career, Faith and Technology

WORKINGWITHQTP

Just another WordPress.com weblog

Solutions365

.net, WCF, Unity, IOC, SQL

SQA and Testers Blog

Just another WordPress.com weblog

Expertqtp's Weblog

Just another WordPress.com weblog

Sai Chamarthi

Experienced Testing Professional

saxenavinay

A great WordPress.com site

Suresh4qtp's Blog

Just another WordPress.com site

jonah95hill

This WordPress.com site is the cat’s pajamas

It's always something...

Just another WordPress.com weblog

Faculty of Information Technology - Lê Ngọc Tiến

Be Yourself! Simplify Our Goals! Nothing is Impossible!

Anti-Malware Testing

Thoughts on security product testing past and present

Methods & Tools

Practical knowledge for the software developer, tester and project manager