Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test-Driven Development - How to write a test before none of implementation code exists

I'm learning TDD but struggling to adopt it as it's not straightforward.

The question I cannot answer is "How to write a test before any of implementation code exists?".

If our target class / target method / target parameter type / target return type don't exist,

  • What do we refer to while writing code in the test. How do we start writing the test?
  • How'd the test fail if all we could write is just the test method name before the actual implementation code?

Everybody tells WHY but not HOW

I've tried my best to find resources that elaborate on writing tests before production code but, assuming that I missed good resources, most of them are filled with cliches explaining why TTD is important than focusing on practises to adopt it.

An example use-case.

Let's assume that we are developing a software for a University and our use-case is course registration.

To keep it simple, let us confine this discussion to

  • scenario : "A Student can enroll in a maximum of any 3 courses per semester"
  • testing service layer and dao layer.

Pseudocode

ENROLL(studentId, courseId)     //check if student enrolled in less than 3 courses in the same semester as given courseId belongs in.     //if yes, enroll him/her.     //if not, return an error. 

The actual implementation of above could span a couple of classes involving services, daos, etc.

Please could you explain how to test-driven-develop it step by step? If you were to implement this using TDD, how'd you do it step-by-step.

I am hoping that this could aid many struggles like me in the future.

like image 933
phanin Avatar asked Dec 25 '13 03:12

phanin


People also ask

Why does TDD want you to write the tests before the code?

The simple concept of TDD is to write and correct the failed tests before writing new code (before development). This helps to avoid duplication of code as we write a small amount of code at a time in order to pass tests.

What do you call the practice of writing a test before the code is returned?

Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against all test cases.


1 Answers

Create EnrollingServiceTest class in src/test/java in the same package as EnrollingService

class EnrollingServiceTest {     private EnrollingService enrollingService;      @Before      public void init() {            enrollingService = new EnrollingService();     }      @Test     public void testEnroll() {            boolean result = enrollingService.enroll(1l, 1l);            assertTrue(result);     ... 

IDE (I am assuming you are using IDE) shows errors - EnrollingService does not exists .

Point cursor on EnrollService - IDE will offer to create a class - let it create in src/main/java

Now IDE says that enroll(long, long) method is missing - let IDE create it for you.

Now IDE shows no errors. Run the test - it fails. Go to enroll and start implementing the logic

And so on...

like image 77
Evgeniy Dorofeev Avatar answered Oct 08 '22 07:10

Evgeniy Dorofeev