Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<groupId>com.houari</groupId>
<artifactId>Calculator</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<jackson-dataformat-yaml.version>2.14.2</jackson-dataformat-yaml.version>
<junit-jupiter-params.version>5.9.2</junit-jupiter-params.version>
</properties>
<!-- H2 Database for Testing -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson-dataformat-yaml.version}</version>
</dependency>
<!-- Lombok for Cleaner Code -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter-params.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<!-- Spring Boot Test for TDD -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.calculator.controller;

import com.example.calculator.service.CalculatorService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/calculator")
public class CalculatorController {

private final CalculatorService calculatorService;

public CalculatorController(CalculatorService calculatorService) {
this.calculatorService = calculatorService;
}

@GetMapping("/add")
public int add(@RequestParam int a, @RequestParam int b) {
return calculatorService.add(a, b);
}

@GetMapping("/subtract")
public int subtract(@RequestParam int a, @RequestParam int b) {
return calculatorService.subtract(a, b);
}

@GetMapping("/multiply")
public int multiply(@RequestParam int a, @RequestParam int b) {
return calculatorService.multiply(a, b);
}

@GetMapping("/divide")
public int divide(@RequestParam int a, @RequestParam int b) {
return calculatorService.divide(a, b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.tdd.service;

import com.example.tdd.model.User;
import com.example.tdd.repository.UserRepository;
import org.springframework.stereotype.Service;
import java.util.Optional;

@Service
public class UserService {

private final UserRepository userRepository;

public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

public User createUser(User user) {
return userRepository.save(user);
}

public User getUserById(Long id) {
Optional<User> user = userRepository.findById(id);
return user.orElseThrow(() -> new RuntimeException("User not found"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.tdd.service;

import com.example.tdd.model.User;
import com.example.tdd.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Optional;

class UserServiceTest {

private final UserRepository userRepository = Mockito.mock(UserRepository.class);
private final UserService userService = new UserService(userRepository);

@Test
void testCreateUser() {
User user = new User(1L, "John Doe", "[email protected]");

Mockito.when(userRepository.save(user)).thenReturn(user);

User createdUser = userService.createUser(user);
assertNotNull(createdUser);
assertEquals("John Doe", createdUser.getName());
}

@Test
void testFindUserById() {
User user = new User(1L, "John Doe", "[email protected]");

Mockito.when(userRepository.findById(1L)).thenReturn(Optional.of(user));

User foundUser = userService.getUserById(1L);
assertNotNull(foundUser);
assertEquals("John Doe", foundUser.getName());
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/houarizegai/calculator/tdd/userModal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.tdd.model;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
private String email;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.tdd.repository;

import com.example.tdd.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "../../../../../.."
}
],
"settings": {}
}
26 changes: 26 additions & 0 deletions src/test/java/com/houarizegai/calculator/CalculatorService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.calculator.service;

import org.springframework.stereotype.Service;

@Service
public class CalculatorService {

public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {
return a - b;
}

public int multiply(int a, int b) {
return a * b;
}

public int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a / b;
}
}
43 changes: 30 additions & 13 deletions src/test/java/com/houarizegai/calculator/CalculatorUITest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
package com.houarizegai.calculator;
package com.example.calculator.service;

import com.houarizegai.calculator.ui.CalculatorUI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class CalculatorUITest {
class CalculatorServiceTest {

private CalculatorUI calculatorUI;
private CalculatorService calculatorService;

@BeforeEach
void setUp() {
calculatorUI = new CalculatorUI();
calculatorService = new CalculatorService();
}

@Test
void testAddition() {
assertEquals(10, calculatorService.add(5, 5));
}

@Test
void testSubtraction() {
assertEquals(3, calculatorService.subtract(8, 5));
}

@Test
void testMultiplication() {
assertEquals(20, calculatorService.multiply(4, 5));
}

@Test
void testDivision() {
assertEquals(2, calculatorService.divide(10, 5));
}

@ParameterizedTest
@CsvSource({"3,5,+,8", "2,8,-,-6", "44.5,10,*,445", "320,5,/,64", "3,5,%,3", "5,3,^,125"})
void testCalculation(double firstNumber, double secondNumber, char operator, double expectedResult) {
assertEquals(expectedResult, calculatorUI.calculate(firstNumber, secondNumber, operator));
@Test
void testDivisionByZero() {
assertThrows(ArithmeticException.class, () -> calculatorService.divide(10, 0));
}
}