Browse Source

add flight throws exception mockito test

feature-pr-Admin
Sona Markosyan 2 years ago
parent
commit
3dc39408ea
  1. 4
      src/main/java/hs/fulda/de/ci/exam/project/Flight.java
  2. 40
      src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java

4
src/main/java/hs/fulda/de/ci/exam/project/Flight.java

@ -45,6 +45,10 @@ public class Flight {
return durationInMinutes;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
@Override
public String toString() {
return "Flight = {" + "flightNumber=" + flightNumber + '\'' + ", departure=" + departure + ", arrival='" + arrival + ", durationInMinutes=" + durationInMinutes + '}';

40
src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java

@ -1,27 +1,41 @@
package hs.fulda.de.ci.exam.project;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import java.io.IOException;
import java.util.ArrayList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class AdminTest {
final Address address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany");
Address address_fr;
Airport airport_fr;
Address address1_ist;
Airport airport1_ist;
Flight flight1;
Address address1;
@BeforeEach
public void setup() {
address_fr = new Address("Frankfurt str", "Frankfurt", "Hessen", "63023", "Germany");
airport_fr = new Airport("Fraport", address_fr, "1234");
address1_ist = new Address("Istanbul str", "Istanbul", "Fatih", "9019", "Turkey");
airport1_ist = new Airport("Istanbul", address1_ist, "5678");
flight1 = new Flight("1", airport_fr, airport1_ist, 140);
address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany");
}
@InjectMocks
Admin admin = new Admin("Max Muster", address1, "example@gmail.com", "012345678" );
@ -49,11 +63,6 @@ public class AdminTest {
@Test
public void test_addFlight_returnsNewFlight() throws IOException {
Address address_fr = new Address("Frankfurt str", "Frankfurt", "Hessen", "63023", "Germany");
Airport airport_fr = new Airport("Fraport", address_fr, "1234");
Address address1_ist = new Address("Istanbul str", "Istanbul", "Fatih", "9019", "Turkey");
Airport airport1_ist = new Airport("Istanbul", address1_ist, "5678");
Flight flight1 = new Flight("1", airport_fr, airport1_ist, 140);
try {
when(flightRepository.save(any(Flight.class))).thenReturn(true);
@ -68,4 +77,15 @@ public class AdminTest {
}
assertNotNull(admin.addFlight("1", airport_fr, airport1_ist, 140));
}
@Test
public void testAddFlight_throwsException() throws IOException {
when(flightRepository.save(any(Flight.class))).thenThrow(IOException.class);
assertThrows(IOException.class, () -> {
admin.addFlight("1", airport_fr, airport1_ist, 140);
});
}
}
Loading…
Cancel
Save