Writing testable code
Upcoming SlideShare
Loading in...5
×
 

Writing testable code

on

  • 636 views

Writing code in such a manner, that its easy to write tests.

Writing code in such a manner, that its easy to write tests.

Statistics

Views

Total Views
636
Views on SlideShare
636
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Writing testable code Writing testable code Presentation Transcript

  • Software DevelopEngineersQA Manual Tests TestinTest Automated Tests Tools gEngineers Magic
  • 1) Do work in constructor.
  •  Work in the constructor such as: creating/initializing collaborators, communicating with other services, Logic to set up its own state removes seams needed for testing, forcing subclasses/mocks to inherit unwanted behavior. Too much work in the constructor prevents instantiation or altering collaborators in the test.
  •  The LawOfDemeter specifies a style guideline: "Only talk to your immediate friends." Objects are passed in but never used directly (only used to get access to other objects)
  •  JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development.
  • import org.junit.*;public class TestFoobar{@Before public void setUp() throws Exception {}@Test public void testSomething () {//This piece will use asserts to verify.}@After public void tearDown() throws Exception{}}
  • Before: Hard to Test // An attempted test that becomes pretty hard/* Basic new operators called class HouseTest extends TestCasedirectly in the class constructor. {(Forever preventing a seam tocreate different kitchen and public void testThisIsReallyHard() {bedroom collaborators). */ House house = new House();class House { /* Im stuck with those KitchenKitchen kitchen = new Kitchen(); and Bedroom objects created inBedroom bedroom; the constructor. */House() { }bedroom = new Bedroom(); }}