Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Unit Testing Best Practices
Agenda
● Standards vs Principles
● Unit Testing Principles
● Let's Derive Standards
Principles vs Standards
● Principles
● a fundamental, primary, or general law or truth from
which others are derived: the ...
Principles and Standards - Example
● Principle
● Code should be Understandable
● Corresponding Standards
● Method Length s...
Standards without Principles
● Misinterpretation
● Method() with complexity 25 might be split into
Method1() - 10, Method2...
Disambiguation – Unit Testing
● In this presentation, we use the term unit testing
to refer to xUnit testing (Junit, Nunit...
Unit Testing Organization/Attitude
● More important than Code.
● Lead to Better Design (due to Continuous
Refactoring)
● B...
Unit Testing Principles
1.Easy to understand (Typical test should take no
longer than 15 seconds to read.)
2.Test should f...
Examples we would use
● Amount getClientProductsSum(List<Product>)
● For a list of products, calculate the sum of product
...
Principle 1 : Easy to Understand –
Example 1
@Test
public void testClientProductSum(){
List<Product> products = new ArrayL...
Principle 1 : Easy to Understand –
Example 2
@Test
public void testClientProductSum_AllProductsSameCurrency()
throws Diffe...
Principle 1 : Easy to Understand
● Name of the Unit Test
● Should indicate the condition being tested and (if
needed) the ...
Principle 1 : Easy to Understand
● Highlight values important to the test
– Test Setup
List<Product> products = new ArrayL...
Principle 1 : Easy to Understand
● Highlight values important to the test
– Assertions
Amount expected = new AmountImpl(ne...
Principle 1 : Easy to Understand
● One condition per test
● Results in simple code without if's , for's etc.
● If a test f...
Principle 1 : Easy to Understand
● No Exception Handling in a test method.
public void testClientProductSum_NoProducts() t...
Principle 1 : Easy to Understand
● Use annotated ExceptionHandling to test for
exceptions.
@Test(expected = DifferentCurre...
Principle 1 : Easy to Understand -
Use new features
● Compare Arrays
● assertArrayEquals(expectedArray,actualArray)
● Test...
Principle 2 : Fail only when there is
a defect in CUT (Code Under Test)
● No dependancies between test conditions.
● Don't...
Principle 3 : Test's should find all
defect's in code
● Why else do we write test :)
● Test everything that could possibly...
Principle 3 : Test's should find all
defect's in code
● Junit FAQ : Should we test setters and getters?
becomeTimidAndTest...
Principle 4 : Less Duplication
● No Discussion on this :)
Principle 5 : Test's should run
quickly
● To maximize benefits, tests should be run as
often as possible. Long running tes...
Principle 5 : Test's should run
quickly
● To maximize benefits, tests should be run as
often as possible. Long running tes...
Result : Tests as Documentation
● Well written tests act as documentation and can
be used for discussions with Business as...
Thank You
Questions?
Upcoming SlideShare
Loading in …5
×

Unit testing best practices with JUnit

341 views

Published on

Unit testing best practices with JUnit

Published in: Education
  • I am very grateful for Jeevan's program because it taught me many techniques on how to overcome common mistakes made in maths. Also, his revision strategy is unique because the same principles can be used in other subjects too and not only maths. Thank you Jeevan, your techniques are very useful! http://t.cn/AirraVnG
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Unit testing best practices with JUnit

  1. 1. Unit Testing Best Practices
  2. 2. Agenda ● Standards vs Principles ● Unit Testing Principles ● Let's Derive Standards
  3. 3. Principles vs Standards ● Principles ● a fundamental, primary, or general law or truth from which others are derived: the principles of modern physics. ● Standards ● Put at its simplest, a standard is an agreed, repeatable way of doing something. ● Usually derived from Principles
  4. 4. Principles and Standards - Example ● Principle ● Code should be Understandable ● Corresponding Standards ● Method Length should be less than XX lines. ● Cyclomatic Complexity of method should be less than YY. ● Class should not be more YY lines. ● No Magic Numbers.
  5. 5. Standards without Principles ● Misinterpretation ● Method() with complexity 25 might be split into Method1() - 10, Method2() – 10, Method3() - 5 ● Some Standards are dependent on the context ● Code like “public static int ZERO = 0” ● What if the new standards come in? ● Standards are some body's interpretation of principles and they can change ● Imagine if rule for complexity is decreased from 10 to 5
  6. 6. Disambiguation – Unit Testing ● In this presentation, we use the term unit testing to refer to xUnit testing (Junit, Nunit etc) done on individual code units (Methods, Classes). ● We do not refer to the screen integration testing performed by a developer on a web application.
  7. 7. Unit Testing Organization/Attitude ● More important than Code. ● Lead to Better Design (due to Continuous Refactoring) ● Best written before Code (TDD ). ● TDD improves Design and Code Quality ● Separated from Production Code ● Find Defects Early ● Continuous Integration
  8. 8. Unit Testing Principles 1.Easy to understand (Typical test should take no longer than 15 seconds to read.) 2.Test should fail only when there is a problem with production code. 3.Tests should find all problems with production code. 4.Tests should have as minimum duplication as possible. 5.Should run quickly.
  9. 9. Examples we would use ● Amount getClientProductsSum(List<Product>) ● For a list of products, calculate the sum of product amounts and return total. ● Throws a DifferentCurrenciesException() if the products have different currencies
  10. 10. Principle 1 : Easy to Understand – Example 1 @Test public void testClientProductSum(){ List<Product> products = new ArrayList<Product>(); products.add(new ProductImpl(100, "Product 15", ProductType.BANK_GUARANTEE, new AmountImpl( new BigDecimal("5.0"), Currency.EURO))); products.add(new ProductImpl(120, "Product 20", ProductType.BANK_GUARANTEE, new AmountImpl( new BigDecimal("6.0"), Currency.EURO))); Amount temp = null; try { temp = clientBO.getClientProductsSum(products); } catch (DifferentCurrenciesException e) { fail(); } assertEquals(Currency.EURO, temp.getCurrency()); assertEquals(new BigDecimal("11.0"), temp.getValue()); }
  11. 11. Principle 1 : Easy to Understand – Example 2 @Test public void testClientProductSum_AllProductsSameCurrency() throws DifferentCurrenciesException { Amount[] amounts = { new AmountImpl(new BigDecimal("5.0"), Currency.EURO), new AmountImpl(new BigDecimal("6.0"), Currency.EURO) }; List<Product> products = createProductListWithAmounts(amounts); Amount actual = clientBO.getClientProductsSum(products); Amount expected = new AmountImpl(new BigDecimal("11.0"), Currency.EURO); assertAmount(actual, expected); }
  12. 12. Principle 1 : Easy to Understand ● Name of the Unit Test ● Should indicate the condition being tested and (if needed) the result – testClientProductSum_AllProductsSameCurrency vs testClientProductSum – testClientProductSum_DifferentCurrencies_ThrowsException vs testClientProductSum1 – testClientProductSum_NoProducts vs testClientProductSum2 ● Keyword test at start of method name is now superfluous. (Junit 4 does not need it.) – Methods can as well be named clientProductSum_DifferentCurrencies_ThrowsException
  13. 13. Principle 1 : Easy to Understand ● Highlight values important to the test – Test Setup List<Product> products = new ArrayList<Product>(); products.add(new ProductImpl(100, "Product 15",ProductType.BANK_GUARANTEE, new AmountImpl(new BigDecimal("5.0"), Currency.EURO))); products.add(new ProductImpl(120, "Product 20",ProductType.BANK_GUARANTEE, new AmountImpl(new BigDecimal("6.0"), Currency.EURO))); COMPARED TO Amount[] amounts = { new AmountImpl(new BigDecimal("5.0"), Currency.EURO), new AmountImpl(new BigDecimal("6.0"), Currency.EURO) }; List<Product> products = createProductListWithAmounts(amounts);
  14. 14. Principle 1 : Easy to Understand ● Highlight values important to the test – Assertions Amount expected = new AmountImpl(new BigDecimal("11.0"), Currency.EURO); assertAmount(expected, actual); COMPARED TO Amount temp = clientBO.getClientProductsSum(products); assertEquals(Currency.EURO, temp.getCurrency()); assertEquals(new BigDecimal("11.0"), temp.getValue());
  15. 15. Principle 1 : Easy to Understand ● One condition per test ● Results in simple code without if's , for's etc. ● If a test fails, you would know the exact condition which is failing. ● Create useful assert methods to test the condition – assertAmount(expected, actual); private void assertAmount(Amount expected, Amount actual) { assertEquals(expected.getCurrency(), actual.getCurrency()); assertEquals(expected.getValue(), actual.getValue()); }
  16. 16. Principle 1 : Easy to Understand ● No Exception Handling in a test method. public void testClientProductSum_NoProducts() throws DifferentCurrenciesException INSTEAD OF public void testClientProductSum(){ try { temp = clientBO.getClientProductsSum(products); } catch (DifferentCurrenciesException e) { fail(); } }
  17. 17. Principle 1 : Easy to Understand ● Use annotated ExceptionHandling to test for exceptions. @Test(expected = DifferentCurrenciesException.class) public void testClientProductSum_DifferentCurrencies_ThrowsException() throws Exception { CODE THAT THROWS EXCEPTION; } INSTEAD OF @Test public void testClientProductSum1() { try { CODE THAT THROWS EXCEPTION; fail("DifferentCurrenciesException is expected"); } catch (DifferentCurrenciesException e) { }}
  18. 18. Principle 1 : Easy to Understand - Use new features ● Compare Arrays ● assertArrayEquals(expectedArray,actualArray) ● Testing Exceptions ● Annotation (exception = Exception.class) ● Testing Performance ● Annotation (timeout = 2) – 2 milliseconds
  19. 19. Principle 2 : Fail only when there is a defect in CUT (Code Under Test) ● No dependancies between test conditions. ● Don't assume the order in which tests would run. ● Avoid External Dependancies ● Avoid depending on (db, external interface,network connection,container).. Use Stubs/Mocks. ● Avoid depending on system date and random. ● Avoid hardcoding of paths (“C:TestDatadataSet1.dat”);//Runs well on my machine..
  20. 20. Principle 3 : Test's should find all defect's in code ● Why else do we write test :) ● Test everything that could possibly break. ● Test Exceptions. ● Test Boundary Conditions. ● Use Strong Assertions ● Do not write “Tests for Coverage” ● Favorite maxim from Junit FAQ ● “Test until fear turns to boredom.”
  21. 21. Principle 3 : Test's should find all defect's in code ● Junit FAQ : Should we test setters and getters? becomeTimidAndTestEverything while writingTheSameThingOverAndOverAgain becomeMoreAggressive writeFewerTests writeTestsForMoreInterestingCases if getBurnedByStupidDefect feelStupid becomeTimidAndTestEverything end End ● Remember this is a infinite loop :)
  22. 22. Principle 4 : Less Duplication ● No Discussion on this :)
  23. 23. Principle 5 : Test's should run quickly ● To maximize benefits, tests should be run as often as possible. Long running tests stop tests from being run often. ● Avoid reading from – File System or – Network ● A temporary solution might be to “collect long running tests into a separate test suite” and run it less often.
  24. 24. Principle 5 : Test's should run quickly ● To maximize benefits, tests should be run as often as possible. Long running tests stop tests from being run often. ● Avoid reading from – File System or – Network ● A temporary solution might be to “collect long running tests into a separate test suite” and run it less often.
  25. 25. Result : Tests as Documentation ● Well written tests act as documentation and can be used for discussions with Business as well. ● Examples – testClientProductSum_AllProductsSameCurrency – testClientProductSum_DifferentCurrencies_ThrowsExcep tion – testClientProductSum_NoProducts
  26. 26. Thank You Questions?

×