[英]spring cloud is failing when I try to mock sleuth in unit tests
I am facing some error when I try to mock objects like Tracer and Span in unit tests if I use Dalston.SR3 or Dalston.Release versions, but this problem doesn't happen if I use Camden.SR6 or Camden.SR7 versions. 如果我使用Dalston.SR3或Dalston.Release版本尝试在单元测试中模拟Tracer和Span等对象时遇到一些错误,但是如果我使用Camden.SR6或Camden.SR7版本则不会发生此问题。
Find an example code here 在此处找到示例代码
Microservice msvc-a is using Dalston version and has two test classes, where only is failing the class where I am trying to mock the Tracer and the Span objects. 微服务msvc-a使用的是Dalston版本,并且具有两个测试类,其中只有使我试图模拟Tracer和Span对象的类失败。
Microservice msvc-b is using Camden version and has the same test classes. 微服务msvc-b使用的是Camden版本,并且具有相同的测试类。
At the same time, I can't understand this situation when I am in debug mode in STS and why I can't see any error trace or something like that... only a NullpointerException. 同时,当我处于STS调试模式时,我无法理解这种情况,为什么我看不到任何错误跟踪或类似信息……仅是NullpointerException。
public class AbstractSpanAccessorTest { @MockBean private Tracer tracer; @MockBean private Span span; private Random random = new Random(); @Before public void mockSpan() { long id = createId(); Span spanMock = Span.builder().name("mock").traceId(id).spanId(id).build(); doReturn(spanMock.traceIdString()).when(span).traceIdString(); doReturn(span).when(tracer).getCurrentSpan(); doReturn(span).when(tracer).createSpan(anyString()); } private long createId() { return random.nextLong(); } }
It was my mistake. 是我的错 The correct way to mock the Span is:
模拟跨度的正确方法是:
@Before
public void mockSpan() {
long id = createId();
span = Span.builder().name("mock").traceId(id).spanId(id).build();
doReturn(span).when(tracer).getCurrentSpan();
doReturn(span).when(tracer).createSpan(anyString());
}
Tracer wouldn't mock at all with Finchley.SR2 so I ended up with this: Tracer完全不会使用Finchley.SR2进行嘲笑,所以我最终得到了这一点:
Tracing tracing = Tracing.newBuilder().build();
Tracer tracer = tracing.tracer();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.