Scavenger233 / REST-API-Project

Building a REST API project from scratch.
0 stars 0 forks source link

Unittesting errors in general #3

Closed Scavenger233 closed 1 week ago

Scavenger233 commented 1 week ago

根据你提供的报错信息,我将逐段解析并解释每个部分的结构和含义,并告诉你如何通过这些信息排查问题。这将帮助你理解常见的Spring Boot测试报错,并能够自主解决问题。

1. 初始化测试上下文阶段

12:13:00.905 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
12:13:00.910 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
12:13:00.927 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.example.controller.LessonControllerTest] from class [org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper]

解析:

如何使用:


2. 寻找配置文件阶段

12:13:00.933 [main] INFO org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.example.controller.LessonControllerTest], using SpringBootContextLoader
12:13:00.935 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com/example/controller/LessonControllerTest]: class path resource [com/example/controller/LessonControllerTest-context.xml] does not exist

解析:

如何使用:


3. Spring Boot 应用启动阶段

2024-10-08 12:13:01.483  INFO 8936 --- [           main] c.e.controller.LessonControllerTest      : Starting LessonControllerTest using Java 1.8.0_381 on s-Macbook.local with PID 8936 (started by pocky2333 in /Users/pocky2333/Desktop/Codes/lesson)
2024-10-08 12:13:01.483  INFO 8936 --- [           main] c.e.controller.LessonControllerTest      : The following 1 profile is active: "test"

解析:

如何使用:


4. 安全过滤器加载阶段

2024-10-08 12:13:02.537  INFO 8936 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@78ec89a6, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@3596b249, ... ]

解析:

如何使用:


5. 处理请求与返回响应阶段

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /instructors/pocky/lessons/10001
       Parameters = {}
          Headers = [Accept:"application/json"]
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = com.example.controller.LessonController
           Method = com.example.controller.LessonController#getLesson(String, long)

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = [Content-Type:"application/json"]
     Content type = application/json
             Body = {"id":10001,"username":"pocky","description":"Spring boot introduction"}

解析:

如何使用:


6. Resolved Exception

Resolved Exception:
             Type = null

解析:

如何使用:


总结与排查技巧:

  1. 检查初始化日志:当测试无法正常启动时,查看初始化日志是否有上下文加载错误、配置文件丢失等问题。
  2. 检查请求与响应日志:关注 MockHttpServletRequestMockHttpServletResponse,通过这些日志,你可以验证请求是否被正确发送,响应是否与预期一致。
  3. 查看异常信息:如果日志中有 Resolved Exception 字段,查看具体的异常类型并追溯到发生错误的地方。
  4. 状态码和响应体检查:如果状态码或响应体与预期不符,可以通过打印和调试工具检查实际返回的内容,并与期望值进行对比。
  5. 使用日志的 DEBUG 模式:开启 DEBUG 日志模式可以提供更多关于应用上下文和依赖注入的细节,对于调试复杂的测试失败问题特别有用。

希望这些解析和建议能帮助你以后通过日志和报错信息自主排查问题。