Ranjeet Borate
4 min readSep 28, 2023

Mastering JUnit Testing for HTTP Methods in REST Controllers

In the world of web development, creating robust and reliable RESTful APIs is essential. However, equally important is ensuring that these APIs function as expected, even as your application evolves. This is where JUnit testing comes to the rescue. In this blog, we will embark on a journey to demystify the art of writing JUnit tests for REST controllers, giving you the tools to confidently test every aspect of your API.

JUnits for HTTP Methods

Understanding the HTTP Methods

Before diving into the world of JUnit testing for REST controllers, let's quickly review the core HTTP methods we'll be working with:

GET: The GET method is used to retrieve data from the server. In our tests, we will verify that our GET endpoints return the expected data in response to client requests.

POST: When you need to create a new resource on the server, you use the POST method. Our tests will ensure that data sent via POST is correctly processed and stored.

PATCH: The PATCH method is employed to make partial updates to a resource. Our tests will guarantee that resource updates occur accurately and efficiently.

PUT: If you need to completely replace a resource or create it if it doesn’t exist, you use the PUT method. We will ensure that PUT requests correctly update or create resources.

DELETE: Finally, the DELETE method allows us to remove resources from the server. In our tests, we will validate that these resources are indeed deleted when requested.

With this understanding of HTTP methods, we'll embark on a hands-on journey to write JUnit tests that cover each of these scenarios. By the end of this blog

Prerequisites before proceeding to write JUnits for

StudentController.java

@RestController
@RequestMapping("/student")
public class StudentController {

@Autowired
private StudentService studentService;

@GetMapping("/getStudentByNameAndClass/{name}/{clazz}")
public List<Student> getStudentByNameAndClass(@PathVariable("name") String name, @PathVariable("clazz") String clazz) {
return studentService.getStudentByNameAndClass(name, clazz);
}

@GetMapping("/getStudentByRollNumber/{rollNumber}")
public Student getStudentByRollNumber(@PathVariable("rollNumber") Integer rollNumber) {
return studentService.getStudentByRollNumber(rollNumber);
}

@PostMapping("/addStudent")
public Student addStudent(@RequestBody String studentJson) {
return studentService.addStudent(studentJson);
}

@PostMapping("/addStudentAndAppendPrefix/{prefix}")
public String addStudentAndAppendPrefix(@RequestBody String studentJson, @PathVariable("prefix") String prefix) {
return studentService.addStudentAndAppendPrefix(studentJson, prefix);
}
}

StudentService.java

@Service
public class StudentService {

@Autowired
private StudentRepository studentRepository;

public List<Student> getStudentByNameAndClass(String name, String clazz){
//Your business logic here
}

public Student getStudentByRollNumber(Integer rollNumber){
//Your business logic here
}

public Student addStudent(String studentJson){
//Your business logic here
}

public Student addStudentAndAppendPrefix(String studentJson, String prefix){
//Your business logic here
}
}

Now lets see the approach to proceed writing the JUnits especially for mostly used HTTP methods such as GET and POST.

StudentControllerTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(StudentController.class)
public class StudentControllerTest {

@Mock
private MockMvc mockMvc;

@MockBean
private StudentService studentService;

//In this case this is some dummy data
private List<Student> studentList = null;
private Student studentOne = null;
private String studentJsonString = null;

@Before
public void setUp(){
this.studentList = Arrays.asList(new Student(1, Master, firstName1, className1), new Student(2, Miss, firstName2, className2), new Student(3, Miss, firstName3, className3)));
this.studentOne = new Student(1, Master, firstName1, className1);
this.studentJsonString = new ObjectMapper().writeValueAsString(studentOne);
}

@Test
public void test_GetStudentByNameAndClass(){
when(studentService.getStudentByNameAndClassTest(eq(name), eq(clazz))).thenReturn(studentList);
mockMvc
.perform(get("/student/getStudentByNameAndClassTest/{name}/{clazz}",name, clazz))
.andExpect(status().isOk());
}

@Test
public void test_GetStudentByRollNumberTest(){
when(studentService.getStudentByRollNumber(eq(rollNumber))).thenReturn(studentOne);
mockMvc
.perform(get("/student/getStudentByRollNumber/{rollNumber}", rollNumber))
.andExpect(status().isOk());
}

@Test
public void test_AddStudent(){
when(studentService.addStudent(eq(studentJsonString))).thenReturn(studentOne);
mockMvc
.perform(post("/student/addStudent")
.content(new ObjectMapper().writeValueAsString(studentOne))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
public void test_AddStudentAndAppendPrefix(){
when(studentService.addStudentAndAppendPrefix(eq(studentJsonString), eq(prefix))).thenReturn(studentOne);
mockMvc
.perform(post("/student/addStudentAndAppendPrefix/{prefix}", prefix)
.content(new ObjectMapper().writeValueAsString(studentOne))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}

Along with .andExpect() you can use .andDo() .andExpectAll() .andReturn()

Conclusion: Time to Put the "Fun" in Functional Testing!

And there you have it, my fellow code wranglers! We’ve journeyed through the world of JUnit testing for REST controllers, armed with the knowledge to make our APIs as robust as the coffee maker in the break room (or at least close).

As we wrap up, remember that writing tests doesn’t have to be a soul-sucking, caffeine-fueled marathon. Embrace the quirks and eccentricities of your code like a stand-up comedian crafting punchlines. After all, we developers are the masters of debugging, the architects of algorithms, and occasionally, the creators of unintentional digital comedy.

So, go forth, write your JUnit tests with confidence, and remember that every assertion is like a punchline in the comedy show of software development. Your code is the stage, your tests are the jokes, and together, they create a symphony of functionality that’ll leave your users applauding. 🎉

Now, it’s time to hit that "Run Tests" button, watch those green checkmarks roll in, and revel in the laughter of bug-free code. And if all else fails, at least you’ll have some amusing error messages to share with your colleagues during your next coffee break.

Stay curious, stay coding, and may your REST controllers always respond with a standing ovation. Happy testing, my friends! 😄👩‍💻👨‍💻

Ranjeet Borate
Ranjeet Borate

Written by Ranjeet Borate

Interested in Tech • General Knowledge Awareness • Astronomy • Airforce and Aircrafts • History • Trekking

No responses yet