SpringBootTest_Mocks

1. O projekcie


2. Co to są mocki i Mockito


3. Struktura projektu


3. pom.xml - zależności i wersje

Kluczowe zależności:

Wersja Java: 17.


4. SpringBootTestMocksApplication.java – uruchomienie aplikacji

package pl.com.pl.springboottest_mocks; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // automatyczna konfiguracja i skanowanie komponentów public class SpringBootTestMocksApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTestMocksApplication.class, args); // uruchomienie aplikacji } }

5. HelloController.java – prosty kontroler REST

package pl.com.pl.springboottest_mocks; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController // kontroler REST zwracający dane bez widoków public class HelloController { @GetMapping("/hello") // obsługa żądania GET pod adresem /hello public String hello() { return "Hello, world!"; // zwraca prosty tekst jako odpowiedź } }

6. GreetingServiceTest.java – unit test z mockiem

package pl.com.pl.springboottest_mocks; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; class GreetingService { String greet(String name) { // metoda generująca powitanie return "Hello, " + name; } } @SpringBootTest public class GreetingServiceTest { @Test void testGreetingWithMock() { GreetingService mockService = Mockito.mock(GreetingService.class); // tworzenie atrapy serwisu when(mockService.greet("Artur")).thenReturn("Cześć Artur"); // ustalenie zachowania mocka String result = mockService.greet("Artur"); // wywołanie metody na mocku assertEquals("Cześć Artur", result); // porównanie wyniku z oczekiwanym verify(mockService).greet("Artur"); // sprawdzenie, że metoda została wywołana } }

Nauka z tego testu:

Uwaga: Ten test pokazuje technikę mockowania, ale nie testuje rzeczywistej implementacji metody greet.


7. HelloControllerTest.java – test warstwy webowej

package pl.com.pl.springboottest_mocks; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @SpringBootTest // ładowanie pełnego kontekstu Spring @AutoConfigureMockMvc // konfiguracja MockMvc do testowania bez serwera public class HelloControllerTest { @Autowired private MockMvc mockMvc; // narzędzie do symulowania żądań HTTP @Test void shouldReturnHelloWorld() throws Exception { mockMvc.perform(get("/hello")) // symulacja żądania GET .andExpect(status().isOk()) // oczekiwanie kodu 200 .andExpect(content().string("Hello, world!")); // oczekiwanie treści odpowiedzi } }

Kluczowe punkty:


8. SpringBootTestMocksApplicationTests.java – sanity check

package pl.com.pl.springboottest_mocks; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest // test sprawdzający, czy kontekst Spring się zbuduje class SpringBootTestMocksApplicationTests { @Test void contextLoads() { } }

9. Pojęcia i wyjaśnienia kodu


10. Najważniejsze wnioski