获取 NullPointerException tracer.currentSpan() 为 Null
我正在尝试为给定的类创建测试用例,并希望返回traceId 值,但得到的trace.currentSpan() 为空。
这是我的类
public class ConsumerService{
private final Tracer tracer;
@Autowired
private RequestService requestService;
public void consumerProductionRequest(DisclaimerRequest disclaimerRequest){
String traceId=tracer.currentSpan.context().traceId();
log.info(traceId);
if(disclaimerRequest.getReqId()==null){
disclaimerRequest.setReqId(UUID.randomUUID().toString());
}
}
}
//Test Class
@ExtendWith(MockitoExtension.class)
class ConsumerServiceTest{
@InjectMocks
ConsumerService consumerService;
@Autowired
Tracer tracer;
@Test
void Test(){
Tracer tracer=Mockito.mock(Tracer.class);
String traceId;
Mockito.when(tracer.currentSpan().context.traceId()).thenReturn(traceId);
DisclaimerRequest disclaimerRequest=new DisclaimerRequest();
consumerService.consumerProductionRequest(disclaimerRequest);
}
}
为什么我得到的 tracer.currentSpan() null。我正在使用 JUnit 5 和新的。请有人帮我解决这个问题。
I'm trying to create test case for given class and want to return traceId value but getting trace.currentSpan() is null.
Here is my Class
public class ConsumerService{
private final Tracer tracer;
@Autowired
private RequestService requestService;
public void consumerProductionRequest(DisclaimerRequest disclaimerRequest){
String traceId=tracer.currentSpan.context().traceId();
log.info(traceId);
if(disclaimerRequest.getReqId()==null){
disclaimerRequest.setReqId(UUID.randomUUID().toString());
}
}
}
//Test Class
@ExtendWith(MockitoExtension.class)
class ConsumerServiceTest{
@InjectMocks
ConsumerService consumerService;
@Autowired
Tracer tracer;
@Test
void Test(){
Tracer tracer=Mockito.mock(Tracer.class);
String traceId;
Mockito.when(tracer.currentSpan().context.traceId()).thenReturn(traceId);
DisclaimerRequest disclaimerRequest=new DisclaimerRequest();
consumerService.consumerProductionRequest(disclaimerRequest);
}
}
Why am I getting tracer.currentSpan() null. I'm using JUnit 5 and new in this. Please someone help me to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里,在您的 test 类中:
将其转换为
(并将相应的导入添加到 Mockito)。您希望 Mockito 创建一个模拟对象,然后将其插入到被测试的对象中。另请注意,您的测试方法本身重新定义
跟踪器
,从而隐藏您在类级别所做的任何其他操作。因此,测试方法中的tracer
规范不会对已注入测试对象的模拟 Tracer 实例产生任何影响。Here, in your test class:
Turn that into
(and add the corresponding import to Mockito). You want that Mockito creates a mock object, and then inserts that into your object under test. Also note that your test method itself redefines
tracer
, thus shadowing whatever else you did on class level. Therefore that spec fortracer
within the test method won't have any effect on the mocked Tracer instance that already got injected into your object under test.这对我有用
不需要使用@Mock
This worked for me
No need of using @Mock
跟踪器将是模拟的,您可以在调试中看到它,或者只是简单地打印它。
此外,每个方法都返回 null,因为您没有指定哪个方法将返回值,因为 spring 上下文在单元测试中不起作用。在集成测试中,您可以自动装配它。
您必须模拟 getCurrentSpan 和 getContext 方法来解决您的问题。另外,您不能仅使用 @Mock 注释来自动装配它。 Spring 上下文在单元测试中不存在,因此不会自动装配。 Spring相关的代码将不起作用。集成测试为您运行 spring 上下文,您可以自动装配它。
tracer will be mock and you can see it in debug or just simply print it.
also each method returns null because you didn't specify which method will return value because spring context does not work in unit tests. In integration tests you can autowire it.
You have to mock getCurrentSpan and getContext methods that will solve your problem. Also you can not autowire it just use @Mock annotation. Spring context does not exist in unit tests so it won't be autowired. Spring related codes won't work. Integration test runs the spring context for you and you can autowire it.