1 / 17

By Rick Mercer with help from Kent Beck and Scott Ambler

Java Review via Test Driven Development (TDD). By Rick Mercer with help from Kent Beck and Scott Ambler. Outline. What is TDD? Using Tests as documentation and explanation Assertions verify your code works Even when you change other parts of the system. Test Driven Development (TDD).

fauve
Download Presentation

By Rick Mercer with help from Kent Beck and Scott Ambler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Review via Test Driven Development (TDD) By Rick Mercer with help from Kent Beck and Scott Ambler

  2. Outline • What is TDD? • Using Tests as documentation and explanation • Assertions verify your code works • Even when you change other parts of the system

  3. Test Driven Development (TDD) • "TDD is emerging as one of the most successful developer productivity enhancing techniques to be recently discovered. The three-step: write test, write code, refactor – is a dance many of us are enjoying" • Eric Vautier, David Vydra "TDD is the Gem of Extreme Programming" • Kent Beck

  4. TDD: What is it? • TDD turns traditional development around • Write test code before the methods • Do so in very small steps • Refuse to add any code until a test exists for it • Have a failing test (red bar) before writing the code to make the test pass • You can run tests anytime • And you should do so quite often

  5. Simple, yet hard to do • When you first try TDD, it requires great discipline because it is easy to “slip” and write methods without first writing a new test • Pair programming helps you to stay on track • Williams and Kessler

  6. Comments, Documentation? • Most programmers don’t read documentation • instead they prefer to work with the code • Programmers often look for sample code that sends messages to instances of a class • Well-written unit tests can provide such a working specification of your code • more docs are necessary • Unit tests effectively become a significant portion of your technical documentation

  7. Testing Framework • An underlying assumption of TDD is that you have a unit-testing framework • Good software developers use a testing framework such as JUnit • Many exist http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks • Without a unit-testing framework, TDD is virtually impossible

  8. TDD Helps Design • TDD makes you think about what you want or need before you write the code • pick class and method names • decide the needed arguments • choose return types • write the class, constructors, instance variables, and methods to make what you want work

  9. Benefits of TDD • Benefits include • Rapid feedback • Interface before algorithm • Know when you’re finished • Change the code with confidence

  10. Principles of TDD • You maintain an exhaustive suite of Programmer Tests • No code goes into production unless it has associated tests • You write the tests first • Tests determine what code you need to write if you are programming by intention • Then you are designing as you write tests

  11. A unit test with 2 test methods importstatic org.junit.Assert.*; import org.junit.Test; publicclass BankAccountTest { @Test publicvoid testDeposit() { BankAccount anAcct = new BankAccount("Zac", 1000.00); assertEquals(1000.00, anAcct.getBalance(), 1e-12); anAcct.deposit(0.52); assertEquals(1000.52, anAcct.getBalance(), 1e-12); } @Test publicvoid testWithdraw() { BankAccount anAcct = new BankAccount("Zac", 1000.52); assertEquals(1000.52, anAcct.getBalance(), 1e-12); anAcct.withdraw(10.52); assertEquals(990.00, anAcct.getBalance(), 1e-12); } }

  12. Annotations • Annotations provide data about a program that is not part of the program itself. Uses include • Information for the compiler • Annotations can be used by the compiler to detect errors or suppress warnings@SuppressWarnings("unused") • Compiler-time and deployment-time processing • Software tools can process annotation information to generate code, XML files • Runtime processing • examined at runtime @Test

  13. JUnit Annotations • Useful annotations used by JUnit • @Testmarks a test method • @Before and @After mark methods that execute before and after every test method in the unit test @BeforeClassmarks a method that executes exactly once before any other annotated method in the unit test

  14. import org.junit.*; publicclass ShowFlowOfControl { @BeforeClass publicstaticvoid beforeAllOthers() { System.out.println("First one time"); } @Before publicvoid beforeEachTest() { System.out.println("Before"); } @After publicvoid afterEachTest() { System.out.println("After"); } @Test publicvoid test1() { System.out.println("one"); } @Test publicvoid test2() { System.out.println("two"); } } What is the output?

  15. Assertions • Useful assertions that go into @Test methods // Asserts that a condition is truepublicstaticvoid assertTrue(boolean condition) publicstaticvoid assertEquals(int expected, int actual) publicstaticvoid assertEquals (double expected, double actual, double err) // Asserts that two Object objects are equal publicstaticvoid assertEquals(Object expected, Object actual) • All assertions of org.junit.Assert can be found at • http://www.junit.org/apidocs/org/junit/Assert.html

  16. So why test code? • So we develop quality software that works • It can get you jobs • It's what good software developers do • It is very important to test your code • You can develop code in half the time • Gives you the power and confidence to refactor • To save lives and help avoid financial losses • Rick says you have to

  17. JUnit • Never in the field of software development was so much owed by so many to so few lines of code • —Martin Fowler

More Related