Search code examples
javamockitojunit5java-17

Invalid use of Matcher while Creating build Junit


After upgrading JDK 1.8 to 17 some of the Junit are giving error while Creating build however when run that Junit independently its working fine .

public class MyClassTest{
@InjectMocks
MyClass myClass;
@Mock
AlertRuleMappingUtility alertRuleMappingUtility;
AutoCloseable closeable;
@Before
public void setUp()  {
    closeable = MockitoAnnotations.openMocks(this);
}
@AfterEach
public void close() throws Exception {
      closeable.close();
  }
@Test
public void getDescriptionTextTest0()  throws Throwable  {
Map<String, String> map = new HashMap<>();
map.put("alert.secured_borrowings", "xyz");
myClass.setAlertType("secured_borroWings");
doReturn(map).when(alertRuleMappingUtility).fetchAlertTypeAndLongDescMapping(any());
String result = myClass.getDescriptionText();
assertNotNull(result);
}
}

Error i'm getting is ERROR] MyclassTest.setUp:31 InvalidUseOfMatchers Misplaced or mis... [INFO] [ERROR] Tests run: 750, Failures: 0, Errors: 1, Skipped: 0


Solution

  • As I can see, you mix junt4 and junit5

    @Before is junit4

    @AfterEach is junit5

    You should change @Before to @BeforeEach