gpt4 book ai didi

spring-boot - Spring boot 2.0.5.RELEASE - sleuth 和 mockito

转载 作者:行者123 更新时间:2023-12-05 08:53:48 25 4
gpt4 key购买 nike

我已经尝试了一个星期来解决这个问题,但一点运气都没有。问题出在单元测试上。

这是我要测试的类:

import brave.Span;
import brave.Tracer;

@Service
public class InternetBackEndRestClient {

@Autowired
private Tracer tracer;

public PasswordJwtResponse generatePassworJwt(PasswordJwtRequest passwordJwtRequest, String traceId) throws LogonProxyException {
log.info("{\"Starting method\": \"generatePassworJwt\", \"input\": {} }", passwordJwtRequest);

Span newSpan = tracer.nextSpan().name("spanPasswordJwtResponse");
...
}
}

如何进行单元测试? Brave.Tracer 是最后一个类,所以我不能 mock 它。无论如何要建立一个上下文?还是模拟示踪剂?

@RunWith(MockitoJUnitRunner.class)
public class InternetBackEndRestClientTest {

@InjectMocks
private InternetBackEndRestClient internetBackEndRestClient;

@Mock
private Tracer tracer;

@Test
public void generatePassworJwt_test() {
internetBackEndRestClient.generatePassworJwt(...);
....
}
}

有人可以帮我吗?

最佳答案

这是对我有用的解决方案:

@RunWith(MockitoJUnitRunner.class)
public class InternetBackEndRestClientTest {

private static final String TRACEID = "12345678901234567890123456789012";

@InjectMocks
private InternetBackEndRestClient internetBackEndRestClient;

@Mock
private Tracer tracer;
@Mock
private Span span;

@Before
public void setUp() {

MockitoAnnotations.initMocks(this);

when(tracer.nextSpan()).thenReturn(span);
when(tracer.nextSpan().name("spanPasswordJwtResponse"))
.thenReturn(span);
when(span.start()).thenReturn(span);

Tracing tracing = Tracing.newBuilder().build();
doReturn(tracing.tracer().withSpanInScope(span))
.when(tracer).withSpanInScope(span);
doNothing().when(span).finish();

...
}
...
}

关于spring-boot - Spring boot 2.0.5.RELEASE - sleuth 和 mockito,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52677709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com