mockito 创建构建Junit时匹配器的使用无效

3bygqnnd  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(149)

在将JDK 1.8升级到17后,一些Junit在创建构建时出现错误,但在独立运行该Junit时,其工作正常。

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] MyclassTest.setUp:31 InvalidUseOfMatchers Misplaced or mis... [INFO] [ERROR] Tests run: 750, Failures: 0, Errors: 1, Skipped: 0

w8rqjzmb

w8rqjzmb1#

正如我所看到的,你混合了junt4和junit5
@Before已退出4
@AfterEach是junit5
您应该将@Before更改为@BeforeEach

相关问题