Annotate setUp() and tearDown() in JUnit 4 and 5 test classes

Proprietary

Adds @Before or @After to a setUp() or tearDown() method of a JUnit 4 test class, and @BeforeEach or @AfterEach in a JUnit 5 one, where the method carries no lifecycle annotation. Unlike JUnit 3, those versions run such a method only when it is annotated, so it never ran before. A class with any JUnit 5 test is treated as JUnit 5, as SonarQube does, so in a class that mixes both only the JUnit 5 tests start running it. The method is left alone when anything in the source set calls it or refers to it, since it would then run twice, when it overrides another method or its supertypes are not all known, when the class already has a method with the same annotation, whose order relative to it JUnit does not fix, and when JUnit would reject the annotated method: one that is static, abstract, takes parameters or returns a value, or is not public under JUnit 4 or is private under JUnit 5. It is also left alone when the method carries an annotation from outside java.lang, such as @Test or @PostConstruct, since a framework may already run it, or when the annotation's simple name may resolve to something else: another type of that name in use in the file, a type variable, a member type of a supertype, or a type in the file's package or behind a star import. Behavior changes by design: the method now runs around each test.

io.moderne.staticanalysis.AnnotateSetUpAndTearDown
mod run . --recipe=io.moderne.staticanalysis.AnnotateSetUpAndTearDown

Install it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.