After contributing to open source projects development, I learnt that the superior quality of those products comes primarily because of the high quality unit tests those developers write. Applying same practice to enterprise software can also deliver superior quality products.
Suppose there is a function substring which returns a new string from the original string starting the index specified i.e.
public static String substring(String input, int beginIndex)
Example: substring(“unhappy”, 2) will return “happy”
Then following are the unit tests that I would write to exercise this function.
Test case #1 – Go Good
Goal: Validate the go good scenario
- Pass input string as “unhappy”
- Pass begin index as 2.
Expected Result: “happy” should be returned
Test case #2: Empty String
Goal: Validate empty String is handled gracefully
- Pass input string as “”
- Pass begin index as 2
Expected Result: “” (i.e. empty string) should be returned.
Test case #3: null String
Goal: Validate null is handled gracefully
- Pass input string as null
- Pass begin index as 2
Expected Result: null should be returned
Test case #4: Special Character String
Goal: Validate special string is handled properly
- Pass input string with special characters say: hi-!@#$%^&*()_+|”:<>?/
- Pass begin index as 2
Expected Result: “-!@#$%^&*()_+|”:<>?/” should be returned
Test case #5: begin Index 0
Goal: Validate begin index boundary condition is handled properly
- Pass input string as “unhappy”
- Pass begin index as 0
Expected Result: “unhappy” should be returned.
Test case #6: Negative begin Index
Goal: Validate negative begin index is handled properly
- Pass input string as “unhappy”
- Pass begin index as -1
Expected Result: IndexOutOfBoundsException should be thrown.
Test case #7: Large begin Index
Goal: Validate large begin Index than the string length is handled properly
- Pass input string as “unhappy”
- Pass begin index as 100
Expected Result: IndexOutOfBoundsException should be thrown.
Test case #8: Last Character begin Index
Goal: Validate boundary condition of begin index handled properly
- Pass input string as “unhappy”
- Pass begin index as 7
Expected Result: “” (empty String) should be returned
Test case #9: Concurrent Invocations
Goal: Validate there aren’t any racing condition in the implementation
- Fire 100+ threads to work on the same function concurrently.
- But each thread should be given different input parameters
Expected Result:
- All the 100 threads should return accurate results.
- None of them should result in exception.
Leave a Reply