How to mock the IssueService ?

Robert Pastor March 10, 2019

Hello,

The doGet method of my servlet performs a

IssueResult issueResult = issueService.getIssue(user, issueKey);

hence I did imagine to mock the IssueService ?

However Mockito does not allow to mock IssueService ?

How to do this ?

Thanks for your help

Robert

1 answer

0 votes
Santwana Sarangi {Appfire}
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 11, 2019

Hi @Robert Pastor,

Are you passing "issueService" as an argument to the servlet constructor or creating an object through sal api ComponetAccessor or ComponentLocator?

If you are passing as an argument, then you can mock the issueService class with Mockito.mock(IssueService.class).

If you are using sal api ComponetAccessor or ComponentLocator, then, please check this link https://docs.atlassian.com/DAC/javadoc/sal/2.6/reference/com/atlassian/sal/testresources/component/MockComponentLocator.html.

Hope this helps!!

Thanks,

Santwana

Robert Pastor March 12, 2019

Dear Santwana,

 Yes, I am passing issueService as an argument to the servlet constructor.

@Scanned
public class IssueCRUD extends HttpServlet {

private static final Logger log = LoggerFactory.getLogger(IssueCRUD.class);

@JiraImport
private IssueService issueService;

public IssueCRUD(IssueService issueService,
ProjectService projectService,
SearchService searchService,
TemplateRenderer templateRenderer,
JiraAuthenticationContext authenticationContext,
ConstantsManager constantsManager) {


this.issueService = issueService;

..

 

Here is my test code

@Before
public void setup() {

mockRequest = Mockito.mock(HttpServletRequest.class);
mockResponse = Mockito.mock(HttpServletResponse.class);

fred = new MockApplicationUser("Fred");
jiraAuthenticationContext = Mockito.mock(JiraAuthenticationContext.class);

Mockito.when(jiraAuthenticationContext.getLoggedInUser()).thenReturn(fred);

IssueService issueService = Mockito.mock(IssueService.class);

.......

launching atlas-unit-test

I got several errors

[ERROR] Found a type [com.atlassian.plugin.spring.scanner.annotation.imports.JiraImport] annotated as a component, but the type is not a concrete class. NOT adding to index file!!

............

finally the error right on the line 102 of the mock of the IssueService class

----------------

testSomething(ut.com.thales.IssuePrime.servlet.IssueCRUD001Test) Time elapsed: 0.12 sec <<< ERROR!
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: interface com.atlassian.jira.bc.issue.IssueService.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.


Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.5-b02
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_05-b13
JVM info : mixed mode
OS name : Windows 7
OS version : 6.1


Underlying exception : java.lang.IllegalArgumentException: object is not an instance of declaring class
at ut.com.thales.IssuePrime.servlet.IssueCRUD001Test.setup(IssueCRUD001Test.java:102)

Thanks for your help

Robert

Suggest an answer

Log in or Sign up to answer