-1

This application is developed using Spring Boot. I have a controller class named BridgeController where I'm performing a POST call.

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

Service Class named BridgeService where it is having a method named status and inside this method, I have used "Status" which is an enum class.

@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;
}

This is my Test Class

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");  
    }
}

My Enum is public enum Status {

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;
}

} Getting an exception at 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.

Can anyone help me in fixing this issue?

Tusar
  • 13
  • 1
  • 1
  • 8
  • You cannot mock an enum. – M. Deinum Feb 10 '20 at 12:03
  • 2
    Yes, you can't create a mock for a `final` class, and an `enum` is just a special case of a `final` class. – daniu Feb 10 '20 at 12:03
  • 3
    Why would you possibly want to mock an Enum class? Why not use the Enum itself? – hovanessyan Feb 10 '20 at 12:06
  • I got to know that we can mock an enum using Mockito 2 – Tusar Feb 10 '20 at 12:24
  • Consider that your `static` method is never supposed to return `null` in a valid scenario. What are you trying to test here? – second Feb 10 '20 at 19:39
  • For valid scenario static method is not returning null, Its returning status reference here I'm checking that status is not null. when(Status.fromId(mappings.getStatusId())).thenReturn(status); Here fromId method is a static method so I'm unable to mock it. Is there any way to mock this – Tusar Feb 11 '20 at 06:28
  • It is possible for a while now: [39. Mocking final types, enums and final methods (Since 2.1.0)](https://www.javadoc.io/static/org.mockito/mockito-core/2.28.1/org/mockito/Mockito.html#Mocking_Final) – snv Jan 18 '22 at 09:20

1 Answers1

0

The problem is you are trying to mock static methods. You won't be able

Why doesn't Mockito mock static methods?

You might want to use Dependency Injection more broadly. Anyway, if still you really want to follow that path, use Powermockito

Mocking static methods with Mockito

inquirymind
  • 135
  • 1
  • 11