如何使用 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 的方法,在此方法中,我使用了"状态",它是一个枚举类。

@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);获得异常

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 模拟静态方法

最新更新