Home » Software Development » Oversharing on Set Up

About Ashley Frieze

Ashley Frieze
Software developer, stand-up comedian, musician, writer, jolly big cheer-monkey, skeptical thinker, Doctor Who fan, lover of fine sounds

Oversharing on Set Up

Consider the following fictitious test fixture code:

beforeEach(() => {
   databaseConnection = openDatabase();
   inputFile = loadBigFile();
   userList = loadUserList();
   imageData = loadImageBytes();
});

If I see something like that, run before EACH test case, I have a visceral reaction. Surely we can’t need to go through this effort for each and every test?

There are a few possible root causes for the above:

  • The test fixture is performing too diverse a series of tests and needs to be broken down into multiple fixtures
  • There are some resources that are needed, but could be loaded once only for the fixture, rather than for each test
  • There are some resources that should only be loaded for the tests that use them, not the others
  • No.. this is exactly what each test needs

The last case is usually rare.

The obverse, of doing the setup in each and every test – The First and Last Rites – can be just as messy.

The focus, therefore, should be on understanding the degrees of test resource and how best to set up resources of each stratum.

Where possible, divide tests into fixtures that don’t need too large a diversity of setups. Similarly, prefer framework code to boilerplate for setting up and tidyup up test resources.

Published on Java Code Geeks with permission by Ashley Frieze, partner at our JCG program. See the original article here: Oversharing on Set Up

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Herbert
23 days ago

Ah, i see. Well tht’as not too tricky at all!