การกำหนดค่าที่ส่งกลับจากสตับ
กำหนดค่าให้ออบเจ็กต์ม็อกส่งคืนค่าที่ระบุหรือโยนข้อยกเว้นเมื่อมีการเรียกใช้เมธอด
การกำหนดค่าที่ส่งกลับจากสตับ เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Testing Mastery: JUnit, Mockito & Integration Tests และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Intro to Stubbing Mocks
When you create a mock object, it doesn't have any real behavior. Its methods do nothing by default, often returning null, 0, or empty collections.
Stubbing is the process of teaching a mock object how to respond to specific method calls. It's like giving your mock a script to follow!
This allows you to control the exact conditions for your tests, isolating the code you're actually testing.
Making Mocks Return Values
The most common way to stub a mock is using when().thenReturn(). This tells Mockito: "When this specific method is called on the mock, return this predefined value."
when(mock.someMethod()).thenReturn(value);
The value can be any object or primitive type that matches the method's return type.
thenReturn() Code Example
Try running this simple example. Notice how the mock's add method returns our stubbed value only when called with (1, 1).
import static org.mockito.Mockito.*;
interface CalculatorService {
int add(int a, int b);
}
public class Main {
public static void main(String[] args) {
// Create a mock of CalculatorService
CalculatorService mockCalc = mock(CalculatorService.class);
// Stub the add(1, 1) method to return 5
when(mockCalc.add(1, 1)).thenReturn(5);
// Call the stubbed method
System.out.println("1 + 1 = " + mockCalc.add(1, 1));
// Call a method that was NOT stubbed
System.out.println("2 + 2 = " + mockCalc.add(2, 2));
}
}Mocks Throwing Errors
Sometimes you need to test how your code handles errors. You can configure a mock to throw an exception when a specific method is called, using when().thenThrow().
when(mock.failingMethod()).thenThrow(new SomeException("Error!"));
This is crucial for testing error paths and ensuring your application gracefully handles unexpected situations.
thenThrow() Code Demo
In this example, our DataService mock is configured to throw an IllegalArgumentException if we try to fetch data with an "invalid" ID.
import static org.mockito.Mockito.*;
interface DataService {
String fetchData(String id);
}
public class Main {
public static void main(String[] args) {
DataService mockData = mock(DataService.class);
// Stub fetchData("invalid") to throw an exception
when(mockData.fetchData("invalid"))
.thenThrow(new IllegalArgumentException("ID not found!"));
// Call a method that was NOT stubbed (returns null by default)
System.out.println("Fetching 'valid': " + mockData.fetchData("valid"));
// Call the stubbed method that throws an exception
try {
System.out.println("Fetching 'invalid': " + mockData.fetchData("invalid"));
} catch (IllegalArgumentException e) {
System.out.println("Caught error: " + e.getMessage());
}
}
}Dynamic Responses: thenAnswer()
What if you need the mock's response to depend on the arguments passed to it, or to perform some custom logic?
when().thenAnswer() is your solution. It takes an Answer object (often a lambda or anonymous class) which provides more control:
- You get access to the actual method arguments.
- You can perform calculations or complex logic.
- You can return a value or throw an exception dynamically.
thenAnswer() Code Demo
Here, the multiply method on our mock actually performs the multiplication based on the arguments it receives. This is more dynamic than a fixed return value.
import static org.mockito.Mockito.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
interface MathService {
int multiply(int a, int b);
}
public class Main {
public static void main(String[] args) {
MathService mockMath = mock(MathService.class);
// Stub multiply for any two integers to perform actual multiplication
when(mockMath.multiply(anyInt(), anyInt())).thenAnswer(
new Answer<Integer>() {
public Integer answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Integer arg1 = (Integer) args[0];
Integer arg2 = (Integer) args[1];
return arg1 * arg2; // Custom logic based on arguments
}
}
);
System.out.println("3 * 4 = " + mockMath.multiply(3, 4));
System.out.println("5 * 2 = " + mockMath.multiply(5, 2));
System.out.println("7 * 0 = " + mockMath.multiply(7, 0));
}
}Multiple Returns: Chaining
What if you want a mock method to return different values on successive calls?
Mockito allows you to chain stubbing methods. You can provide multiple return values to thenReturn(), or chain multiple thenReturn(), thenThrow(), or thenAnswer() calls.
when(mock.method()).thenReturn(val1, val2, val3);when(mock.method()).thenReturn(val1).thenThrow(ex).thenReturn(val3);
After all specified values or actions are exhausted, the last one will be repeated for subsequent calls.
Chained Stubbing Demo
Observe how our mock Queue returns "Item A", then "Item B", and then throws an exception on subsequent poll() calls.
import static org.mockito.Mockito.*;
import java.util.Queue;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Queue<String> mockQueue = mock(Queue.class);
// Stub poll() to return different values/actions on successive calls
when(mockQueue.poll())
.thenReturn("Item A")
.thenReturn("Item B")
.thenThrow(new IllegalStateException("Queue is empty!"));
System.out.println("Poll 1: " + mockQueue.poll()); // Returns "Item A"
System.out.println("Poll 2: " + mockQueue.poll()); // Returns "Item B"
try {
System.out.println("Poll 3: " + mockQueue.poll()); // Throws exception
} catch (IllegalStateException e) {
System.out.println("Caught error on Poll 3: " + e.getMessage());
}
// Subsequent calls will repeat the last action (throw exception)
try {
System.out.println("Poll 4: " + mockQueue.poll());
} catch (IllegalStateException e) {
System.out.println("Caught error on Poll 4: " + e.getMessage());
}
}
}Check Your Understanding
You are testing a method that processes items from a queue. You need the queue's poll() method to return "Task 1", then "Task 2", and finally null (indicating an empty queue) for subsequent calls. Which Mockito stubbing approach would you use?
Stubbing Return Values: Recap
In this lesson, you learned how to control the behavior of your mock objects by stubbing their method calls. This is fundamental for isolating the unit under test and creating predictable test environments.
when().thenReturn(): To make a mock method return a specific value.when().thenThrow(): To make a mock method throw an exception.when().thenAnswer(): For dynamic responses based on method arguments or custom logic.- Chaining: To define a sequence of different behaviors for successive calls to the same method.
Next, we'll explore Mockito's argument matchers for even more flexible stubbing and verification!
คำถามที่พบบ่อย
บทเรียน “การกำหนดค่าที่ส่งกลับจากสตับ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกำหนดค่าที่ส่งกลับจากสตับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Testing Mastery: JUnit, Mockito & Integration Tests ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดค่าที่ส่งกลับจากสตับ”
กำหนดค่าให้ออบเจ็กต์ม็อกส่งคืนค่าที่ระบุหรือโยนข้อยกเว้นเมื่อมีการเรียกใช้เมธอด คุณปฏิบัติ Testing Mastery: JUnit, Mockito & Integration Tests ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Testing Mastery: JUnit, Mockito & Integration Tests หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Testing Mastery: JUnit, Mockito & Integration Tests บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การกำหนดค่าที่ส่งกลับจากสตับ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests นี้ได้ไหม
ได้ บทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกำหนดค่าที่ส่งกลับจากสตับ
- ตัวจับคู่อาร์กิวเมนต์ของ Mockito
- การสอดส่องออบเจ็กต์จริง
- การโยนข้อยกเว้นและการเรียกต่อเนื่อง