Back to Repositories

Testing Job Management Controller Authentication in XXL-Job

This test suite validates the JobInfo controller functionality in XXL-Job’s admin interface, focusing on user authentication and job management operations. It implements Spring MVC test framework to simulate HTTP requests and verify controller responses.

Test Coverage Overview

The test suite covers core job management API endpoints with focus on authentication and job listing functionality.

  • Tests authentication flow with admin credentials
  • Validates job listing with specific parameters
  • Covers cookie-based session management
  • Tests request parameter handling

Implementation Analysis

The testing approach utilizes Spring’s MockMvc framework for simulating HTTP requests and validating responses. It implements a BeforeEach setup for authentication and maintains session state through cookies.

The implementation follows Spring’s testing patterns with explicit content type specification and form parameter handling. The test structure demonstrates proper separation of setup and test execution.

Technical Details

  • JUnit Jupiter for test execution
  • Spring MockMvc for HTTP request simulation
  • SLF4J for test logging
  • MediaType.APPLICATION_FORM_URLENCODED for request formatting
  • LinkedMultiValueMap for parameter management

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, setup compartmentalization, and explicit parameter handling.

  • Separate login setup using @BeforeEach
  • Clear parameter organization
  • Proper content type specification
  • Logging integration for debugging
  • Extension of abstract test class for common functionality

xuxueli/xxl-job

xxl-job-admin/src/test/java/com/xxl/job/admin/controller/JobInfoControllerTest.java

            
package com.xxl.job.admin.controller;

import com.xxl.job.admin.service.impl.LoginService;
import jakarta.servlet.http.Cookie;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

public class JobInfoControllerTest extends AbstractSpringMvcTest {
  private static Logger logger = LoggerFactory.getLogger(JobInfoControllerTest.class);

  private Cookie cookie;

  @BeforeEach
  public void login() throws Exception {
    MvcResult ret = mockMvc.perform(
        post("/login")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .param("userName", "admin")
            .param("password", "123456")
    ).andReturn();
    cookie = ret.getResponse().getCookie(LoginService.LOGIN_IDENTITY_KEY);
  }

  @Test
  public void testAdd() throws Exception {
    MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
    parameters.add("jobGroup", "1");
    parameters.add("triggerStatus", "-1");

    MvcResult ret = mockMvc.perform(
        post("/jobinfo/pageList")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            //.content(paramsJson)
            .params(parameters)
            .cookie(cookie)
    ).andReturn();

    logger.info(ret.getResponse().getContentAsString());
  }

}