简体   繁体   中英

JUnit AssertionError: Expecting code to raise a throwable

I am trying to write a test for a method that throws custom exception. It fails with Assertion error. What could be done to properly catch the exception and pass the test?

Service method:

@Service
public class CustomServiceImpl implements CustomService {
    @Autowired
    UserUtil userUtil;
    public ResultDTO getResultDto (String type, Long id) throws CustomException {
        User user = userUtil.getCurrentUser();
        if (user == null) {
            throw new CustomException("User does not exist");
        }
    }
}

Test method:

@MockBean
CustomServiceImpl  customServiceImpl ;

@Test
public void test01_getResultDto() {
    UserUtil userUtil = Mockito.mock(UserUtil.class);
    Mockito.when(userUtil.getCurrentUser()).thenReturn(null);
    Assertions.assertThatThrownBy(() -> customServiceImpl.getResultDto (Mockito.anyString(), Mockito.anyLong())) 
        .isInstanceOf(CustomException .class)
        .hasMessage("User does not exist");
}

This test fails with the following error:

java.lang.AssertionError: Expecting code to raise a throwable.
    at com.ps.service.CustomServiceImplTest.test01_getResultDto(CustomServiceImplTest.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)


--------------------------- EDIT ------------------------ Changed the code Test to include the following

@Mock
UserUtil userUtil;
@InjectMocks
CustomServiceImpl cutomServiceImpl;

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
}

@Test
public void test01_getResultDto() {
    when(userUtil.getCurrentUser()).thenReturn(null);
    assertThatThrownBy(() -> customServiceImpl.getResultDto ("type", 1L)) 
        .isInstanceOf(CustomException .class)
        .hasMessage("User does not exist");
}

Seems to be working.

Thanks to the advice in comments.

I have made suitable changes to my original code and the following set up works well.

@Mock
UserUtil userUtil;
@InjectMocks
CustomServiceImpl cutomServiceImpl;

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
}

@Test
public void test01_getResultDto() {
    when(userUtil.getCurrentUser()).thenReturn(null);
    assertThatThrownBy(() -> customServiceImpl.getResultDto ("type", 1L)) 
        .isInstanceOf(CustomException .class)
        .hasMessage("User does not exist");
}

Another way you can do it is:

@MockBean
CustomServiceImpl  customServiceImpl ;
@Rule
public ExpectedException exceptionRule = ExpectedException.none();

@Test
public void test01_getResultDto() {
    exceptionRule.expect(CustomException.class);
    UserUtil userUtil = Mockito.mock(UserUtil.class);
    Mockito.when(userUtil.getCurrentUser()).thenReturn(null);
    customServiceImpl.getResultDto ("type", 1L);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM