Recipe Name:
Use AssertJ ThrowingCallable assertions (JUnit v4)
Description:
Use AssertJ ThrowingCallable assertions (JUnit v4)
Level:
warning
Language:
  • java
Tags:
  • AssertJ
Recipe
id: scw:assertj:junit-assert-throws-v4
version: 10
metadata:
  name: Use AssertJ ThrowingCallable assertions (JUnit v4)
  shortDescription: Use AssertJ ThrowingCallable assertions (JUnit v4)
  level: warning
  language: java
  enabled: true
  comment: 'NB: assertThatThrownBy(code).as(description).isInstanceOf(throwableClass) is not offered as a migration because it can''t use the description if no exception is thrown by the code.'
  tags: AssertJ
search:
  methodcall:
    args:
      1:
        type: String
      2:
        type:
          reference:
            matches: java\.lang\.Class<.*>
          checkInheritance: true
    argCount: 3
    name: assertThrows
    type:
      reference:
        matches: org\.junit\.Assert
      checkInheritance: true
availableFixes:
- doStaticImports: true
  name: 'Use AssertJ: assertThatCode(code).isInstanceOf(throwableClass)'
  actions:
  - rewrite:
      to: org.assertj.core.api.Assertions.assertThatCode({{{ arguments.2 }}}).as({{{ arguments.0 }}}).isInstanceOf({{{ arguments.1 }}})
      target: self
- doStaticImports: true
  name: 'Use AssertJ: assertThatExceptionOfType(throwableClass).isThrownBy(code)'
  actions:
  - rewrite:
      to: org.assertj.core.api.Assertions.assertThatExceptionOfType({{{ arguments.1 }}}).as({{{ arguments.0 }}}).isThrownBy({{{ arguments.2 }}})
      target: self