jenkinsci / cdevents-plugin

The CDEvents Plugin for Jenkins allows interoperability between different CI/CD tools by adopting core CDF project specification
https://plugins.jenkins.io/cdevents/
Apache License 2.0
4 stars 9 forks source link

Improve unit test coverage: BuildCDEvent::buildPipelineRunStartedModel #6

Closed rorybakerfmr closed 7 months ago

rorybakerfmr commented 1 year ago

This issue reserved for Fidelity Open Source Workshop

We would like to increase our unit test coverage, to ensure our methods are returning sensible objects. In this case, we want to ensure BuildCDEvent::buildPipelineRunStartedModel returns a CloudEvent object, specifically one with a "type" field indicative of the Pipeline Run Started event, and the "pipelinename" extension accurately reflecting the getFullDisplayName() return value of Run getParent() result.

To limit the amount of mocking/stubbing, we recommend Mockito to create mocks for the Run / Job / TaskListener objects normally created by Jenkins. You may also used MockedStatic to mock static dependencies outside of the BuildCDEvent class (such as ModelBuilder).

To get started:

  1. Create BuildCDEventTest.java in src/test/java/io/jenkins/plugins/cdevents
  2. Add the Mockito extension to the test class with @ExtendWith(MockitoExtension.class)
  3. Set up your mocks objects for the hudson.model.Run, hudson.model.Job and hudson.model.TaskListener objects we will be passing in to the method under test. (Tip: use Mockito's RETURNS_DEEP_STUBS feature)
  4. In your test method, wrap the test in a try-with-resources block that sets up a MockedStatic instance of ModelBuilder. When ModelBuilder.buildJobModel(...) is called, you may return a new empty JobModel object.
  5. Review the code for buildPipelineRunStartedModel in BuildCDEvent, and set the appropriate when(...).thenReturn(...) behavior needed for your test.
  6. Call BuildCDEvent.buildPipelineRunStartedModel(...). Assert that .getType() and .getExtension("pipelinename") have the correct values.
rorybakerfmr commented 1 year ago

I would like to address this issue