Recipe Name:
Test framework: Enforce @Rule annotation for a JUnitSoftAssertions field
Description:
A JUnitSoftAssertions field always need to have the @Rule annotation
Level:
error
Language:
  • java
Tags:
  • testing
  • framework specific
  • JUnit 4
  • quality
Documentation

As JUnitSoftAssertions are JUnit rules, fields referencing instances of JUnitSoftAssertions should always be marked with the @Rule annotation.

Before
public MyClass instance = new MyClass();
After
@Rule
public MyClass instance = new MyClass();
References
Recipe
id: scw:java:junit4:JUnitSoftAssertions-rule
version: 10
metadata:
  name: 'Test framework: Enforce @Rule annotation for a JUnitSoftAssertions field'
  shortDescription: A JUnitSoftAssertions field always need to have the @Rule annotation
  level: error
  language: java
  enabled: true
  descriptionFile: descriptions/Test_framework__Enforce__Rule_annotation_for_a_JUnitSoftAssertions_field.html
  tags: testing;framework specific;JUnit 4;quality
search:
  field:
    type: org.assertj.core.api.JUnitSoftAssertions
    without:
      annotation:
        type: org.junit.Rule
availableFixes:
- name: Add @Rule
  actions:
  - rewrite:
      to: |-
        @org.junit.Rule
        {{{ . }}}