gpt4 book ai didi

java - Mockito 测试 - SimpMessageHeaderAccessor 不能由 getHeaders() 返回 getHeaders() 应该返回 MessageHeaders

转载 作者:行者123 更新时间:2023-11-28 21:19:12 25 4
gpt4 key购买 nike

如何编写单元测试,因为它包括内部调用其他方法。

public class MyService implements UserDestinationResolver {

@Override
public UserDestinationResult myMethod(Message<?> message){
SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);

****some logics to use access to get the return value UserDestinationResult***

return UserDestinationResult;

这里的getAccessor是MessageHeaderAccessor类的一个静态方法

@Nullable
public static <T extends MessageHeaderAccessor> T getAccessor(Message<?> message, Class<T> requiredType) {
return getAccessor(message.getHeaders(), requiredType);
}

这是我的测试用例:

@RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {

@Mock
private Message<?> message;

private SimpMessageHeaderAccessor accessor;

@Mock
private UserDestinationResult userDestinationResult;

@InjectMocks
private MyService myService;

@Before
public void set_up(){
accessor = SimpMessageHeaderAccessor.wrap(message);
}

@Test
public void resolveDestination(){
when(MessageHeaderAccessor.getAccessor(message,
SimpMessageHeaderAccessor.class)).thenReturn(accessor);

"""""""""""""""""""""""""""""""
assertEquals(excepted, actual);
}

发生错误:

SimpMessageHeaderAccessor cannot be returned by getHeaders() getHeaders() should return MessageHeaders If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because: 1. This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing. 2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

Message是一个接口(interface),我把它当成一个实例mock了,但是它根本获取不到headers,它是一个mock...我该如何解决这个问题..

最佳答案

您不能使用 vanilla mockito 模拟静态方法。

我建议在 MyService 中创建一个 protected 方法来包装静态调用,然后模拟它:

public class MyService implements UserDestinationResolver {

@Override
public UserDestinationResult myMethod(Message<?> message){
SimpMessageHeaderAccessor accessor = getAccessor(message, SimpMessageHeaderAccessor.class);
...
}

protected SimpMessageHeaderAccessor getAccessor(Message<?> message, Class<T> requiredType){
return SimpMessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
}

然后在测试中:

@RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {

@Mock
private Message<?> message;


@Mock
private UserDestinationResult userDestinationResult;

@InjectMocks
@Spy
private MyService myService;

@Before
public void set_up(){
accessor = SimpMessageHeaderAccessor.wrap(message);
}

@Test
public void resolveDestination(){
// Arrange
doREturn(accessor).when(myService).getAccessor(message,
SimpMessageHeaderAccessor.class));

....
}

这里的变化是您需要监视 MyService 才能模拟 protected 方法。

关于java - Mockito 测试 - SimpMessageHeaderAccessor 不能由 getHeaders() 返回 getHeaders() 应该返回 MessageHeaders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54253405/

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