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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How Agile Works at Tesla [Video]
  • You’ve Got Mail… and It’s a SPAM!
  • Java String Templates Today
  • Application Architecture Design Principles

Trending

  • How Agile Works at Tesla [Video]
  • You’ve Got Mail… and It’s a SPAM!
  • Java String Templates Today
  • Application Architecture Design Principles
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. TestNG and Guice: a Marriage Made in Heaven

TestNG and Guice: a Marriage Made in Heaven

Cedric Beust user avatar by
Cedric Beust
·
Dec. 13, 10 · Interview
Like (0)
Save
Tweet
Share
8.75K Views

Join the DZone community and get the full member experience.

Join For Free

TestNG has allowed users to control the instantation of their objects for a while now (around 2007). This is enabled by the IObjectFactory interface:

public interface IObjectFactory extends Serializable {
Object newInstance(Constructor constructor, Object... params);
}

Implement this interface, let TestNG know about your implementation and whenever TestNG needs to instantiate a test class, it will call the newInstance method of your object factory. This allows for a lot of flexibility, and interestingly, this interface appeared in the TestNG distribution long before Dependency Injection became as popular as it is today.

IObjectFactory has been very useful to TestNG users throughout the years, but the emergence of Dependency Injection has made its existence even more important. More and more TestNG users want to inject their test classes with DI frameworks, and over the past few months, I have noticed a sharp increase in Guice users.

IObjectFactory obviously works great with Guice (and Hani and I documented this extensively in our book) but the increased number of questions on the mailing-list prompted me to wonder if I couldn’t make this easier on Guice users.

As it turns out, the answer is yes.

Meet my little new friend, the guiceModule attribute:

@Test(guiceModule = GuiceExampleModule.class)
public class GuiceTest {

@Inject
ISingleton m_singleton;

@Test
public void singletonShouldWork() {
m_singleton.doSomething();
}
}

And that’s it! No need for IObjectFactory or modifying your build files, everything you need is contained in this new guiceModule attribute of the @Test annotation.

Obviously, this module needs to create the necessary bindings for the @Inject annotation to work properly, for example:

public class GuiceExampleModule implements Module {

@Override
public void configure(Binder binder) {
binder.bind(ISingleton.class).to(ExampleSingleton.class)
.in(Singleton.class);
}

}

With this new attribute, using TestNG with Guice has never been easier. Try it for yourself, download the beta and tell us what you think!

From http://beust.com/weblog/2010/12/10/testng-and-guice-a-marriage-made-in-heaven/

TestNG

Opinions expressed by DZone contributors are their own.

Trending

  • How Agile Works at Tesla [Video]
  • You’ve Got Mail… and It’s a SPAM!
  • Java String Templates Today
  • Application Architecture Design Principles

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: