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

Trending

  • Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
  • Auditing Tools for Kubernetes
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  • The SPACE Framework for Developer Productivity
  1. DZone
  2. Coding
  3. Frameworks
  4. JUnit Testing Made Easier With Eclipse Templates

JUnit Testing Made Easier With Eclipse Templates

Byron M user avatar by
Byron M
·
Feb. 25, 10 · Interview
Like (0)
Save
Tweet
Share
11.93K Views

Join the DZone community and get the full member experience.

Join For Free

When you’re writing JUnit tests, you’ll often find yourself repeating the same boilerplate code. Take the following code:

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

public class EngineTest {
@Before
public void setup() {
...
}

@Test
public void test() throws Exception {
...
}

@After
public void tearDown() {
...
}
}

f you want to add a test, you have to somehow add lines 3, 11, 12 and 14 before you even get to the actual test. The same holds for setup and teardown code. We know that Eclipse can help with adding imports automatically when you save or autocomplete, but it still takes time to get everything in place.

Eclipse templates can help take away the drudgery by adding all this boilerplate code automatically. It can automatically add a test, setup and teardown methods together with calls to assertEquals, assertTrue and others. I’m assuming JUnit 4 in all the templates. If you’re new to templates, then see this tip to get an overview.

Import the templates automatically

Download the template below then import it into Eclipse.

  • JUnit_templates: This bundle includes:
    test4: Creates a test method together with the @Test annotation and its import. NB: The 4 at the end is to distinguish it from two other  Eclipse built-in templates called test and Test.
    setup4: Creates a setup method with the @Before annotation and its import.
    teardown4: Creates a teardown method (called tearDown) with an @After annotation and its import.
    asequals: A call to assertEquals with its static import. The expected an actual default to locally available objects, but can easily be changed.
    astrue & asfalse: A call to assertTrue/assertFalse and its static import. The argument defaults to the nearest boolean but can easily be changed.
    asnull & asnotnull: A call to assertNull/assertNotNull and its static import. The argument defaults to the nearest object, but can easily be changed.

If you want to create the templates manually, open the template XML in your favourite XML editor, then see this tip, taking the values from the relevant fields in the XML.

From http://eclipseone.wordpress.com

Template Eclipse JUnit

Opinions expressed by DZone contributors are their own.

Trending

  • Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
  • Auditing Tools for Kubernetes
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  • The SPACE Framework for Developer Productivity

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: