60 lines
2.0 KiB
Java
60 lines
2.0 KiB
Java
package pckg.Backend;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import java.io.File;
|
|
import java.io.PrintWriter;
|
|
|
|
class OnlineShopTest {
|
|
|
|
private OnlineShop shop;
|
|
|
|
@BeforeEach
|
|
void setUp() throws Exception {
|
|
// 1. Eine temporäre Test-CSV erstellen, damit der Konstruktor nicht fehlschlägt
|
|
File resourcesDir = new File("resources");
|
|
if (!resourcesDir.exists()) resourcesDir.mkdir();
|
|
|
|
File testFile = new File("resources/produkte.csv");
|
|
try (PrintWriter out = new PrintWriter(testFile)) {
|
|
out.println("id,name,gewicht,preis,mwst,bestand"); // Header
|
|
out.println("1,TestApfel,0.5,2.99,7,10"); // Test-Daten
|
|
out.println("2,TestBirne,0.4,3.49,7,5");
|
|
}
|
|
|
|
shop = new OnlineShop();
|
|
}
|
|
|
|
@Test
|
|
void testCSVLoading() {
|
|
// Prüfen, ob die Produkte aus der CSV korrekt geladen wurden
|
|
assertNotNull(shop.warehouse);
|
|
assertEquals(2, shop.warehouse.size());
|
|
assertEquals("TestApfel", shop.warehouse.get(0).getName());
|
|
}
|
|
|
|
@Test
|
|
void testDecreaseBestand() {
|
|
Produkt p = shop.warehouse.get(0); // TestApfel hat Bestand 10
|
|
|
|
// Erfolgreiches Abziehen
|
|
boolean success = shop.decrease(p, 3);
|
|
assertTrue(success);
|
|
assertEquals(7, p.getBestand());
|
|
|
|
// Zu viel abziehen
|
|
boolean fail = shop.decrease(p, 20);
|
|
assertFalse(fail);
|
|
assertEquals(7, p.getBestand()); // Bestand darf sich nicht ändern bei Fehlschlag
|
|
}
|
|
|
|
@Test
|
|
void testIncreaseBestand() {
|
|
Produkt p = shop.warehouse.get(1); // TestBirne hat Bestand 5
|
|
shop.increase(p, 10);
|
|
assertEquals(15, p.getBestand());
|
|
}
|
|
}
|