Slides used in the February 2017 meetup of the Java Beginners Meetup in Utrecht. The covered topics were testing and TDD. Some exercises to practice are also referred.
12. Brief introduction
Writing tests to check if your code works seems good, right?
But, maybe we are only testing what we expect to happen.
8
13. Brief introduction
Writing tests to check if your code works seems good, right?
But, maybe we are only testing what we expect to happen.
What if we write our tests before we write (bits of) code?
8
14. Brief introduction
Writing tests to check if your code works seems good, right?
But, maybe we are only testing what we expect to happen.
What if we write our tests before we write (bits of) code?
Test Driven Development
8
16. Why TDD?
• No influence from knowledge gained while implementing
10
17. Why TDD?
• No influence from knowledge gained while implementing
• Immediately know if every bit of code works
10
18. Why TDD?
• No influence from knowledge gained while implementing
• Immediately know if every bit of code works
• Keep code as simple as possible
10
19. Why TDD?
• No influence from knowledge gained while implementing
• Immediately know if every bit of code works
• Keep code as simple as possible
• Make code easy to test
10
20. Why TDD?
• No influence from knowledge gained while implementing
• Immediately know if every bit of code works
• Keep code as simple as possible
• Make code easy to test
• Catch bugs early
10
23. Simple test in JUnit
import org.junit.Test;
import org.junit.Assert;
public class SimpleTest {
@Test
public void test1Returns1 () {
Assert.assertEquals("1",
getAnswer (1));
}
}
12
24. Test preparation
public class PrepareTest {
private FizzBuzz testObject;
@Before
public void setup() {
testObject = new FizzBuzz ();
}
@Test
//...
}
13
26. Fizz Buzz project
Take repository at:
https://github.com/patrickkostjens/fizz-buzz-exercise
Install and run using README
14
27. Fizz Buzz rules
• Dividable by 3? Fizz
• Dividable by 5? Buzz
• Dividable by 3 and 5? Fizz Buzz
• None of the above? Just a number
Exercise
Given a number, return the Fizz Buzz answer for that
number.
15
28. Easy, right?
Fizz Buzz seems to be very easy
yet
there are many solutions
See: https://ditam.github.io/posts/fizzbuzz/
16