Disclosure: This text could comprise affiliate hyperlinks. Once you buy, we could earn a small fee.
JUnit greatest practices in Java
One of many books which helped me loads is Pragmatic Unit Testing in Java with JUnit by Andrew Hunt and Dave Thomas, This e-book shares a number of JUnit greatest practices which can be price studying and studying if you’re critical about Unit testing utilizing JUnit.
By no means undermine the significance of automated testing and check instances written by the Developer who writes the code and that is the rationale many firms are asking programmers to design, code and supply unit exams throughout their Java interviews.
These JUnit greatest practices not simply apply to JUnit however to some other testing framework as properly like TestNG, they’re extra on testing and writing check instances that are extra essential expertise than instruments like JUnit or TestNG.
10 Important JUnit Greatest practices for Java Builders
1. All the time check Core Strategies
It is not virtually doable to get 100% code protection, so do not goal to put in writing unit exams for every methodology and trivial operations, as a substitute, write unit exams for a technique that’s prone to have bugs throughout upkeep. All the time check core methodology and core lessons that are used closely by totally different elements of a program.
In case you comply with this greatest follow whereas writing a check, you can be stunned with the standard of code, it usually leads to fewer bugs through the formal testing cycle.
2. Run the JUnit check as a part of the Construct Course of
You must Combine Junit exams along with your construct script in order that with each compile your exams run robotically. Maven and ANT two hottest construct expertise for Java functions gives assist to run Junit exams. This isn’t only a greatest follow however a regular of constructing Java functions.
All the time run check instances whereas constructing initiatives this not solely verifies new code but in addition alerts with any potential error which ends up because of latest adjustments. Typically whereas fixing a bug builders introduce one other bug when you have a JUnit check built-in with the construct or following CI practices then you might be higher ready to take care of them.
3. All the time Take a look at for boundary Situations
Develop check instances based mostly on utilization and boundary circumstances, That is my favourite Junit check follow and principally requested as an interview query on JUnit as properly. For instance, if you’re requested to put in writing a perform to switch all prevalence of a given character from String e.g.
public String change(String textual content, char ch){ …}
How will you write a check case for that? to my shock, many Java program begins specializing in JUnit check syntax like setUp() and tearDown() even earlier than considering of precise check eventualities like :
1) Take a look at for empty textual content or empty character?
2) Take a look at for the character which isn’t in String?
3) Take a look at for characters that come throughout begin, finish, or center of String?
5) Take a look at with String accommodates simply accommodates one character a number of instances?
4. Alight Take a look at with Enterprise Necessities
Be sure your JUnit check is aligned with your small business requirement specified within the BRD or Enterprise Requirement doc.
5. Checks for Non-Useful Necessities
Write a check for the non-functional requirement as properly, whereas writing a Thread-safe class, it is essential to put in writing exams that attempt to break thread security.
6. Take a look at for Ordering
If a perform or methodology is dependent upon upon the order of occasions then ensure that your JUnit check covers ordering requirement and my take is to check each side of the coin means with right ordering methodology ought to produce an accurate consequence and with incorrect ordering, it ought to throw Exception or confirms the choice operation. Another JUnit greatest follow that’s price remembering.
7. Use @Ignore Annotation
One Thought which helps me whereas writing a unit check is to create dummy exams whereas working with necessities as a result of that’s the perfect time you bear in mind necessities and having a check case with a remark that describes the intent of check enables you to to implement it later or simply put @Ignore if you happen to don’t have time to implement check and utilizing Junit4 annotations. This helps me to cowl a lot of the necessities whereas writing code in addition to check. this simply enforces you to put in writing as many check instances as outlined by necessities.
8. Keep away from testing easy getter strategies
Writing trivial JUnit exams like for getter and setter methodology is generally a waste of time. keep in mind that you do not have the freedom to put in writing an infinite variety of unit exams both when it comes to your growth time or when you are constructing your software. as unit exams run robotically through the construct course of, they’re required to complete early and trivial unit check simply provides on time and conceal extra helpful case to run later.
9. Keep away from dependence on Database and File System
Hold your Unit check unbiased of Environmental knowledge like Database, File System, and so on. The unit check is dependent upon environmental knowledge that will work in some environments and will not work on others. It is a good suggestion to make use of a fastidiously chosen set of knowledge embedded in check instances in addition to a placeholder methodology that may be plugged to the database if required utilizing configuration.
10. Use Instruments
You must at all times use obtainable instruments like DBunit, XMLUnit, and Spring check framework based mostly upon your challenge and your want.