junit测试用例通过,但仍在控制台java中打印异常


@Test
public void testGetAspectByAspectName() {
try {
this.getUserTwin();
assertEquals(userAlmService.getAspectByUserName("6cade-bced-4e69-8red-271c95d343baa", null).toString(),this.getAspect().toString());
}
catch (NullPointerException e) {
assertEquals(NullPointerException.class, e.getClass());
}
}

我打算用这个测试用例来检查nullpointerexception。测试用例通过了,但我在控制台中收到了NULL异常消息如何限制不在控制台中打印我尝试了@Test(预期=NullPointerexception.class(,但它仍在控制台中打印

java.lang.NullPointerException: null

我正在使用sprinrunner

@RunWith(SpringRunner.class)
@SpringBootTest

试试这个:

@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void fooBar() throws FooBarException {
exception.expect(FooBarException.class);
exception.expectMessage(containsString("bla bla bla"));
//your code here
}

最新更新