Spring Sınama Bağlamı Çerçevesi
Spring'in sınama bağlamı çerçevesinin bütünleştirme sınamaları için uygulama bağlamlarını nasıl başlatıp yönettiğini anlayın.
Spring Sınama Bağlamı Çerçevesi, CoddyKit'te ücretsiz bir Testing Mastery: JUnit, Mockito & Integration Tests dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Testing Mastery: JUnit, Mockito & Integration Tests öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Testing Mastery: JUnit, Mockito & Integration Tests kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Spring Test Context Intro
Welcome to Spring Boot integration testing! When you write tests for Spring applications, you need a way to bring up a part of your application. That's where the Spring Test Context Framework comes in.
It provides the infrastructure to load and manage an ApplicationContext for your tests, ensuring they run in a Spring-aware environment. This is crucial for tests that interact with beans, services, or data layers.
The @SpringBootTest Annotation
The core of Spring Boot integration testing is the @SpringBootTest annotation. It tells JUnit to bootstrap the Spring Boot application context for your test.
- It starts a full
ApplicationContext. - It finds the main configuration class (often your
@SpringBootApplicationclass). - It provides access to all your Spring beans within the test.
Think of it as running your entire application, but specifically for your test's needs.
How Contexts Are Loaded
When @SpringBootTest is used, Spring's test framework looks for your main application class (the one annotated with @SpringBootApplication) and uses it to create an ApplicationContext.
This context is essentially a container for all the components (beans) of your application. Your test then runs against this live application context, allowing it to interact with actual services, repositories, and other Spring-managed components.
Context Caching for Speed
Loading a full Spring ApplicationContext can be slow. To speed up your test suite, the Spring Test Context Framework intelligently caches contexts.
- If multiple tests require the exact same context configuration, Spring will reuse the previously loaded context.
- This significantly reduces test execution time, especially in large projects.
- The context is only reloaded if the configuration (e.g., properties, active profiles) changes.
Basic Spring App Example
Here's a minimal Spring Boot application. When you use @SpringBootTest, the framework will load a context based on this class, making its beans available to your tests.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(MyApp.class, args);
System.out.println("App context loaded: " + (context != null));
}
}Using @SpringBootTest (Test)
Now, let's see how a test would use @SpringBootTest to load the context for the MyApp from the previous scene. We inject the ApplicationContext to verify it's loaded.
Notice this test class doesn't have a main method itself, as JUnit runners handle its execution.
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
class MyApplicationTests {
@Test
void contextLoads(ApplicationContext context) {
assertThat(context).isNotNull();
System.out.println("ApplicationContext loaded for test!");
}
}Customizing Web Environment
For web applications, @SpringBootTest can start a web server for your tests. You control this behavior using the webEnvironment attribute:
MOCK(default): Provides a mock servlet environment.RANDOM_PORT: Starts an embedded server on a random port.DEFINED_PORT: Starts an embedded server on the default or configured port.NONE: Does not start an embedded server.
RANDOM_PORT is often preferred to avoid port conflicts.
Web Environment Example
Here's how you'd configure @SpringBootTest to start a web server on a random port. This allows you to perform actual HTTP requests against your application within your tests.
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.beans.factory.annotation.Value;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class WebAppTests {
@Value(value="${local.server.port}")
private int port;
@Test
void serverStarts() {
assertThat(port).isNotZero();
System.out.println("Web server started on port: " + port);
}
}Overriding Test Properties
Sometimes, you need specific configuration properties for your tests that differ from your main application (e.g., a test database URL). You can override these using the properties attribute in @SpringBootTest.
This allows you to isolate your test environment without changing production code.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
@SpringBootApplication
public class PropApp implements CommandLineRunner {
@Value("${app.message:Default Message}")
private String message;
public static void main(String[] args) {
SpringApplication.run(PropApp.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("App Message: " + message);
}
}Quick Check
Which @SpringBootTest.WebEnvironment option would you use to start an embedded web server on an available, random port?
Recap: Spring Test Context
You've learned about the Spring Test Context Framework, a powerful tool for integration testing. We covered:
- The purpose of the framework and
@SpringBootTest. - How application contexts are loaded and cached.
- Customizing test environments with
webEnvironment. - Overriding properties for test-specific configurations.
This foundation is essential for writing robust and efficient Spring Boot integration tests!
Sıkça Sorulan Sorular
“Spring Sınama Bağlamı Çerçevesi” dersi ücretsiz mi?
Evet — “Spring Sınama Bağlamı Çerçevesi” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Testing Mastery: JUnit, Mockito & Integration Tests kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Testing Mastery: JUnit, Mockito & Integration Tests kursu toplamda 4 dersten oluşur.
“Spring Sınama Bağlamı Çerçevesi” dersinde ne öğreneceğim?
Spring'in sınama bağlamı çerçevesinin bütünleştirme sınamaları için uygulama bağlamlarını nasıl başlatıp yönettiğini anlayın. Testing Mastery: JUnit, Mockito & Integration Tests ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Testing Mastery: JUnit, Mockito & Integration Tests öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Testing Mastery: JUnit, Mockito & Integration Tests, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“Spring Sınama Bağlamı Çerçevesi” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Testing Mastery: JUnit, Mockito & Integration Tests dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Testing Mastery: JUnit, Mockito & Integration Tests dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Spring Sınama Bağlamı Çerçevesi
- RESTful API'leri Sınama
- Sınamalar için Gömülü Veritabanları
- @WebMvcTest ve MockMvc ile Web Katmanlarını Test Etme