Spring MVC অ্যাপ্লিকেশনগুলির জন্য Unit Testing এবং Integration Testing খুবই গুরুত্বপূর্ণ, কারণ এগুলি নিশ্চিত করে যে আপনার কোড সঠিকভাবে কাজ করছে এবং বিভিন্ন কম্পোনেন্ট একসাথে ঠিকভাবে কাজ করছে। Spring Testing ফ্রেমওয়ার্ক JUnit, Mockito, এবং Spring Test সমর্থন করে, যা ইউনিট টেস্ট এবং ইন্টিগ্রেশন টেস্ট করার জন্য শক্তিশালী টুল সরবরাহ করে।
1. Unit Testing in Spring MVC
Unit Testing হল শুধুমাত্র একক ইউনিট (যেমন একটি মেথড বা ক্লাস) পরীক্ষা করা। Spring MVC অ্যাপ্লিকেশনের ক্ষেত্রে, এটি সাধারণত Controller, Service, এবং Repository-এর জন্য করা হয়।
Unit Testing Example for Controller with Mockito
Controller:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;
@Controller
public class MyController {
private final MyService myService;
public MyController(MyService myService) {
this.myService = myService;
}
@GetMapping("/greet")
public String greet(Model model) {
String message = myService.getGreetingMessage();
model.addAttribute("message", message);
return "greet";
}
}
Service:
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String getGreetingMessage() {
return "Hello from Service!";
}
}
JUnit Test for Controller with Mockito:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.ui.Model;
import static org.mockito.Mockito.*;
public class MyControllerTest {
@Mock
private MyService myService;
@Mock
private Model model;
private MyController myController;
@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this); // Initialize mocks
myController = new MyController(myService);
}
@Test
public void testGreet() {
// Given
when(myService.getGreetingMessage()).thenReturn("Hello, Test!");
// When
String result = myController.greet(model);
// Then
verify(myService, times(1)).getGreetingMessage(); // Ensure method was called once
verify(model, times(1)).addAttribute("message", "Hello, Test!"); // Ensure model is updated
assertEquals("greet", result); // Check the view name
}
}
Explanation:
- Mockito:
@Mockঅ্যানোটেশন ব্যবহার করে মক অবজেক্ট তৈরি করা হয়। - MockitoAnnotations.openMocks(this): মক অবজেক্টগুলি ইনিশিয়ালাইজ করা হয়।
when(myService.getGreetingMessage()).thenReturn("Hello, Test!"): এটিmyService.getGreetingMessage()কল করার সময় একটি কাস্টম রেসপন্স প্রদান করে।verify(): এটি নিশ্চিত করে যে মক অবজেক্টের উপর নির্দিষ্ট মেথড একাধিক বার কল হয়েছে কিনা।
2. Integration Testing in Spring MVC
Integration Testing হল একাধিক কম্পোনেন্টের ইন্টিগ্রেশন পরীক্ষা করা, অর্থাৎ পুরো অ্যাপ্লিকেশন বা কিছু অংশের মেলবন্ধন পরীক্ষা করা। Spring MVC অ্যাপ্লিকেশনগুলিতে, Integration Testing সাধারণত Controller-এর সাথে Service, Repository ইত্যাদির কাজ নিশ্চিত করার জন্য করা হয়।
Spring Test Framework ব্যবহৃত হয় Spring MVC অ্যাপ্লিকেশনে ইনটিগ্রেশন টেস্ট পরিচালনা করতে।
Integration Test Example with Spring Test
Spring Boot Integration Test:
Spring Boot প্রকল্পে @SpringBootTest এবং @WebMvcTest অ্যানোটেশনগুলি ইনটিগ্রেশন টেস্টিংয়ের জন্য ব্যবহৃত হয়।
Controller Integration Test with @SpringBootTest:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@SpringBootTest
public class MyControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testGreet() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/greet"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("greet"))
.andExpect(MockMvcResultMatchers.model().attribute("message", "Hello from Service!"));
}
}
Explanation:
@SpringBootTest: Spring Boot অ্যাপ্লিকেশনের সম্পূর্ণ কনটেক্সটে টেস্ট চালায়।MockMvc: এটি Spring MVC কন্ট্রোলার মেথডগুলির HTTP রিকোয়েস্ট এবং রেসপন্স সিমুলেট করতে ব্যবহৃত হয়।MockMvcRequestBuilders.get("/greet"): এটি/greetURL এ GET রিকোয়েস্ট পাঠায়।andExpect(): রিকোয়েস্টের পর ফলস্বরূপ status, view, এবং model যাচাই করা হয়।
Integration Test Example for Service Layer
Service Layer Integration Test:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
public class MyServiceIntegrationTest {
@Autowired
private MyService myService;
@Test
public void testGetGreetingMessage() {
String message = myService.getGreetingMessage();
assertEquals("Hello from Service!", message);
}
}
Explanation:
@SpringBootTest: Spring Boot অ্যাপ্লিকেশন কনটেক্সট লোড করে টেস্ট চালায়, যা প্রকৃত সার্ভিসের ইন্টিগ্রেশন নিশ্চিত করে।- Service Layer Testing: এখানে আমরা Service লেয়ারের কাজ যাচাই করেছি, যা ডাটাবেস বা অন্য কম্পোনেন্টের সাথে ইন্টিগ্রেটেড হতে পারে।
3. Mocking Dependencies in Integration Testing
Spring Test ফ্রেমওয়ার্কে মকিং করতে Mockito ব্যবহার করা হয়। যখন আপনি প্রকৃত সার্ভিস বা ডাটাবেস ব্যবহার করতে না চান, তখন আপনি মক অবজেক্ট দিয়ে পরীক্ষাগুলি করতে পারেন।
Example: Mocking Repository in Service Layer
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class MyServiceTest {
@Mock
private MyRepository myRepository;
@InjectMocks
private MyService myService;
@Test
public void testGetGreetingMessage() {
// Arrange
when(myRepository.getGreeting()).thenReturn("Mocked Greeting");
// Act
String message = myService.getGreetingMessage();
// Assert
assertEquals("Mocked Greeting", message);
verify(myRepository, times(1)).getGreeting(); // Ensure method was called
}
}
Explanation:
@Mock: মক অবজেক্ট তৈরি করা হয়েছে।@InjectMocks: সার্ভিস ক্লাসে মক অবজেক্ট ইনজেক্ট করা হয়।when(): মক অবজেক্টে প্রেরিত মেথডের জন্য রিটার্ন মান সেট করা হয়।verify(): এটি নিশ্চিত করে যে মক অবজেক্টের মেথডটি নির্দিষ্ট পরিমাণে কল হয়েছে কিনা।
Conclusion
Spring MVC অ্যাপ্লিকেশনের Unit Testing এবং Integration Testing নিশ্চিত করতে আপনার কোডের কাজ সঠিকভাবে হচ্ছে এবং বিভিন্ন কম্পোনেন্ট একসাথে কার্যকরীভাবে কাজ করছে।
- Unit Testing: মক অবজেক্ট দিয়ে একক ইউনিট পরীক্ষা করা, যেমন কন্ট্রোলার, সার্ভিস বা রেপোজিটরি।
- Integration Testing: প্রকৃত Spring Context এবং কম্পোনেন্টের ইন্টিগ্রেশন পরীক্ষা করা, যেমন কন্ট্রোলার এবং সার্ভিস স্তরের একত্রে কাজ নিশ্চিত করা।
Spring Test Framework এবং JUnit, Mockito এর সাহায্যে Spring MVC অ্যাপ্লিকেশনগুলির ইউনিট ও ইন্টিগ্রেশন টেস্ট খুবই সহজ এবং শক্তিশালী।