2

I'm trying to mock HttpStatus methods using mockito

@Autowired
Handler handler;

@Mock
Request request;

@Mock
Response aAResponse;

@Mock
RestHandler restHandler

logger.info("Response's HttpStatus is2xxSuccessful is false");
    try {
        Mockito.when(restHandler.getResponse(request)).thenReturn(aAResponse);
        Mockito.when(aAResponse.getHttpStatus()).thenReturn(HttpStatus.ACCEPTED);
        Mockito.when(aAResponse.getHttpStatus().is2xxSuccessful()).thenReturn(false);
        handler.fetchDetails(notification, provider, mapping);
    } catch (Exception e) {
        logger.error("Exception  -> {}", e.getMessage());
    }

But

aAResponse.getHttpStatus()

is giving me null. How to mock enum values and its methods using Mockito. Is it correct to mock enum values and its methods?

final Response response = restHandler.getResponse(request);

I know response is final and mockito can not mock final objects but for that I followed the answer https://stackoverflow.com/a/53837478/4478171

user387600
  • 99
  • 3
  • 15
  • 2
    Personally, I'd mock the result of `getHttpStatus()` to return a successful status – Fred Jan 28 '20 at 09:19
  • Can you show us your full test class? – Imaguest Jan 28 '20 at 09:21
  • Does this answer your question? [Mocking Java enum to add a value to test fail case](https://stackoverflow.com/questions/5323505/mocking-java-enum-to-add-a-value-to-test-fail-case) – SteffenJacobs Jan 28 '20 at 09:22
  • @Fred Tried. But of no use. 'Mockito.when(aARespose.getHttpStatus()).thenReturn(HttpStatus.ACCEPTED)' – user387600 Jan 28 '20 at 09:28
  • @SteffenJacobs No. I want to mock enum methods. – user387600 Jan 28 '20 at 09:29
  • @user387600 what didn't work out? – Fred Jan 28 '20 at 09:33
  • @Fred Its not returning mock HttpStatus, instead returning the HttpStatus from the response. – user387600 Jan 28 '20 at 09:39
  • is `getHttpStatus` final by any chance? – Fred Jan 28 '20 at 10:05
  • @Fred No, its a public method. – user387600 Jan 28 '20 at 10:17
  • How do you instantiate `restHandler` and `aAResponse`? – Imaguest Jan 28 '20 at 10:27
  • I'm mocking both objects using `@Mock` – user387600 Jan 28 '20 at 10:28
  • It's complicated to help you without more details or source code. I think the problem is that your mocked `restHandler` is not injected and therefore not used when you call `handler.fetchDetails`. Maybe I'm wrong, complicated to say without details – Imaguest Jan 28 '20 at 10:39
  • @Imaguest Edited my question. I'm mocking `restHandler. – user387600 Feb 20 '20 at 09:17
  • the problem is `handler`. How do you get `handler` instance? What is `handler`? Be precise when you ask something. – Imaguest Feb 20 '20 at 09:36
  • Its just a class which i'm mocking in my test class. – user387600 Feb 20 '20 at 09:39
  • It's impossible to say what it's wrong here.. You don't provide enough informations / code. Just to say: It's not enough to mock everything and expecting that your mocked instances will magically interact when you call `handler.fetchDetails`. Are you injecting `RestHandler` into this so secret `handler` instance? Are you constructing `handler` passing your mocked `restHandler` instance? – Imaguest Feb 20 '20 at 09:46
  • `Handler` is the class under test. So I'm autowiring it in my test class. And `RestHandler` is another class which is autowired in `Handler` class. What exactly do you need. If possible I will share the info. – user387600 Feb 20 '20 at 10:00
  • Ok, and you don't do anything else with your `handler`? Just autowiring it? If it's the case, you've got your answer. You're mocking things that will never be used when calling `handler.fetchDetails`. If you autowire an `Handler` instance and this instance autowires `RestHandler`, you have to say it: hey `Handler` instance, you don't have to go for the `RestHandler` instance you usualy go for, take this mocked one instead. If you're using Spring boot, you can take a look at `@InjectMocks` annotation [here an example](https://howtodoinjava.com/mockito/mockito-mock-injectmocks/) – Imaguest Feb 20 '20 at 10:06

1 Answers1

0

Why would you mock the enum when you can mock the method that return the enum?

Replace

Mockito.when(aAResponse.getHttpStatus().is2xxSuccessful()).thenReturn(false);

with

Mockito.when(aAResponse.getHttpStatus()).thenReturn(HttpStatus.NOT_FOUND);

You don't want a mocked enum to return a specific value. You want the response to have a status with a non-successfull error-code...

Nicktar
  • 5,548
  • 1
  • 28
  • 43
  • I tried this too. But its not working. Its not mocking the getHttpStatus() method instead returning the original value i.e., 200 Ok. – user387600 Jan 28 '20 at 09:35