Created
November 17, 2023 03:11
-
-
Save heliomarpm/46a92e6275c4fed23537521bef0e5aae to your computer and use it in GitHub Desktop.
Java MVC + Repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//model | |
public class Car { | |
private int id; | |
private String brand; | |
private String model; | |
private double price; | |
// Construtor, getters e setters | |
@Override | |
public String toString() { | |
return "ID: " + id + ", Brand: " + brand + ", Model: " + model + ", Price: $" + price; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//controller | |
import java.util.List; | |
public class CarController { | |
private CarRepository carRepository; | |
public CarController(CarRepository repository) { | |
this.carRepository = repository; | |
} | |
public void addCar(String brand, String model, double price) { | |
Car newCar = new Car(); | |
newCar.setBrand(brand); | |
newCar.setModel(model); | |
newCar.setPrice(price); | |
carRepository.addCar(newCar); | |
} | |
public void removeCar(int carId) { | |
carRepository.removeCar(carId); | |
} | |
public List<Car> getAllCars() { | |
return carRepository.getAllCars(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//unit test | |
import org.junit.jupiter.api.Test; | |
import org.mockito.Mockito; | |
import java.util.List; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
class CarControllerTest { | |
@Test | |
void addCar_ShouldCallRepositoryMethod() { | |
CarRepository mockRepository = Mockito.mock(CarRepository.class); | |
CarController controller = new CarController(mockRepository); | |
controller.addCar("TestBrand", "TestModel", 10000); | |
Mockito.verify(mockRepository, Mockito.times(1)).addCar(Mockito.any()); | |
} | |
@Test | |
void removeCar_ShouldCallRepositoryMethod() { | |
CarRepository mockRepository = Mockito.mock(CarRepository.class); | |
CarController controller = new CarController(mockRepository); | |
// Configurando um mock de Car | |
Car mockCar = Mockito.mock(Car.class); | |
Mockito.when(mockRepository.getCarById(1)).thenReturn(mockCar); | |
// Executando o método que queremos testar | |
controller.removeCar(1); | |
// Verificando se o método removeCar foi chamado no repositório com o mockCar | |
Mockito.verify(mockRepository, Mockito.times(1)).removeCar(mockCar); | |
} | |
@Test | |
void getAllCars_ShouldCallRepositoryMethod() { | |
CarRepository mockRepository = Mockito.mock(CarRepository.class); | |
CarController controller = new CarController(mockRepository); | |
controller.getAllCars(); | |
Mockito.verify(mockRepository, Mockito.times(1)).getAllCars(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Repository | |
import java.util.ArrayList; | |
import java.util.List; | |
public class CarRepository { | |
private List<Car> cars = new ArrayList<>(); | |
public void addCar(Car car) { | |
car.setId(cars.size() + 1); | |
cars.add(car); | |
} | |
public void removeCar(int carId) { | |
cars.removeIf(car -> car.getId() == carId); | |
} | |
public List<Car> getAllCars() { | |
return new ArrayList<>(cars); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unit test | |
import org.junit.jupiter.api.Test; | |
import org.mockito.Mockito; | |
import java.util.List; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
class CarRepositoryTest { | |
@Test | |
void addCar_ShouldIncreaseCarCount() { | |
CarRepository carRepository = new CarRepository(); | |
int initialCount = carRepository.getAllCars().size(); | |
carRepository.addCar(Mockito.mock(Car.class)); | |
int updatedCount = carRepository.getAllCars().size(); | |
assertEquals(initialCount + 1, updatedCount); | |
} | |
@Test | |
void removeCar_ShouldDecreaseCarCount() { | |
CarRepository carRepository = new CarRepository(); | |
Car car = Mockito.mock(Car.class); | |
carRepository.addCar(car); | |
int initialCount = carRepository.getAllCars().size(); | |
carRepository.removeCar(car.getId()); | |
int updatedCount = carRepository.getAllCars().size(); | |
assertEquals(initialCount - 1, updatedCount); | |
} | |
@Test | |
void getAllCars_ShouldReturnAllCars() { | |
CarRepository carRepository = new CarRepository(); | |
carRepository.addCar(Mockito.mock(Car.class)); | |
carRepository.addCar(Mockito.mock(Car.class)); | |
List<Car> cars = carRepository.getAllCars(); | |
assertEquals(2, cars.size()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment