0

I am trying to test following code with JMockito.

Code

@RunWith(MockitoJUnitRunner.class)
public class StepFunctionClientTest {

    private static final String TEST_STATE_MACHINE_ARN = "testStateMachineArn";
    private static final String TEST_EXECUTION_ARN = "testExecutionArn";
    private ObjectNode additionalData;
    private ObjectMapper objectMapper;

    @Mock AWSStepFunctions mockAWSStepFunctions;
    private StepFunctionClient stepFunctionClient;

    @Before
    public void setUp() throws Exception {
        stepFunctionClient = new StepFunctionClient(mockAWSStepFunctions, TEST_STATE_MACHINE_ARN);
        additionalData = TestData.getObjectNode(TestData.ADDITIONAL_DATA);
        objectMapper = new ObjectMapper();
    }

    @Test(expected=InternalServiceException.class)
    public void test() throws Exception {
        StartExecutionResult result = new StartExecutionResult();
        result.setExecutionArn(TEST_EXECUTION_ARN);

        when(mockAWSStepFunctions.startExecution(any())).thenThrow(new JsonProcessingException("Error"){});

        stepFunctionClient.startExecution(
                TEST_ORDER_ID,
                TEST_PAYMENT_STATUS,
                null,
                false);
    }
}

Error

[junit] Testcase: test(com.project.orderservice.client.StepFunctionClientTest):       Caused an ERROR
[junit] Unexpected exception, expected<com.project.commons.exceptions.InternalServiceException> but was<org.mockito.exceptions.base.MockitoException>
[junit] java.lang.Exception: Unexpected exception, expected<com.project.commons.exceptions.InternalServiceException> but was<org.mockito.exceptions.base.MockitoException>
[junit]     at org.mockito.internal.runners.DefaultInternalRunner$1.run(Unknown Source)
[junit]     at org.mockito.internal.runners.DefaultInternalRunner.run(Unknown Source)
[junit]     at org.mockito.internal.runners.StrictRunner.run(Unknown Source)
[junit]     at org.mockito.junit.MockitoJUnitRunner.run(Unknown Source)
[junit] Caused by: org.mockito.exceptions.base.MockitoException: 
[junit] Checked exception is invalid for this method!
[junit] Invalid: com.project.orderservice.client.StepFunctionClientTest$1: Error
[junit]     at com.project.orderservice.client.StepFunctionClientTest.test(StepFunctionClientTest.java:157)

What i am missing here?

2
  • add the full stacktrace, and the entire test class for better picture Commented Jan 12, 2018 at 17:21
  • updated with full code and stacktrace Commented Jan 12, 2018 at 17:29

1 Answer 1

1

JsonProcessingException is a checked exception, meaning that if you want to throw it, the method has to have it in its signature. Otherwise you get the error above:

Checked exception is invalid for this method!

You can throw only the checked exception listed here -> StartExecutionRequest.

Or a RuntimeException.

Sign up to request clarification or add additional context in comments.

4 Comments

thansk for pointing it out. after a little closer look i noticed that JsonProcessingException is being thrown by the ObjectMapper which is not being mocked in my testcases.
just add a @Mock annotation then instead of creating the real instance
what do you mean? @Mock annotation where? The ObjectMapper is being created internally in StepFunctionClient. Can i mock an object which is not being injected from outside?
If its an instance field then you could use the @InjectMocks on the client

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.