Accessing the test instance when using TestKit

I’m hoping someone can point me in the right direction for integration testing with TestKit, because I’m struggling against the lack of documentation / working examples.

In attempting to write the most basic skeleton test, I’m hitting up against a ComponentAccessor has not been initialised error, but none of the material I have found online seems to address this issue in the context of TestKit.

Could someone explain what I am supposed to do instead of the (non-functional) example below? Any help greatly appreciated!

package it;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.testkit.client.Backdoor;
import com.atlassian.jira.testkit.client.util.TestKitLocalEnvironmentData;
import com.atlassian.jira.testkit.client.util.TimeBombLicence;

import static org.junit.Assert.assertTrue;

import java.util.Set;

import org.junit.Test;

public class LgtmServletTest {
	
	Backdoor testKit;
	
	public LgtmServletTest() {
		
		testKit = new Backdoor(new TestKitLocalEnvironmentData());
		testKit.restoreBlankInstance(TimeBombLicence.LICENCE_FOR_TESTING);
		testKit.usersAndGroups().addUser("test-user");
		testKit.project().addProject("Testing Testing", "TT", "test-user");
	}

    @Test
    public void testCreationOfProject() {
    	
    	// interact with the plugin being tested
    	// resulting in creation of a ticket in project TT
    	
    	Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("TT");
    	
    	Set<String> issueKeys = ComponentAccessor.getIssueManager().getAllIssueKeys(project.getId());
    	
    	assertTrue( 0 < issueKeys.size() );
    }
    
}

1 Like

Tagging staff directly might be rather irregular, but…

@izinoviev – as the most recent committer to the TestKit repo might you be able to shed some light? :slight_smile:

Hi @fyse

:thinking:
ComponentAccessor is an internal class of Jira available inside of Jira’s JVM.

On the other hand, TeskKit is a library to perform func tests on running Jira, by calling Jira’s REST APIs. (plublic, internal or even hacky backdoors available only for tests).
So, if your goal is to write a test using testkit, you should communicate with Jira via controls provided by testkit.
Smth like

Backdoor testKit;
final Project project = testKit.project().getProject("TT");
final List<Issue> issues = node.backdoor().search().getSearch(new SearchRequest().jql("project = TT ")).issues;

etc.

Please note that testkit is limited by Jira REST APIs, so not all features from Jira Java API may be available.

Hope it helps a bit.
If not, we’ll be happy to answer other questions.


Ilya Zinoviev
Jira Development

2 Likes

Thanks for your response, hugely appreciated!

That makes a lot of sense, it certainly seemed like I was just approaching things in the wrong way.

But I think part of my confusion is that I couldn’t seem to find any get methods in TestKit, which is what I initially expected. For example testKit.project().getProject() doesn’t seem to exist?

Also, should the following work? I’m just getting an error java.lang.NoClassDefFoundError: org/springframework/util/ReflectionUtils$FieldFilter.

final List<Issue> issues = testKit.search().getSearch(new SearchRequest().jql("project = TT ")).issues;

Thanks for your help! Could all these problems be because I’m missing vital dependencies / have POM errors??

Managed to get to the bottom of the the spring-related error, which was addressed by adding spring-context as a test dependency…

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.4.RELEASE</version>
    <scope>test</scope>
</dependency>

@izinoviev Is that expected, or should that have been provided by the TestKit framework?

Also, it doesn’t seem like testKit.project().getProject() is in the latest version of TestKit. Does that mean there’s an enhanced version on the way soon…? :slight_smile:

@fyse

Also, it doesn’t seem like testKit.project().getProject() is in the latest version of TestKit. Does that mean there’s an enhanced version on the way soon…? :slight_smile:

Yes, sorry for misinformation =)
Indeed those fancy methods exist as a part of Jira’s internal library called jira-func-tests
It seems that it should be publicly available, so I would suggest you to try it out.

<dependency>
    <groupId>com.atlassian.jira</groupId>
    <artifactId>jira-func-tests</artifactId>
    <version>your_jira_version</version>
</dependency>

It also includes com.atlassian.jira.functest.framework.BaseJiraFuncTest from which you can inherit your tests. Then you’ll be able to use fancy methods in your tests =)

backdoor.project().getProject("KEY");