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?
Community moderators have prevented the ability to post new answers.
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.
+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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Why do you want to test Atlassian's code?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.