Наблюдение за реальными объектами
Узнайте, как использовать шпионы Mockito для частичной имитации реальных объектов, вызывая настоящие методы и одновременно проверяя взаимодействия.
«Наблюдение за реальными объектами» — бесплатный урок Testing Mastery: JUnit, Mockito & Integration Tests на CoddyKit. Это урок 3 из 4. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Testing Mastery: JUnit, Mockito & Integration Tests, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Testing Mastery: JUnit, Mockito & Integration Tests содержит 4 уроков всего.
Части этого урока еще не переведены и отображаются на английском.
What are Mockito Spies?
In Mockito, you've learned to create mocks to fully control object behavior. But what if you only want to change a few methods while keeping the original behavior for others?
That's where spies come in! A spy wraps a real object, allowing you to:
- Call the object's actual methods by default.
- Override (stub) specific methods to return predefined values.
- Verify interactions with the real object.
Think of it as 'partial mocking' – using the real thing, but with a few tweaks.
Creating Your First Spy
Creating a spy is straightforward. Instead of Mockito.mock(), you use Mockito.spy() and pass in an instance of the real object you want to spy on.
Let's define a simple DataService class we'll use for our examples. This service will perform some operations.
import org.mockito.Mockito;
public class DataService {
public String fetchData(String id) {
return "Real data for " + id;
}
public int processData(String data) {
return data.length();
}
}
public class Main {
public static void main(String[] args) {
DataService realService = new DataService();
DataService spyService = Mockito.spy(realService);
System.out.println("Spy created successfully!");
}
}Spies Call Real Methods
The key characteristic of a spy is that, by default, it will call the actual methods of the object it's wrapping. This is different from a mock, which has no real implementation and returns default values.
Let's see our spyService call a real method:
import org.mockito.Mockito;
public class DataService {
public String fetchData(String id) {
System.out.println("--- Calling REAL fetchData for ID: " + id + " ---");
return "Real data for " + id;
}
public int processData(String data) {
return data.length();
}
}
public class Main {
public static void main(String[] args) {
DataService realService = new DataService();
DataService spyService = Mockito.spy(realService);
String result = spyService.fetchData("123");
System.out.println("Result from spy: " + result);
int processed = spyService.processData("Hello");
System.out.println("Processed data length: " + processed);
}
}Stubbing a Spy: Overriding Behavior
While spies call real methods by default, you can still stub them to return specific values or throw exceptions, just like with mocks. This lets you control parts of the object's behavior.
However, when stubbing a spy, it's often safer to use doReturn().when() syntax:
Mockito.when(spy.method()).thenReturn(value)might execute the real method first if it's called during thewhen()part.Mockito.doReturn(value).when(spy).method()avoids calling the real method during stubbing, which is crucial if the real method has side effects or throws exceptions.
Stubbing a Spy in Action
Let's stub our spyService's fetchData method to return a custom value for a specific ID, while other calls still go to the real method:
import org.mockito.Mockito;
public class DataService {
public String fetchData(String id) {
System.out.println("--- Calling REAL fetchData for ID: " + id + " ---");
return "Real data for " + id;
}
public int processData(String data) {
return data.length();
}
}
public class Main {
public static void main(String[] args) {
DataService realService = new DataService();
DataService spyService = Mockito.spy(realService);
// Stubbing the spy for a specific input
Mockito.doReturn("Mocked data for specific ID")
.when(spyService)
.fetchData("specialId");
// This call will return the mocked data
String result1 = spyService.fetchData("specialId");
System.out.println("Result for specialId: " + result1);
// This call will go to the real method
String result2 = spyService.fetchData("regularId");
System.out.println("Result for regularId: " + result2);
}
}Verifying Spy Interactions
Just like with mocks, you can use Mockito.verify() to ensure that certain methods were called on your spy. This is powerful for confirming that your code interacts with the real object as expected.
verify() works exactly the same for spies as it does for mocks. You can check:
- If a method was called.
- How many times it was called.
- With what arguments it was called.
Verifying Spy Calls Example
Let's verify that our fetchData method was called on the spy, even when it executed its real implementation:
import org.mockito.Mockito;
public class DataService {
public String fetchData(String id) {
System.out.println("--- Calling REAL fetchData for ID: " + id + " ---");
return "Real data for " + id;
}
public int processData(String data) {
return data.length();
}
}
public class Main {
public static void main(String[] args) {
DataService realService = new DataService();
DataService spyService = Mockito.spy(realService);
// Call a method on the spy (it will execute the real method)
spyService.fetchData("user1");
spyService.processData("data1");
spyService.fetchData("user2");
// Verify interactions
Mockito.verify(spyService).fetchData("user1");
Mockito.verify(spyService, Mockito.times(2)).fetchData(Mockito.anyString());
Mockito.verify(spyService).processData("data1");
System.out.println("Verification successful!");
}
}Spies vs. Mocks: The Key Difference
It's crucial to understand when to use a spy versus a traditional mock:
- Mocks: Create a completely fake object. All methods are 'empty' and return default values unless you explicitly stub them. Ideal for isolating the unit under test.
- Spies: Wrap a real object. All methods execute their actual implementation unless you explicitly stub them. Useful when you want to use most of the real object's behavior but control a few specific interactions.
Prefer mocks for true unit isolation. Use spies when dealing with complex objects where only a few methods need to be controlled, or for legacy code.
When to Use Spies
Spies are particularly useful in scenarios where:
- You're testing a class that interacts with a complex dependency, and you only need to override a small part of that dependency's behavior.
- You have a legacy class with many methods, and creating a full mock would be tedious, but you need to ensure some methods are called or return specific values.
- You want to test partial behavior of a real object without fully replacing it.
However, use them sparingly. Over-reliance on spies can lead to less isolated tests that are harder to maintain.
Quick Check: Spy Behavior
Consider a Logger class with a log(String message) method that prints to console. If you spy on a new Logger() object, what happens when you call spyLogger.log("Test") without any stubbing?
Recap: Spying on Real Objects
You've successfully learned about Mockito spies!
- Spies wrap real objects, allowing you to use their actual methods by default.
- You create them with
Mockito.spy(realObject). - You can stub specific methods using
doReturn().when(spy).method()to override their behavior. - You can verify interactions on spies using
Mockito.verify(), just like with mocks. - Use spies carefully, primarily when partial control over a real object is needed, rather than full isolation.
This flexibility helps you write more targeted tests for complex scenarios.
Часто задаваемые вопросы
Урок «Наблюдение за реальными объектами» бесплатный?
Да — полный текст урока «Наблюдение за реальными объектами» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Testing Mastery: JUnit, Mockito & Integration Tests, подпишись на CoddyKit PRO. Курс Testing Mastery: JUnit, Mockito & Integration Tests содержит 4 уроков всего.
Чему я научусь в уроке «Наблюдение за реальными объектами»?
Узнайте, как использовать шпионы Mockito для частичной имитации реальных объектов, вызывая настоящие методы и одновременно проверяя взаимодействия. Ты практикуешь Testing Mastery: JUnit, Mockito & Integration Tests с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.
Нужен ли мне опыт, чтобы начать Testing Mastery: JUnit, Mockito & Integration Tests?
Предыдущий опыт не требуется. Testing Mastery: JUnit, Mockito & Integration Tests на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 3 из 4.
Сколько времени занимает урок «Наблюдение за реальными объектами»?
Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.
Можно ли писать и запускать код в этом уроке Testing Mastery: JUnit, Mockito & Integration Tests?
Да. Каждый урок Testing Mastery: JUnit, Mockito & Integration Tests включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.
Все уроки этого курса
- Подстановка возвращаемых значений
- Сопоставители аргументов Mockito
- Наблюдение за реальными объектами
- Выброс исключений и последовательные вызовы