24 lines
1012 B
Java
24 lines
1012 B
Java
package com.baiyue.common.web;
|
|||
|
|
|
||
|
|
import com.baiyue.common.core.api.ResultCode;
|
||
|
|
import com.baiyue.common.core.exception.BizException;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||
|
|
|
||
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
||
|
|
|
||
|
|
class GlobalExceptionHandlerTest {
|
||
|
|
private final GlobalExceptionHandler handler = new GlobalExceptionHandler();
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void businessExceptionKeepsBusinessCodeAndCorrelation() {
|
||
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||
|
|
request.setAttribute(CorrelationContext.REQUEST_ID, "r1");
|
||
|
|
request.setAttribute(CorrelationContext.TRACE_ID, "t1");
|
||
|
|
var response = handler.handleBusiness(new BizException("inventory is locked"), request);
|
||
|
|
assertThat(response.getStatusCode().value()).isEqualTo(400);
|
||
|
|
assertThat(response.getBody().code()).isEqualTo(ResultCode.BUSINESS_ERROR.code());
|
||
|
|
assertThat(response.getBody().requestId()).isEqualTo("r1");
|
||
|
|
}
|
||
|
|
}
|