title: Jenkins scripted pipeline does not fetch the test result if unit test fails
body: <p>I have gradle based java projects with junit tests which I'm building CI jobs for. I was successfully able to integrate slack with Jenkins using slack Slack Notification plugin.</p>
  
  <p>Jenkins Version : 2.173<br>
  Slack Notification version : 2.20</p>
  
  <p>The jenkins CI is a scripted pipeline, which has the below code:</p>
  
  <pre><code>
  stage('Test') {
          def slackHelper = new com.xyz.jenkins.libraries.SlackNotifier(env)
          try {
              sh "./gradlew test"
              junit 'build/test-results/test/*.xml'
          } finally {
              AbstractTestResultAction testResultAction =  currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
  
              slackHelper.getTestStatuses(currentBuild)
              slackSend(channel: '#ci-cd', attachments: slackHelper.buildUnitTestSlackNotificationMessage())
          }
      }
  </code></pre>
  
  <p>SlackNotifier is a library which has the below code:</p>
  
  <pre><code>/**
   * Calculates test result as a string
   * @param currentBuild : jenkins object, should be passed from jenkins pipeline script
   * @return the final test result as a string
   */
  @NonCPS
  def getTestStatuses(currentBuild) {
      final AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
      if (testResultAction != null) {
          this.total = testResultAction.totalCount
          this.failed = testResultAction.failCount
          this.skipped = testResultAction.skipCount
          this.passed = total - failed - skipped
      }
  }
  
  </code></pre>
  
  <p>buildUnitTestSlackNotificationMessage does this in the same class:</p>
  
  <pre><code>def buildUnitTestSlackNotificationMessage() {
          final JSONObject unitTestResult = new JSONObject()
          unitTestResult.put("fallback", this.jenkinsEnvironment.JOB_NAME + "with build#" + this.jenkinsEnvironment.BUILD_NUMBER + "finish with unit test result : Passed: " + this.passed + " | Failed: " + this.failed + " | Skipped: " + this.skipped )
          unitTestResult.put("color", this.getUnitTestReportColor())
          unitTestResult.put("pretext", "Message from CI job: " + this.jenkinsEnvironment.JOB_NAME + "#" + this.jenkinsEnvironment.BUILD_NUMBER)
          unitTestResult.put("title", "BuildLog")
          unitTestResult.put("title_link", "&lt;&lt;&lt;JenkinsHost&gt;&gt;&gt;" + this.jenkinsEnvironment.JOB_NAME + "/" + this.jenkinsEnvironment.BUILD_NUMBER  + "/console")
          unitTestResult.put("text", "Passed: " + this.passed +  " | Failed: " + this.failed + " | Skipped: " + this.skipped)
          unitTestResult.put("image_url", this.getLogoURL())
          this.attachments.add(unitTestResult)
          return this.attachments.toString()
      }
  </code></pre>
  
  <p>All is well when all the tests pass. But when tests fail I get the below notification:</p>
  
  <pre><code>Message from CI job: &lt;&lt;&lt;JobName&gt;&gt;&gt;#47
  BuildLog
  Passed: null | Failed: null | Skipped: null
  </code></pre>
  
  <p>It turns out <code>testResultAction</code> is null when any unit tests fail here.  </p>
  
  <p>And I'm unable to come to the bottom of this. Please help.</p>