Table of contents
1. Introduction
2. Test Driven Development
3. Unit testing in Java
4. Exercise 1
5. Exercise 2
1
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
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
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
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
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
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
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
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
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
Tools and frameworks
Frameworks:
• JUnit
• TestNG
Tools:
• Mockito
• EasyMock
• Powermock
• Hamcrest
11
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
Test preparation
public class PrepareTest {
private FizzBuzz testObject;
@Before
public void setup() {
testObject = new FizzBuzz ();
}
@Test
//...
}
13
Fizz Buzz project
Take repository at:
https://github.com/patrickkostjens/fizz-buzz-exercise
Install and run using README
14
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 thatnumber.
15
Easy, right?
Fizz Buzz seems to be very easy
yet
there are many solutions
See: https://ditam.github.io/posts/fizzbuzz/
16
Bowling scoring system
• No project setup available
• Don’t build an interface
• Start simple!
• Try to do TDD
17