3

I have this complicated method. I want to mock just the result. All what is inside should basically be ignored. I am using Mockito .

class JiraManager
{
    public static  List<Issue> searchMitarbeiterIssue(String mitarbeiterName) throws JqlParseException, SearchException {
        ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
        JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class);
        SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
        String jqlSearchString = "project = BLUB AND issuetype = BLOB AND text ~ \"" + myName+ "\""+" AND status = aktive";
        final Query query = jqlQueryParser.parseQuery(jqlSearchString);
        List<Issue> issues = null;
        Query queryToExecute = JqlQueryBuilder.newBuilder(query).buildQuery();
        // get results for the jql query
        SearchResults searchResult = searchService.search(user, queryToExecute, PagerFilter.getUnlimitedFilter());
        try {
            Method newGetMethod = null;
            try {
                newGetMethod = SearchResults.class.getMethod("getIssues");
            } catch (NoSuchMethodException e) {
                try {
                    LOGGER.warn("SearchResults.getIssues does not exist - trying to use getResults!");
                    newGetMethod = SearchResults.class.getMethod("getResults");
                } catch (NoSuchMethodError e2) {
                    LOGGER.error("SearchResults.getResults does not exist!");
                }
            }
            if (newGetMethod != null) {
                issues = (List<Issue>) newGetMethod.invoke(searchResult);
            } else {
                LOGGER.error("ERROR NO METHOD TO GET ISSUES !");
                throw new RuntimeException("ICT: SearchResults Service from JIRA NOT AVAILABLE (getIssue / getResults)");
            }
        } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            LOGGER.error("Jql Helper can not get search result (ICT)", e);
        } catch (Exception e) {
            LOGGER.error("Jql Helper can not get search result - other exception (ICT)", e);
        }
        return issues;
    }
}

I do not want Mockito to run all the Code inside the method. It should just return the List . That's all . So I tried this:

try (MockedStatic<JiraManager> utilities = Mockito.mockStatic(JiraManager.class)) {
    utilities.when(() -> JiraManager.searchMitarbeiterIssue(any()))
            .thenReturn(Collections.singletonList(mitarbeiterIssueResult));
    assertTrue(JiraManager.searchMitarbeiterIssue("").size() == 1);
} 

But it is not working. It always returns null. Why ?? Is the code inside the method executed ? What is Mockito exactly doing ?

Toni26
  • 489
  • 4
  • 11

1 Answers1

5

Below works for me.

  1. Create MockedStatic class field
private MockedStatic<MyUtilClassWithStaticMethods> myUtil;
  1. Initialise MockedStatic before each test case
@BeforeEach
void initialiseWorker() {
    myUtil = mockStatic(MyUtilClassWithStaticMethods.class);
}
  1. Close MockedStatic after each test case
@AfterEach
public void close() {
    myUtil.close();
}
  1. Mock static method behaviour in test case
@Test
void test() {
    when(MyUtilClassWithStaticMethods.staticMethod(any())).thenReturn(null);
}

You can return list instead of null here.

Hardik
  • 115
  • 1
  • 8
  • Interestingly your solution works !!! Thanks a lot. But why is my solution not working. Hm . But thanks anyway. – Toni26 Jul 09 '21 at 20:29
  • Your solution : You don't call when on the MockedStatic object. That is the difference. No idea why there is a when method on the MockedStatic object if it does not work. – Toni26 Jul 09 '21 at 20:36