Test für User und WetterService
parent
8540dc08e8
commit
04669ca008
|
@ -13,15 +13,9 @@
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>5.8.1</version>
|
<version>4.13.1</version>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
|
||||||
<version>5.8.1</version>
|
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -34,6 +28,25 @@
|
||||||
<artifactId>commons-codec</artifactId>
|
<artifactId>commons-codec</artifactId>
|
||||||
<version>1.15</version>
|
<version>1.15</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>5.9.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.9.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- JUnit Platform Commons -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-commons</artifactId>
|
||||||
|
<version>1.9.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
package domainTest;
|
||||||
|
|
||||||
|
import domain.Auto;
|
||||||
|
import domain.Ort;
|
||||||
|
import domain.User;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class UserTest {
|
||||||
|
|
||||||
|
private User testUser;
|
||||||
|
private final String testFilename = "users.txt";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
Ort ort = new Ort(12345, "Teststadt");
|
||||||
|
Auto auto = new Auto("Testauto", 0.12);
|
||||||
|
testUser = new User("testuser", "password", ort, auto, 80.0, 15.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
// Löschen der temporären Testdatei nach jedem Test
|
||||||
|
File file = new File(testFilename);
|
||||||
|
if (file.exists()) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegistrieren() throws IOException {
|
||||||
|
// Führe die Registrierung durch
|
||||||
|
testUser.registrieren();
|
||||||
|
|
||||||
|
// Überprüfe, ob die Daten korrekt in die Datei geschrieben wurden
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(testFilename))) {
|
||||||
|
String line = reader.readLine();
|
||||||
|
assertEquals("testuser," + testUser.getHashedPassword() + ",12345,Teststadt,Testauto,0.12,80.0,15.0", line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEinloggen() throws IOException {
|
||||||
|
// Vorbereitung der Testdaten in einer temporären Datei
|
||||||
|
try (PrintWriter writer = new PrintWriter(testFilename)) {
|
||||||
|
writer.println("testuser," + testUser.getHashedPassword() + ",12345,Teststadt,Testauto,0.12,80.0,15.0");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Einloggen des Users
|
||||||
|
assertTrue(testUser.einloggen());
|
||||||
|
|
||||||
|
// Überprüfe, ob die Userdaten korrekt gesetzt wurden
|
||||||
|
assertEquals("Teststadt", testUser.getHeimatstandort().getOrtName());
|
||||||
|
assertEquals("Testauto", testUser.getAuto().getName());
|
||||||
|
assertEquals(0.12, testUser.getAuto().getCO2AusstossProKm(), 0.001); // Delta für Genauigkeit
|
||||||
|
assertEquals(80.0, testUser.getDurchschnittsgeschwindigkeitPKW(), 0.001); // Delta für Genauigkeit
|
||||||
|
assertEquals(15.0, testUser.getDurchschnittsgeschwindigkeitFahrrad(), 0.001); // Delta für Genauigkeit
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEinloggen_FalscheDaten() throws IOException {
|
||||||
|
// Vorbereitung der Testdaten in einer temporären Datei
|
||||||
|
try (PrintWriter writer = new PrintWriter(testFilename)) {
|
||||||
|
writer.println("testuser," + testUser.getHashedPassword() + ",12345,Teststadt,Testauto,0.12,80.0,15.0");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ändere Passwort, um ein falsches Einloggen zu erzwingen
|
||||||
|
testUser.setPassword("wrongpassword");
|
||||||
|
|
||||||
|
// Einloggen des Users mit falschen Daten
|
||||||
|
assertFalse(testUser.einloggen());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package domainTest;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.junit.Test;
|
||||||
|
import domain.WetterService;
|
||||||
|
|
||||||
|
|
||||||
|
public class WetterServiceTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAktuellesWetter() throws IOException, InterruptedException {
|
||||||
|
String location = "Berlin";
|
||||||
|
String expectedWeatherRegex = "Aktuelle Temperatur: \\d+\\.\\d+°C, Wetter: .*";
|
||||||
|
|
||||||
|
String result = WetterService.getAktuellesWetter(location);
|
||||||
|
|
||||||
|
assertTrue("Expected weather format not found in result", result.matches(expectedWeatherRegex));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetWettervorhersage() throws IOException, InterruptedException {
|
||||||
|
String location = "Berlin";
|
||||||
|
String expectedForecastStart = "3-Tage-Wettervorhersage:";
|
||||||
|
|
||||||
|
String result = WetterService.getWettervorhersage(location);
|
||||||
|
|
||||||
|
assertTrue("Expected forecast start not found in result", result.startsWith(expectedForecastStart));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue