Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow

Similar presentations


Presentation on theme: "1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow"— Presentation transcript:

1 1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow mailto:carlfrs@idi.ntnu.no

2 2 Testing Not closely integrated with development.  prevents measurement of the progress of development.  can't tell when something starts working or when something stops working. JUnit to cheaply and incrementally build a test suite that helps to: – measure your progress, – spot unintended side effects. – focus your development efforts.

3 3 Testing with JUnit Automatic testing framework. – Acceptance tests. – Integration test. – Unit test. Reports the number of defects graphically. May create many tests for each method.

4 4 JUnit – Defines how to structure test cases – Provides the tools to run them. Implement a test as a subclass of TestCase.

5 5 JUnit Example Interplay of code and tests. – The style: to write a few lines of code, then a test that should run, – or even better: to write a test that won't run, then write the code that will make it run. Example: Solve the problem of representing arithmetic with multiple currencies.

6 6 Example: Money class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount fCurrency= currency; } public int amount() { return fAmount; } public String currency() { return fCurrency; }

7 7 Example: Money public Money add(Money m) { return new Money(amount()+m.amount(), currency()); }

8 8 JUnit Define MoneyTest as a subclass of TestCase. Put MoneyTest in the same package as the classes under test  access to the package private methods. – Add method testSimpleAdd, that will exercise the simple version of Money.add() above. – A JUnit test method is an ordinary method without any parameters.

9 9 Example: MoneyTest public class MoneyTest extends TestCase { //… public void testSimpleAdd() { Money m12CHF= new Money(12, "CHF"); // (1) Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); // (2) assert(expected.equals(result)); // (3) }

10 10 Developing Tests public void testEquals() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); assert(!m12CHF.equals(null)); assertEquals(m12CHF, m12CHF); assertEquals(m12CHF, new Money(12, "CHF")); // (1) assert(!m12CHF.equals(m14CHF)); }

11 11 Developing Tests public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money)anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } return false; } Override the method hashCode whenever you override method equals.

12 12 Assertions Verification by calling assert (inherited from TestCase). – Assert triggers a failure - logged by JUnit when the argument isn't true. – TestCase defines an assertEquals convenience method. Logs the printed value of the two objects if they differ. – Shows why a test failed in a JUnit test result report. Logged as a string representation created by toString.

13 13 Test Fixture public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; protected void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); }

14 14 Tests Refactored public void testEquals() { assert(!f12CHF.equals(null)); assertEquals(f12CHF, f12CHF); assertEquals(f12CHF, new Money(12, "CHF")); assert(!f12CHF.equals(f14CHF)); } public void testSimpleAdd() { Money expected= new Money(26, "CHF"); Money result= f12CHF.add(f14CHF); assert(expected.equals(result)); }

15 15 Running of Tests Two additional steps are needed to run the two test cases: 1. define how to run an individual test case, 2. define how to run a test suite. JUnit supports two ways of running single tests: – static. – dynamic.

16 16

17 17 JUnit Review (1) In general: development will go much smoother writing tests a little at a time when developing. When imagine of how the code will work, capture the thoughts in a test. Test code is just like model code in working  best if it is factored well.

18 18 JUnit Review (2) Keeping old tests running is just as important as making new ones run. The ideal is to always run all of your tests. When you are struck by an idea, defer thinking about the implementation. First write the test. Then run it. Then work on the implementation.

19 19 Testing Practices (1) Martin Fowler: "Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.” Only a fraction of the tests are actually useful. – Write tests that fail even though they should work, or tests that succeed even though they should fail. – Think of it is in cost/benefit terms. You want tests that pay you back with information.

20 20 Testing Practices (2) Receive a reasonable return on your testing investment: – During Development. – During Debugging. Caution: – Once you get them running, make sure they stay running. Ideally, run every test every time you change a method. Practically, the suite will soon grow too large to run all the time.

21 21 Testing Practices (3) Try to optimise your set-up code so you can run all the tests. Or, – create special suites that contain all the tests that might possibly be affected by your current development. – run the suite every time you compile. – make sure you run every test at least once a day: overnight, during lunch, during one of those long meetings….

22 22 References http://www.junit.org/

23 23 Questions


Download ppt "1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow"

Similar presentations


Ads by Google