Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/oussama-zbair/tdd-dojo

Test Driven Development (TDD) Practice with Java & Junit4
https://github.com/oussama-zbair/tdd-dojo

java junit4 maven object-oriented-programming tdd-java test-driven-development testing unit-testing

Last synced: 1 day ago
JSON representation

Test Driven Development (TDD) Practice with Java & Junit4

Awesome Lists containing this project

README

        

Test-driven development (TDD) is a software development approach that involves writing tests for your code before you write the code itself. This approach helps to ensure that the code meets the requirements and works correctly.

To do TDD with Java, you will need a testing framework. There are many testing frameworks available for Java, but some popular ones include JUnit and TestNG. These frameworks provide the tools and APIs needed to write and run tests for your Java code.

Here is an example of how to use JUnit to do TDD with Java:

1. First, add the JUnit dependency to your project. This will allow you to use the JUnit APIs in your tests.

```xml

junit
junit
4.12
test

```

2. Next, write a test that describes the desired behavior of the code. For example, let's say we want to write a method that calculates the sum of two numbers. The test might look something like this:

```java
import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest {
@Test
public void testCalculateSum() {
int x = 3;
int y = 4;
int expectedSum = 7;
int actualSum = Calculator.calculateSum(x, y);
assertEquals(expectedSum, actualSum);
}
}
```

3. Next, run the test to see if it fails. Since we haven't written the **calculateSum()** method yet, the test should fail.

4. Now, write the **calculateSum()** method and make sure it passes the test. The method might look something like this:

```java
public class Calculator {
public static int calculateSum(int x, int y) {
return x + y;
}
}
```
5. Run the test again to make sure it passes. If it does, then we can be confident that the **calculateSum()** method is working correctly.

This is just a simple example, but it illustrates the basic idea behind TDD with Java. By writing tests before the code itself, we can ensure that our code meets the desired specifications and works correctly.

------------

With the goal of advancing my TDD practice, I created solutions for two exercises frequently given in Java software engineering interviews, featuring a train and a restaurant scenario. The source code can be accessed for review