繁体   English   中英

如何使用 Mockito 2 模拟枚举类

[英]How to Mock an Enum Class using Mockito 2

此应用程序是使用 Spring Boot 开发的。 我有一个名为 BridgeController 的控制器类,我在其中执行 POST 调用。

@PostMapping(path = STATUS, produces = MediaType.APPLICATION_JSON)
public Response status(@RequestBody final Request request) {
    return this.bridgeService.status(request);
}

名为 BridgeService 的服务类,它有一个名为 status 的方法,在这个方法中,我使用了“Status”,它是一个枚举类。

@Override
public Response status(final request request) {
    final String id = request.getId();
    final Response response = Response.build();
    final Status status = Status.fromId(mappings.getId());
    if (null == status) {
        response.setStatus(RequestStatus.FAILURE);
        response.setStatusMsg(
                "Unable to find a valid mapping for the status id : " + mappings.getStatusId());
    } else {
        response.setResponse(status.getName());
    }
    return response;
}

这是我的测试班

public class BridgeControllerTest extends BaseTest {
    MockMvc mockMvc;

    @Autowired
    WebApplicationContext context;

    @InjectMocks
    BridgeController bridgeController;

    @MockBean
    request request;

    @Mock
    Status status;

    @Mock
    BridgeService bridgeService;

    ObjectMapper objmapper = new ObjectMapper();

    @Before
    public  void setUp(){
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void statusSuccessTest() throws JsonProcessingException, Exception{  
        Mappings mappings = Mappings.build();
        when(statusRequest.getId()).thenReturn("12345");
        when(Status.fromId(mappings.getStatusId())).thenReturn(status);
        MvcResult result=this.mockMvc.perform(post("/status")
                .content(objmapper.writeValueAsString(request))
                .contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
        String resultContent = result.getResponse().getContentAsString();      
        AppResponse appResponse = objmapper.readValue(resultContent, Response.class);
        assertEquals("Request processed successfully", Response.getStatusMsg());
        Assert.assertTrue(Response.getStatus().getValue()=="SUCCESS");  
    }
}

我的枚举是公共​​枚举状态 {

PENDING(1, "PENDING"), CONFIRMED(2, "CONFIRMED"), DECLINED(3, "DECLINED");

private final int id;

private final String name;

Status(final int id, final String name) {
    this.id = id;
    this.name = name;
}
public int getId() {
    return this.id;
}
public String getName() {
    return this.name;
}

@JsonCreator
public static Status fromId(final int id) {
    for (final Status status : Status.values()) {
        if (status.getId() == id) {
            return status;
        }
    }
    return null;
}

when(AAStatus.fromId(requestMappings.getStatusId())).thenReturn(null);处获得异常when(AAStatus.fromId(requestMappings.getStatusId())).thenReturn(null);

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
    Those methods *cannot* be stubbed/verified.
    Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

谁能帮我解决这个问题?

问题是您正在尝试模拟静态方法。 你将无法

为什么 Mockito 不模拟静态方法?

您可能希望更广泛地使用依赖注入。 无论如何,如果您仍然真的想走这条路,请使用 Powermockito

使用 Mockito 模拟静态方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM