# Testing Jira connections

Jira mocks - JQL results, issue creation and empty result handling.

When your Zato service queries Jira for issues, creates tickets, or updates workflows, you need to test that logic without hitting the real Jira API. This page shows how to mock Jira responses so your tests run fast and don't require Jira credentials.

**Note:** If you're new to unit testing with Zato, check the [tutorial](https://zato.io/docs/api-testing/tutorial.html) first.

## Basic usage {#basic-usage}

Services access Jira connections like this:

```python
from zato.server.service import Service

class GetJiraIssues(Service):
    name = 'jira.issues.get'
    input = 'project'

    def handle(self):
        jira = self.cloud.jira['my-jira']

        with jira.conn.client() as client:
            results = client.jql(
                jql=f'project={self.request.input.project}',
                fields=['key', 'summary', 'status']
            )

        self.response.payload = {
            'issues': [
                {'key': r['key'], 'summary': r['summary']}
                for r in results.get('issues', [])
            ]
        }
```

Mock the Jira results in your test:

```python
from zato_testing import ServiceTestCase
from myapp.services import GetJiraIssues

class TestGetJiraIssues(ServiceTestCase):

    def test_returns_issues(self):

        # Configure Jira JQL results
        self.set_response('jira:my-jira', {
            'issues': [
                {'key': 'PROJ-001', 'summary': 'Fix login bug'},
                {'key': 'PROJ-002', 'summary': 'Add new feature'},
                {'key': 'PROJ-003', 'summary': 'Update documentation'}
            ]
        })

        service = self.invoke(GetJiraIssues, project='PROJ')

        issues = service.response.payload['issues']
        self.assertEqual(len(issues), 3)
        self.assertEqual(issues[0]['key'], 'PROJ-001')
```

Note the `jira:` prefix to distinguish Jira connections from other connection types.

## Creating issues {#creating-issues}

Mock issue creation:

```python
class CreateJiraIssue(Service):
    name = 'jira.issue.create'
    input = 'project', 'summary', 'description'

    def handle(self):
        jira = self.cloud.jira['my-jira']

        with jira.conn.client() as client:
            result = client.create_issue(
                project=self.request.input.project,
                summary=self.request.input.summary,
                description=self.request.input.description
            )

        self.response.payload = {'issue_key': result['key']}
```

```python
class TestCreateJiraIssue(ServiceTestCase):

    def test_creates_issue(self):

        self.set_response('jira:my-jira', {'key': 'PROJ-004'})

        service = self.invoke(CreateJiraIssue,
            project='PROJ',
            summary='New issue',
            description='Issue description'
        )

        self.assertEqual(service.response.payload['issue_key'], 'PROJ-004')
```

## Empty results {#empty-results}

Test handling of no issues found:

```python
class TestNoIssues(ServiceTestCase):

    def test_handles_empty_results(self):

        self.set_response('jira:my-jira', {'issues': []})

        service = self.invoke(GetJiraIssues, project='EMPTY')

        self.assertEqual(service.response.payload['issues'], [])
```

## Learn more {#learn-more}

- [API testing](https://zato.io/docs/api-testing/) - Tests in plain English, run against real or mocked connections
- [API testing tutorial](https://zato.io/docs/api-testing/tutorial.html) - A complete test suite, from the first scenario to the report
- [Testing REST connections](https://zato.io/docs/api-testing/rest.html) - Mock outgoing REST calls and assert on what your service sent
- [Testing SQL connections](https://zato.io/docs/api-testing/sql.html) - Mock database queries and the rows they return
- [Testing LDAP connections](https://zato.io/docs/api-testing/ldap.html) - Mock directory lookups and the entries they find
- [Testing Microsoft 365](https://zato.io/docs/api-testing/ms365/) - Mock mailboxes, calendars, SharePoint, Teams and the rest
- [Testing service invocations](https://zato.io/docs/api-testing/services.html) - Services that call other services, with the callees mocked
- [Tests in CI/CD](https://zato.io/docs/api-testing/ci-cd.html) - Running the suite in a pipeline and reading its output
