Unit Testing Atlassian Events

Scott Walter August 24, 2015

I have a Stash plugin that tracks several events that occur on Stash, such as various pull request events, ref changes, etc. I am in the process of writing unit tests for all of these various events which map to methods, for instance:

 

@EventListener
public void opened(PullRequestOpenedEvent event)

..is for when a pull request is opened.

When writing unit tests for these various methods, I need a way to construct a PullRequest object so I can call this method to trigger it for the purposes of testing. Is there a way to do that, because it seems like the constructor for the PullRequest class is private. Or am I thinking of this in the wrong way?

EDIT:

 

I started to dive into the mockito stuff, just want to verify with you guys that I'm going down the right path.

public abstract class MockPullRequest implements PullRequest {
	@Mock private PullRequestParticipant mockAuthor;

	public PullRequestParticipant getAuthor() {
    	when(mockAuthor.getUser().getDisplayName()).thenReturn("Sammy Sosa");
    	when(mockAuthor.getUser().getEmailAddress()).thenReturn("sammy.sosa@gmail.com");
    	return mockAuthor;
	}
}

Here is an example of a single class variable from the PullRequest class and two commands that I run on the Author object. Is this what you guy's were talking about?

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Ulrich Kuhnhardt [IzymesDev]
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.
August 24, 2015

One way is to mock the PR object. Either via your own MockPullRequest implements PullRequest class or by using @Mock  PullRequest provided by the Mockito API. You may choose to mock out all other objects contained in a PR as well.

Adam
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 24, 2015

+1. The essential part of Mockito is that you add the @Mock annotation on a field of your test class and during your tests it'll be populated by an object that pretends to be a PullRequest (or whatever type that field is) but all the methods return null/default values. You can then use static methods on the Mockito API to set up the methods to return whatever data you like (or other mocks) for each test.

Scott Walter August 25, 2015

I gave mockito a look and started writing some code and it looks like the perfect solution, but there seem to be a few ways to employ mockito. From each pullrequestevent I need to extract the pullrequest object and then get almost every single bit of information you can get from a pull request. With that in mind, would it make sense to create MockPullRequest class?

0 votes
Mike Friedrich
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.
August 24, 2015

Why do you want to test Atlassian's code?

Scott Walter August 24, 2015

I don't want to test whether or not atlassian's code works so much as testing that all of my event handlers work. In order to trigger my event handlers in a way that doesn't need to interface with any kind of network, I need to call the method locally (unless you know of another way that doesn't involve standing up some dev instance of stash and calling REST API commands).

TAGS
AUG Leaders

Atlassian Community Events