I am trying to mock out an Issue in JIRA that has a custom field.
@Before public void setUp() { customFieldManager = mock(CustomFieldManager.class); customField = mock(CustomField.class); issue = mock(Issue.class); componentAccessor = mock(ComponentAccessor.class); new MockComponentWorker() .addMock(ComponentAccessor.class, new ComponentAccessor()) .addMock(CustomFieldManager.class, customFieldManager) .addMock(CustomField.class, customField) .addMock(Issue.class, issue) .init(); when(ComponentAccessor.getCustomFieldManager()).thenReturn(customFieldManager); when(customFieldManager.getCustomFieldObjectByName("myCustomField")).thenReturn(customField); when(issue.getCustomFieldValue(customField)).thenReturn("1000:1000"); }
I get this error
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. 2. inside when() you don't call method on mock but on some other object. 3. the parent of the mocked class is not public. It is a limitation of the mock engine.
which shows triggers at line
when(ComponentAccessor.getCustomFieldManager()).thenReturn(customFieldManager);
Im new to mockito, I have been able to get it work with more simple examples that do not use the field manager and accesserr.
The method im trying to Unit test is a Method that searches for a Custom Field and pulls it values. It goes simular to the following:
public static String findCustomField(Issue jiraIssue) { CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager(); CustomField customField = customFieldManager.getCustomFieldObjectByName("myCustomField"); Object teamTrackItemId = jiraIssue.getCustomFieldValue(customField); Issue parentIssue = jiraIssue.getParentObject(); if (myCustomField!= null) { if (teamTrackItemId.toString().contains(":")) { return teamTrackItemId.toString(); } } else if (parentIssue != null) { Object myCustomField= parentIssue.getCustomFieldValue(customField); if (myCustomField!= null) { if (myCustomField.toString().contains(":")) { return myCustomField.toString(); } } } return null; }
I am stumped on what is going wrong with this.
When I run something that just pulls that mocked field itself
For example:
@Before public void setUp() { customField = mock(CustomField.class); issue = mock(Issue.class); new MockComponentWorker() .addMock(CustomField.class, customField) .addMock(Issue.class, issue) .init(); when(issue.getCustomFieldValue(customField)).thenReturn("1000:1000"); } @Test public void retrievingCorrectCustomFieldValue() { System.out.println(issue.getCustomFieldValue(customField)); String customField = issue.getCustomFieldValue(customField).toString(); assertEquals("1000:1000", customField); }
It pulls the customField value. Its wont if I try to pull the field with my method itself that im trying to unit test that has to deal with the Manager and Accssor Class.
Any advice?
Thanks
Community moderators have prevented the ability to post new answers.
You cannot mock static things like ComponentAccessor directly with mockito. The use of the MockComponentWorker is already taking care of this. The line
when(ComponentAccessor.getCustomFieldManager()).thenReturn(customFieldManager);
Simply isn't needed. You are already accomplishing this by adding it to the MockComponentWorker.
As an alternative, you might find the Mockito @Rule easier to use. It does the same thing using annotations instead: https://docs.atlassian.com/jira/latest/com/atlassian/jira/junit/rules/MockitoContainer.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.