Write the test before the code, and let the failure tell you what to build
A blank JUnit test passes, and so does a validator hard-coded to return true, so the real work starts when a phone number without a country code slips through as valid. Software engineer Sumana Saha of Ratepay GmbH builds a Spring Boot validator one failing test at a time. What remains is where that red-green-refactor rhythm helps a team and where it starts to hurt.
Learn the red-green-refactor rules, the given-when-then test structure and where TDD falls short
Sumana Saha, a Software Engineer at Ratepay GmbH, explains test-driven development and its red-green-refactor cycle, then demonstrates it by building a phone number validator test-first in a Java Spring Boot project with JUnit 5 and IntelliJ IDEA. She links TDD to agile incremental delivery, weighs its benefits against costs such as tests tightly coupled to implementation, and recommends resources for getting started.
“So we are always here not to write garbage code. We should follow the clean code principle that how much is needed.”
“This is the fail fast approach. That means we know the test is failing.”
What you will learn
Apply the red-green-refactor cycle: write a failing unit test, write just enough code to pass, then refactor and repeat.
Structure unit tests with given-when-then and a setup method annotated with @BeforeEach in JUnit 5.
Separate production code in the main package from test code in the test package of a Maven Spring Boot project.
Use invalid and edge-case test data to expose bugs that a happy-path test misses.
Weigh TDD's costs, such as tests tightly coupled to implementation and a steep start for newcomers, against its fail-fast benefits.
Key takeaways from the speaker
Software testing automation
Microservices testing methodologies
Mockito framework
Details
Level
Intermediate
Format
Tech talk
Technical depth
Practitioner
For
Early career, Mid-career
Length
33:23
Published
June 2021
Questions this talk answers
What is test-driven development?
It is a practice where you write a test before any production code, watch it fail, write the simplest code to make it pass, then refactor. This back-and-forth loop is known as red-green-refactor.
Where did TDD come from?
It began as test-first development, and as the speaker cited, Kent Beck coined the term test-driven development in 2003.
Why are TDD and agile so often paired?
Agile assumes requirements change constantly and delivers incrementally, and TDD supplies a quality technique for that incremental development.
How should a unit test be structured?
Follow given-when-then: provide test data, call the production method, then assert on a variable named actual. Use a setup method annotated with @BeforeEach to create a fresh instance for each test.
What are the downsides of TDD?
Tests can be tightly coupled to implementation details and fail unexpectedly when code is refactored, and newcomers need deep knowledge of the internal architecture. Unspecified features can also be missed, so good documentation of use cases helps.
About this talk
TDD is a design technique that guides the development of software through testing. With TDD, test cases are written before the code which they test, in a technique called test-first programming, which forces the developer to think about what could possibly go wrong even before the implementation itself begins. Experiments and observations in the industry have shown that TDD increases code quality and reduces defect density.
In this session, we will see how exactly we can achieve this design technique for a working software / API. The idea behind Red-Green-Refactor will be discussed and shown with examples.
Prerequisite : Basic programming knowledge in Java, IntelliJ IDEA, Spring Boot
Speaker 1: I am Sumana Saha. I'm here to present a topic which is very attractive at this moment, a software development department, which is a test-driven development. And I'm here to talk about something about the TDD and how we can do it. Speaker 1: My name is Sumana Saha and I am a software engineer. I work at Ratepay GmbH. It is located in Berlin, but basically I'm from India and I have done my bachelor's. Currently, I'm doing my master's in software engineering. I'm really passionate about clean code and writing microservices and various type of programming and stuff like that. So if you're really interested to talk to me, you can Speaker 1: Just ping me in any kind of social media like LinkedIn. I have Twitter. I have so after the session, I can share my my contact details as well. So you can. Yeah, yeah. Happily share a connection request to me a fun fact about me that I'm a very much a cat lover person and not so fun fact. It's like I'm a perfectionist. That is like sometimes it's difficult to difficult to mitigate my own expectations. So it's it's really it's really the the
Speaker 1: Yeah, the quality I have right now. Okay. And now what is the agenda for today? Speaker 1: First of all, I would like to ask you that to you anybody have experience with TDD or do you know what is actually a TDD a lot? If you can just drop a message, it will be really beneficial for me to understand. Like, do you have any idea? What is it and how to do it and stuff like that? Thank you, Irina for writing. Yes, Speaker 1: Yes, so I think you have the understanding. Yeah, your career as well. So today I can share I can share the rules and I would like to show a simple, simple, very simple way to write the test cases using TDD and how we can actually do it. And so don't I mean without wasting any time. This agenda is like I'll talk about a history. I'll talk about what are the connection with a jail. I'll talk about a
Speaker 1: Yeah, I'll talk about a practical approach, then the pros and cons, what people say about TDD and then some useful resources you can look up look up to later on. Okay, first of all, what is TDD? TDD means that the abbreviation stands for trace driven development and how it works. First, we write test. That means what exactly we write test. There is a requirement. Let's say you are Speaker 1: You are asked to do, for example, phone number validation. So what exactly you do. So you you first start with writing tests. So in a traditional way, as a developer, I have seen the other side of the picture that we tend to write the productive code, but no here in this approach, we will write the test first. So what what will happen? So when we write test, it will show us a lot of errors. The test will fail because there is no productive codes are written. Then we will write test.
Speaker 1: Then we will go to write the productive code. So now when the productive code is written. So now when the productive code is written, the very, it's a very, very, very good. Speaker 1: So when the productive code is written. So when we write test, it will show us a lot of errors. The test will fail because there is no productive codes are written. Then we will go to write the productive code. So now when the productive code is written, the very, I would say very.
Speaker 1: Simple simplest, like just to pass the test case, then we run the test case. Then it passes. Now we go to the test class again to refactor the test. And then we see the test is keep passing. That means whatever productive code we have written is good to go. Then again, we write more test. Then suddenly the tests are failing. Then we go to the productive code. This is the Speaker 1: Back and forth back and forth process is actually the main essence of the test practice. So it is not a, not a, you'd say a theoretical topic. It's very much hands on. And we should really like to, I mean, we should all try to do at least once how exactly it works.
Speaker 1: And today I'll show some, I'll show a real life code example for my previous example, which is the phone number validation for, for explaining how exactly it works. Speaker 1: A brief history. So it is, it is like a, not that we are talking about EDD very soon. I mean, or very recently, it already originated in 20th century. And initially it was named as a.
Speaker 1: Test first development, but in 2003, Mr. Kent Beck, who is an American software engineer, he actually coined the term as test driven development stuff. So he is the person who wrote many books on that. And there are also a great work by him on this, this area. Nevertheless, let's move forward. Apart from the history, we have the agile connection. So, so why people always Speaker 1: Always tie this to work together, agile and TDD, because agile tells us that every requirements are changing constantly, and we should be able to keep that development up so that we can provide the software according to the need. So it is called the incremental development process. It is a methodology. It is not that it's a, it's a one time thing.
Speaker 1: And one project is done that it's finished. It is a practice like, like, like, like we do many practices in our life. For example, we get up every day, we brush our teeth and so on, so on, so forth. So this is also a practice that we also follow in terms of software development process. Speaker 1: So what TDD provides in this agile picture, it actually provides a quality development technique. That is when a software is written, or the way software is written, how the quality is assured. This is a technique always, I mean, when it is supposedly to be used in this agile methodology, but all the developers are not always all the time doing that. We will see what we do.
Speaker 1: We will see why and why not. Another point is like the test case analysis should always be done before the writing of the test cases. I have shared, I have showed a small picture of a penguin. He's Kowalski. If you know the animations movie Madagascar, he's also a character from the movie. So he's always doing the analysis. It's a very, very crucial part when we are actually writing the test Speaker 1: I'm talking about the test cases and talking about the test cases and so on and so forth. Then when we are talking about agile, always we are thinking in a, in a way that how we can produce a minimum viable product with in, with in a sprint. So when I talk about sprint, I talk about a framework, for example, Scrum or Kanban, etc. So it's always like to provide a software, which is, uh,
Speaker 1: which is, uh, doing the happy path and, uh, which is using the TDD and, um, this is a great, uh, approach to actually, uh, uh, see a problem and provide a solution. Speaker 1: Working software is a primary measure of the progress. And the TDD is actually provides the incremental developments, which always goes hand in hand with agile. Here. Um, this is the point where we would like to go for, uh, in depth, like, uh, um, this is the point where we would like to go for, uh, Speaker 1: In depth, like, uh, a turning point where we will see soon the, uh, real life problems and how we actually write test cases. So here, moving forward, the practical approach, the rules, there are rules. This is something like we should follow the guideline. And then we.
Speaker 1: Jump to write the test cases. It is not that always we think, okay, this is the test, how I should write it and blind. We have rules. The first rule is. Speaker 1: We have to write a single unit that is describing the aspect of a program. For example, I mentioned phone number validator. We are not talking about the whole project. We're talking about a particular feature. That feature is, is.
Speaker 1: Provided or current, uh, covered with unit test. So here we shall start writing a unit test. Speaker 1: When the test is failing, obviously we have not written any productive code. The program will fail. Speaker 1: Now we will just write enough code to pass the test. Then we will refactor. This is also known as red green refactor. You have.
Speaker 1: You have seen many, um, many books and many posters. This is what is red green refactor. That means when we write a test, it will fail. Speaker 1: We write the productive code. It will pass. Then we go in a refactoring mode to provide more functionality and better test coverages. So this is a sorry, a back and forth approach. And it follows in a circle.
Speaker 1: Okay. Now, obviously we have to repeat so that we know how exactly it is happening. Let's look at a real life problem. Speaker 1: Before that, we have to use some tools to prove you to, uh, to write test driven development. I am using Java. Speaker 1: You can use any language, but I have seen that most libraries are available for this.
Speaker 1: You need test cases with Java are really good. For example, Moquito is one. J unit five is one. Um, I'm using intelligent idea because I feel, uh, it has a lot of great, uh, I would say test library support. And the overview is really nice. And look and feel is nice. And a lot of cool tips and tricks are there.
Speaker 1: I'm using spring part project with Maven right now to show you the example, you can use any other build to like grid and et cetera. So it is up to you what you would like to use, but, uh, this is a demo where, uh, I'm using this, this options to show you right now. Speaker 1: And, uh, just let's look some, look, some real quotes here. So what do I see? You are seeing an application, which is, which has a name, TDD Java example application.
Speaker 1: Which I created with Spring Boot and it has a bare minimum, just a structure. Nothing is written. So this class you are looking at, it is the main class of the Spring Boot application. And, uh, now going forward, I will try to show you how we can start, uh, the, uh, practice TTD here. Speaker 1: So we would like to start with the test class. So, as I mentioned that I would like to, uh, uh, uh, cover the phone number validation function.
Speaker 1: So I have created a package in this, in this, uh, project structure, you can see both the productive part and the test part. Speaker 1: So you see the productive part is written in the main and the test part is written in the test sub package. So this is a clear separation for any Maven projects. If you are starting on.
Speaker 1: Then, uh, I would like to read a class. I have created a, uh, selected a name called phone number validator test. And now my class is ready. Speaker 1: So here I have written a goal because it gives me a clarity. What I should, what I am going to do here. And, uh, my goal is to create a phone number validator function. So now what I shall do, I shall write a method, which is.
Speaker 1: Annotated with test here. The test annotation is coming from the J unit library. So you see the import or J unit Jupiter API test here. Um, you see that I just, uh, mentioned only a name that test underscore phone number validation function. It has nothing. Speaker 1: So ideally it should pass because I haven't provided anything at all.
Speaker 1: As you see, when I am running the application, I see the test is passed. Now I have to obviously, um, this is not my goal. Just to write a blank test. I have to provide a phone Speaker 1: number validation function. So how to do that. I have created a phone number validator object. So here you see that there is no phone number validator at all present in my project. So I would like to, so either in this situation, my test is failing because you see, there is a red bar in IntelliJ is telling you that no, no where it is present. So my test is failing here.
Speaker 1: So now I would like to create the validator class where I should create, I should create in the productive section, which is a main. So you see the target director is very important here that you should choose the main, not the test going forward. Speaker 1: I have one of our class, but it has nothing. So my code should do nothing. I mean, the, the test is actually doing nothing now to move forward.
Speaker 1: What I should do, I should refactor the test and to provide the validation function and try to see whether my test is passing or failing. And you see that I have created a setup method to provide, um, a new instance of this object. And what is actually doing? Speaker 1: The annotation before each, it is actually setting the stage before running the test. So every method is annotated with test.
Speaker 1: It will look where the before each is located and it will try to get the settings configuration, blah, blah, and then try to run the test. So here I would like to create a new instance and then like to run the test. That's the reason. Speaker 1: I created a new instance in the setup method. And this is a general, general, um, standard that you should write, uh, the name setup. It's not like, uh, whatever configure or et cetera. So this is a very, um, up to the date approach. Like you should write that way.
Speaker 1: Then, oops, then, um, I would like to refactor the test method. What I should do. So the general process of writing test is you should provide a test data. You should do something. That means you should call your productive method and then you compare the results. So here the, the, the good practices as are like, you should like, uh, do given when then this is a very, uh, Speaker 1: I would say cool approach to write or structured these test method in that way, given when then approach. So here I have given a phone number test data, whatever be it. Now in the when section, I would like to validate the test data, but when I'm calling the function validate, you see that it is read that IntelliJ cannot recognize that what exactly the validate is for. So it is throwing an error. So what I should do now, I should go to the productive class, which
Speaker 1: Is a phone number validator class and then create this method. So here am I, I have done that. So I have by default set it as written true. You can say it as anything like as I have, I have written type true or false. You can set anything. So here as my productive code says, whatever string I'm giving in this function, I will return true. Let's look at the test class and see whether this test is passing or not.
Speaker 1: I'm sure it will pass because I have, I have nothing concrete validation written here. So like you have seen that I have added the when part. Now I have to add in the then part. So what I'm writing in the then part, like I'm just trying to get the actual result and expectation. So here, this is a very, uh, standard notation to, to get the method or the variable name. Like here, what I have used is actual.
Speaker 1: Actual, actual, uh, should be used where you are expecting something and you are, you are, I mean, not expecting, you are trying to measure the data, the result. So you call the actual, when the productive method is actually calling something, I mean, giving you some data, and then you are asserting the actual to test the data.
Speaker 1: So the assert, uh, uh, I mean, statement is actually trying to get whether the actual is true or not. But in this case, it will be true because I have nothing in the productive code. So I will run the application and then my test is passing. Okay. Now this is not an end. Our main goal is to create a phone number validated function. This is not that this is a something like a structure or a, or a base I have created. Now what?
Speaker 2: Now I have added a functional functionality, which is the phone number should be greater than, uh, the size of the phone number should be greater than 10. If not, it will return false. If yes, it will return true. Now let's look at the existing code base, uh, existing test code. And try to understand whether it shall pass or not.
Speaker 1: So according to my test case, if I run it, it will pass because I have already given a phone number, which is, which is more than 10 characters. So it's, it's totally fine here, but it is nothing end here. So what are the good thing and bad thing for, for, for way of, of writing this, this methods or this test cases, which is, which is, we are not here to only checking that which Speaker 2: Passing which are, I mean only passing here. So we are here to see how many, I mean, what are the invalid days data and how we can mix and match data and provide input and then test.
Speaker 1: Now I would like to provide some invalid test data. Let's say my, my taste data is having less than, uh, length is less than 10. And what I have done that I have, I have, I have, um, uh, added a invalid test data. I have added a web test data. I have added a web test data.
Speaker 1: Then section where my actual test data are actually validating. It is the, uh, method, uh, the variable name is actual for invalid test data. And then I'm here asserting it. So what will happen? Ideally, it should also pass because I'm asserting it to fall. That means it is correct. Speaker 1: My test is correct. That it is, uh, not a valid phone number. Let's see. Yes, it is passing. And now we are in a state.
Speaker 1: It's like, we shall always, we shall always go to the productive code and refactor and then come back to the test code and try, keep on trying running the test so that we can get the, uh, idea of how exactly the code should look like and how the method should look like. It is not that we are just writing.
Speaker 1: I mean, huge set up functionalities before in the, in the productive class, and then try to mix and match test classes. So we are always here not to write garbage code. We should follow the clean code principle that how much is needed. We should write that nothing extra. So it will help all developers to save their time.
Speaker 1: To debug and whatever manual testing and everything. So here it is a very, I would say, um, effective tool to, to start with a development in, in this way. Let's move forward now. So now I would like to do something. Speaker 1: Like I have given a, I have given a, uh, new, um, data is data, which has something like I have, I would like to see the weather.
Speaker 1: My productive code shall handle, uh, this kind of data or not like without country code. So I have not given any country code. I just give any simple data. Speaker 1: Now what I'd like to do, I would, I have done when that means what my, um, uh, phone number validator should provide me as a result. And then I shall assert it as a true.
Speaker 1: So as of now I expect it should be also true. That means it is valid, but actually in ideal case, it is not valid. That is our failure that this. Speaker 1: In this situation like this, uh, whenever this, this point, like, um, uh, wrong data is getting true. That is a bug here in bug in our code. So to solve this case, we would like to go to the productive code and write the clause.
Speaker 1: Uh, phone number is starting with, let's say plus four, nine example. That's a simple example. If yes, then only I shall pass. Otherwise not pass. where the phone number is starting with, let's say, plus 49. Example, just a simple example. If yes, then only I shall pass, otherwise not pass. Moving forward, here, I have just said that when I have a phone number, which is not started with plus 49, so it shall provide me, I would say, the validation, which is false.
Okay, what I can say that I missed to add one screenshot where this one, that the SRT, the without country code is true, is failing. So here, the point is what I missed, that when I run this code, I will see that my test cases are failing. And to make good or to make it pass, I have to go to the productive code and add the functionality.
And then if I go to the test cases and then run, I should be seeing that the code is getting executed properly without any error. So maybe I will update my, I would say, PPT later on and can share. But this is the goal where we should do that, keep on refactoring the productive code and trying on test and then back and forth option.
So this is the main essence of the TDD in the real life. But I would say this is a very, very simple program. I'm pretty sure that you are doing or anybody has TDD experience, has a lot of great, I mean, features and everything. So I would be really happy that, yeah, if you could share your experiences writing test classes and stuff like that with a more complex structure and so on and so forth.
So it will be great discussion for us here in a, I'd say, in a software development area. Nevertheless, so now I would like to share some pros and cons for TDD. So let's start with the pros, which is like, it is a very efficient tool to find errors and the problems. The thing is like, as I showed that the phone number has, without country code, I know that my code has not implemented anything.
So I know this test should fail. This is the fail fast approach. That means we know the test is failing. I understand. So now I will make the better code so that the test is passing and write just enough code, not super extra or stuff like that. For doing the TDD, it is required to knowledge for internal software under test, which is beneficial.
And it is actually for thorough testing. We know that it helps us always finding hidden errors and always say that, like whenever we have this kind of validation methods, it's really, really helpful for all the developers who are following this approach because we think where the problem occurs, where will be validation fail. So that is a great tool here.
Now, programmer introspection is something like always we, as a programmer, we look for, I mean, we have our detective eye in terms of debugging the code, writing the code. But nevertheless, here we are always providing a lot of great introspective view in terms of the TDD perspective. It's always a great tool to optimize our code. That means I mentioned that we are here a fan of clean code, not extra code we should write and stuff like that.
And there are internal knowledge is required so that we should provide the maximum code coverage. Now, I'd like to share some negative part of the TDD, which is that who, as a developer, let's say newbie who has joined a team, he or she needs to provide, or I mean, needs to get the code access. Then it requires high-level knowledge of internal architecture, how exactly a software is working.
Then the test cases are really tightly coupled to the implementation details. And then sometimes it fails for like unwanted test failures when the code is refactored. And also that it is might, I mean, the negative part is sometimes it is missing the feature. That means unimplemented code has not been found or seen in this approach. So there should be somewhere good documentation so we can provide that these are the found use cases are provided and then people can actually start this, follow this TDD approach and then coding.
Now, moving forward, I would like to share some user survey. That means what people think about this approach and how it is, how much accepted is this approach by a software developer community? As you see that this TDD approach is actually, I mean, is having a lot of matrices. That means whiteboarding, like how we are thinking to draw a use case, something like that.
How the diagram or there are a lot of tools which are provides diagrams, UML tooling and stuff like that. So how it can help us. Then text-based approach and then electronic approach and then modeling tools. So every, I mean, every aspect it has there its presence. And in my experience, I have seen that not everyone is very much fan of TDD.
There is a reason is like they, I mean, as a developer, we would need to think everything first, then write the code. It is difficult. I mean, it is difficult sometimes, but I think that it is great opportunity to start. Why not? Because we will face no debugging at all. That means normally when a software is running, we have bugs.
So always we need to debug the software to find the bugs and stuff like that. So here we shall like never, never, ever find a bug if we try to follow this approach. And it's really difficult at beginning, but I can share from my experience, it is very beneficial when you are already hands-on and initially it is tough.
And I'd say you should take guidance and you should read proper documentations and maybe you can share, look some videos and stuff like that. I have some resources and books to share. So there are two books I would refer. First one is Test-Driven Development with Java. And the second one is by the inventor who is Ken Beck.
And these two books are really good, but sometimes books are a little bit boring because they are all we have to read and read and then code. But nevertheless, there is a video course, very helpful, which is Clean Code with Java, Learn Simple Design and Refactoring and TDD, which is done by In28 Minutes officially in Udemy. You can get it.
It is not free, unfortunately, but you can get it in 10 euro or something or any other currency when it is sale because it is really useful. Then the blog, I followed this blog from Martin Fowler. He is a renowned software engineer and he has done a great work in this software engineering development perspective. So you can look up this kind of resources for more information and stuff like that.
And if you'd like to join me in any discussion with TDD, I'm really happy to help you out. And I'm really open. I am sharing my GitHub and LinkedIn addresses so that you can, if you would like, you can connect with me and for the future, yeah, references, for example. This is my LinkedIn profile. This is my GitHub profile.
I'm sharing just right now. And, yeah, I'll share this presentation and this code base in GitHub after the conference. if you feel that there is a, yeah, you'd like to connect, you can connect anytime. I will be really happy to join you. And now, now it's your turn and I will be answering all your questions. So, first of all, it's the stage is yours.
If you have any questions, let me know and how you feel about this presentation. Also, let me know if you feel that there is a improvement, chances of improvement. Also, please let me know. I'll surely work on those topics. So, yeah, now the stage is yours. Please let me know your questions. I'm looking at the chat and unfortunately, I can't see any questions.
So, yeah, I will wait for some time. Yeah, this is awkward silence. I believe that everyone is very busy or maybe looking at other conferences. I'm sure there are great conference topics and stuff. So, just, you can just put like at least some, some comments that what exactly you felt. Yes, Zalak, you can get the recording for the session.
I think it will be available in a month or so because there are a lot of sessions and the Women Tech Network team, they are working really hard to provide the recordings via YouTube link. So, it is public. For example, you can go in the Women Tech Network YouTube channel and you can find the last year's old conference topic.
So, this is the YouTube channel. You can go and you can, you can see there are, there are already uploaded sessions. Thank you, Marcia. And, I would, yeah, I'm really happy that you, you felt that the topic is really clear to you. So, I, if you, if you like, you can connect or, yeah, I'm really happy if you feel that is, this, this topic and my presentation is useful for you.
It's really great. Thank you, Zalak. Thank you so much for your comment and thank you, Rimal. and, yeah, it's very nice to see you joining my session and, yeah, it's really nice. Thank you very much, everyone.
Transcribed automatically; names checked against the speaker record. Select a time to jump there.
Article based on this talk
How do you practise test-driven development on real Java code?
Sumana Saha builds a phone number validator one failing test at a time and is candid about where TDD gets hard.
In short
Test-driven development (TDD) means writing a failing unit test before any production code, writing just enough code to make it pass, then refactoring, in a repeating loop. Sumana Saha, a Software Engineer at Ratepay GmbH, shows that loop on a small Spring Boot phone number validator in Java. She argues the habit is hard at first but pays off in cleaner code, while being frank about where it falls short.
Key takeaways
Software testing automation
Microservices testing methodologies
The Mockito framework
Test first, then code, then refactor
Saha opens with the core loop. Given a requirement such as phone number validation, developers usually reach for production code first. In TDD the order flips: the test comes first, it fails because nothing exists yet, and only then is the simplest production code written to make it pass. The test is refactored and run again, new tests are added, something fails, and the cycle restarts.
She stresses that this is a skill to practise rather than a theory to memorise: "So it is not a, not a, you'd say a theoretical topic. It's very much hands on." (3:33)
She traces the idea back to test-first development and, as she cited, to Kent Beck, who coined the term test-driven development in 2003. She then ties it to agile. Requirements change constantly, agile answers with incremental delivery inside sprints run with frameworks such as Scrum or Kanban, and TDD supplies the quality technique for that incremental process. Test case analysis, she adds, should always come before writing the tests.
The rules of red, green, refactor
The practical approach rests on a few rules. Write a single unit test that describes one aspect of the program, such as the phone number validator rather than the whole project. Watch it fail, because no production code exists yet. Write just enough code to pass. Then refactor to add functionality and better coverage, and repeat.
"This is what is red green refactor. That means when we write a test, it will fail." (9:09)
For tooling she uses Java with JUnit 5 and Mockito, IntelliJ IDEA for its test support and tips, and a Spring Boot project built with Maven. She notes that Gradle or another language would work just as well.
A phone number validator, one failing test at a time
The demo starts from a bare Spring Boot application. Saha creates a phone number validator test class in the test package and shows that an empty method annotated with @Test passes, which proves nothing. Referencing a validator class that does not exist yet turns IntelliJ red, so she creates the class in the main package, keeping production and test code clearly separated as Maven projects expect.
She then adds a setup method annotated with @BeforeEach to create a fresh validator instance before each test, and structures the test method as given, when, then: provide test data, call the production method, and assert on a variable named actual. A validate method hard-coded to return true passes, which she treats as scaffolding rather than a result. Adding a length rule, then invalid data shorter than ten characters, then a number without a country code shows how each new case drives a change in the production code. The country-code case exposes a bug, because invalid data is reported as valid, so she adds a check that the number starts with +49 as a simple example.
Throughout, she argues for writing only what the tests demand: "So we are always here not to write garbage code. We should follow the clean code principle that how much is needed." (19:43)
What TDD gives you, and what it costs
On the plus side, Saha calls TDD an efficient way to find errors and problems early. Because she knew the country-code rule was not implemented, she expected the test to fail: "This is the fail fast approach. That means we know the test is failing." (24:09) She also credits it with thorough testing that surfaces hidden errors, with encouraging programmer introspection, and with pushing toward high code coverage without extra code.
The costs are real. A newcomer to a team needs high-level knowledge of the internal architecture before writing useful tests. "Then the test cases are really tightly coupled to the implementation details." (26:04) That coupling can cause unwanted test failures when code is refactored, and the approach can miss features that were never specified, so she recommends good documentation of the agreed use cases.
Getting started despite the learning curve
Saha acknowledges that not every developer is a fan, because thinking everything through before writing code is difficult. Her view is that the effort reduces debugging later, and that guidance, documentation and videos help newcomers through the early phase: "And it's really difficult at beginning, but I can share from my experience, it is very beneficial when you are already hands-on and initially it is tough." (28:09)
She recommends the book Test-Driven Development with Java, Kent Beck's book on the subject, the Udemy course Clean Code with Java, Learn Simple Design and Refactoring and TDD by in28minutes, and Martin Fowler's blog.
Watch the talk
Test Driven Development (TDD) in practice, by Sumana Saha, 33 minutes: https://www.womentech.net/video/test-driven-development-tdd-in-practice
About the speaker
Sumana Saha is a Software Engineer at Ratepay GmbH in Berlin, with around six years of experience as a software developer. She currently develops microservices with Spring Boot, Apache Kafka, Docker and Java 8.
Questions this talk answers
What is test-driven development?
Where did TDD come from?
Why are TDD and agile so often paired?
How should a unit test be structured?
What are the downsides of TDD?
Drafted with AI from the talk transcript. Editor review pending.
Frameworks and ideas mentioned
Test-driven development (TDD)
Red-green-refactor
Given-when-then
Agile
Scrum
Kanban
Clean code
Resources mentioned
Test-Driven Development with Java (book)
Kent Beck's book on test-driven development
Clean Code with Java, Learn Simple Design and Refactoring and TDD (in28minutes course on Udemy)
Martin Fowler's blog
Cite this talk
Sumana Saha. "Test Driven Development (TDD) in practice." WomenTech Network, 2021. https://www.womentech.net/video/test-driven-development-tdd-in-practice