DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Designing a Page Object Model + TestNG Hybrid Framework: Patterns That Actually Scale
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Quality Crisis in Robotics and How Video Annotation Fixes It
  • Code Rewriting With AI and TDD

Trending

  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik
  • Event-Driven AI Systems With Kafka and Autonomous Agents
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. TestNG @BeforeClass Annotation Example

TestNG @BeforeClass Annotation Example

TestNG method that is annotated with @BeforeClass annotation will be run before the first test method in the current class is invoked.

By 
Jagadeesh Motamarri
·
Sep. 28, 13 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
45.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

TestNG method that is annotated with @BeforeClass annotation will be run before the first test method in the current class is invoked.

Here is a quick example

Code

?
package com.skilledmonster.example;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Example to demonstrate use of @BeforeClass annotation of TestNG framework
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class TestNGAnnotationBeforeClassExample {
@BeforeClass
public void oneTimeSetUp() {
System.out.println("@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.");
}
@Test
public void validateSum() {
System.out.println("@Test : validateSum()");
int a = 5;
int b = 10;
Assert.assertEquals(a + b, 15);
}
@Test
public void validateDifference() {
System.out.println("@Test : validateDifference()");
int a = 5;
int b = 10;
Assert.assertEquals(b - a, 5);
}
}

Output

 testng_beforeclass_annotation 

As shown in the above console output, oneTimeSetUp() method is invoked before executing validateDifference() and validateSum() methods.

Download

 Download TestNG @BeforeClass Example
TestNG Annotation

Published at DZone with permission of Jagadeesh Motamarri. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Designing a Page Object Model + TestNG Hybrid Framework: Patterns That Actually Scale
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Quality Crisis in Robotics and How Video Annotation Fixes It
  • Code Rewriting With AI and TDD

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook