Professional Documents
Culture Documents
DEVELOPMENT
Test-Driven Development
Quotes
Kent Beck said “Test-first code tends to be more cohesive
and less coupled than code in which testing isn’t a part
of the intimate coding cycle”
“If you can’t write a test for what you are about to code,
then you shouldn’t even be thinking about coding”
TDD Overview
• Made popular by Extreme Programming
5. Refactor code
• Cleanup the code
• Rerun tests to ensure cleanup did not break anything
Repeat
Test First vs. Test Last
• Pick a piece of functionality • Pick a piece of functionality
• Write a test that expresses a small • Write production code that
task that fails implements entire functionality
• Write production code until test • Write tests to validate all
passes functionality
• Run all tests • Run all tests
• Rework code until all tests pass • Rework code until all tests pass
• Repeat
Test First vs. Test Last
TDD Benefits
• Instant Feedback
• Developer knows instantly if new code works and if it
interferes with existing code [1]
Re
is
Analys
qu
ire
m
n
en
igs
ts
n
De
Pla t io
Initial nni
ng ent a
Planning le m
Im p
TDD combines
Test
ing
it on
u a
a l
Ev
Deployment
Running Example:
Preorder Coffee with Gift Card
US and Tasks
Start with
Task 1
Further Break Down the Task
• Represent order info
• Represent gift card info
• Represent receipt info
• Cons:
• False sense of confidence?
• Non-TDD folks may not understand why writing so many
tests and not functionality
@Test The @Test annotation indicates the following method as a test method.
public void method() The following method has to be declared as public void
@BeforeClass The following static method is executed once, before the start of all tests.
public static void method() It is generally using for highly intensive operations such as DB connections.
The following method has to be declared as public static void
@AfterClass The following static method is executed once after all tests have been completed.
public static void method() It is generally using for clean-up activities such as disconnect from a database.
The following method has to be declared as public static void
@Ignore This annotation is useful when you want temporarily disable the execution of a specific test.
public static void method() The following method has to be declared as public static void
@Test (expected = Exception.class) If the method does not throw the given exception, the test will fail.
@Test(timeout=500) If the method takes longer than 500 milliseconds, the test will fail.
Annotations – Junit 4
Assert Methods & Description & Syntax Assert Methods & Description &
void assertEquals (“Message”, boolean expected, boolean Syntax
1
actual) 5 void assertNotNull(“Message”, Object object)
It compares that the given two values are the same. It checks that the object is not null
Syntax: assertEquals(“Str1 is not equal to str 2”, str1, str2); Syntax: assertNotNull(“Object is NULL! Pls check
2 void assertTrue(“Message”, boolean condition) it!”, obj);
It checks that the boolean condition is true. 6 void assertSame(“Message”, boolean condition)
Syntax: assertTrue(“Bolean expression is not TRUE! Please It checks that two objects refer to the same
check it!”, val1 < val2); object
3 void assertFalse(“Message”, boolean condition) Sytnax: assertSame(obj1,obj2);
It checks that the boolean condition is false. 7 void assertNotSame(boolean condition)
Sytnax: assertTrue(“Bolean expression is not FALSE! Please It checks that two objects do not refer to the
check it!”, val1 < val2); same object
4 void assertNull(“Message”, Object object) Syntax: assertNotSame(obj1,obj2);
It checks that the object is null
Syntax: assertNotNull(“Object is not NULL! Pls check it!”,
obj);
Test execution order • JUnit tests are running in an arbitrary order. If
you want to run your tests in a fixed order,
you should
use @FixMethodOrder(MethodSorters.NA
ME_ASCENDING) annotation. With this
annotation, you can lexically sort and test the
methods in ascending order. It is shown below
screenshot.
Annotation Example
• When you run the below code, you will see that
• @BeforeClass runs only once before the all tests started
• @Before runs before each test started
• @Test defined attached method is a test method.
• @After runs after each test finished
• @AfterClass runs only once after all tests are finished.
• In the below code, I used “@FixMethodOrder(MethodSorters.NAME_ASCENDING)” annotation and lexically
ordered test names to run the tests in lexical order.
• I mean that all test names are started with T01_…, then T02_…, then T03…
• By using “@FixMethodOrder(MethodSorters.NAME_ASCENDING)” annotation, these test cases will run in the
following order T01 → T02 → T03