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
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. The Difficulty With Custom @Rule in JUnit 4

The Difficulty With Custom @Rule in JUnit 4

Rob Williams user avatar by
Rob Williams
·
Feb. 17, 12 · Interview
Like (0)
Save
Tweet
Share
5.60K Views

Join the DZone community and get the full member experience.

Join For Free

@Rules are no doubt a good idea. But let me first be my usual critical self. The way they were rolled out as part of the 4.x train makes The Herminator look like Master of Festivities as Buckingham Palace. First off, what on earth is the basis of that name?? Well if you thought it was just misguided and would quickly give up its secrets in the only temple of learning [Einstein] humankind knows: example, you were wrong. Why? The only example for something like 3 releases was a TemporaryFolder. Go ahead, try and put those together in a conceptual space that is going to make sense to anyone, I dare you (that SNL skit sending up Clint‘s Super Bowl ad was pretty awesome, still on the brain).

The idea of a temporary folder, though, that you can just declare, fill with crap, and leave for someone else to cleanup is dreamy. Not only because, as Beck likes to point out, we are lazy (devs), but because the most pernicious stupidity about tests, that, unfortunately, is still as pervasive and contagious as foot fungus, is that they take time. I love tests that take no time. Mainly because they put the lie to that, but also because it keeps your focus on your own code.

I was writing some code over the weekend using neo4j (more on that) and one thing I already LOVE about that thing is they have a super simple way to make an embedded database so you can write most your code inside basically barren JUnit tests that run in milliseconds.

However, there is the small matter of setting up the db, then tearing it down, oh, and the db, in its embedded form, needs a folder.

Enter rules (read, pluggable custom resources).

First I found a bunch of people talking about making rules by implementing MethodRule, but that is now deprecated . (One of the hazards of coding by google..) But then, surprise, now I can be sunny self for a minute, I did a bit more searching to see what the successor was, got a name, searched on that and it led me to the project. Decided to clone it and look at the source. Now there are a bunch of examples, including one called EmbeddedResource. The JavaDoc, however, shows a usage scenario where you instantiate it in place and provide Before and After implementations. Damn, the sun wasn't out for long. This is when annotation-based programming becomes so stupid its not even like reverting to Structured Programming, it's going to give you Linear Programming nightmares. But, it gets worse. I figure out that I am going to make my own rule by simply extending EmbeddedResource, then setting up and tearing down the db, and starting and finishing the transaction. That all goes great, but get this: I need a temp folder, so of course, I declare a Rule for one in my Rule class and inside my Before, that folder has not been created yet! Yeah, that's right, we are in that special circle in hell that the Spring guys got trapped in with their test stuff: where you don't even know when each Before and @After is going to be called. Someone else noticed this, it was a bug that they supposedly fixed in 4.8, but is broken again in 4.10, read about it here.

This is still the right way to do this, but alas, as usual, the framework took years to grow more than one example and even then, the road to extension is a potholed, untraveled cart path.


Update: Stupid resolution #___: you really can‘t put a Rule inside a Rule. This makes me think that the one example of a neo4j Rule I found was one that simply extended TemporaryFile. I‘m old school: I hate violating the most basic rule of OO programming [Is a graphDatabase a temporary file?]. So for now, I have to have 2 Rules, and pass the folder from the one to the other. The problem with this approach is that get this: by the time @After is called on my temp db, the folder is already gone so database.shutdown() throws a file not found exception! [Insert circus music here….] I thought I had this beat because I added a getter and put logic in there that waited for the first call then created the db there, but still got the error about the folder not being ignited.

JUnit JavaDocs
Example JUnit Test
Neo4J JavaDocs

 

From http://www.jroller.com/robwilliams/entry/custom_rule_in_junit_4

JUnit

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • Building a REST API With AWS Gateway and Python
  • Integrate AWS Secrets Manager in Spring Boot Application
  • Cloud Performance Engineering

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
  • +1 (919) 678-0300

Let's be friends: