【问题标题】:JUnit AssertionError: Expecting code to raise a throwableJUnit AssertionError:期望代码引发一个 throwable
【发布时间】:2020-03-16 03:20:24
【问题描述】:

我正在尝试为引发自定义异常的方法编写测试。它因断言错误而失败。如何正确捕获异常并通过测试?

服务方式:

@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");
        }
    }
}

测试方法:

@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");
}

此测试失败并出现以下错误:

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)


--------------------------- 编辑 ------------------- ----- 更改了代码 Test 以包含以下内容

@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");
}

似乎工作正常。

感谢cmets中的建议。

【问题讨论】:

  • 您的方法调用 customServiceImpl.getResultDto() 不会引发异常。这就是 AssertionError 告诉你的。
  • 您正在测试一个模拟 (@MockBean CustomServiceImpl)。那没有意义。测试服务 A 时应该模拟的是 A 的依赖项,即在 A 中使用和注入的服务。您不需要 Spring 集成测试来测试它。只有一个普通的旧单元测试。您首先使用构造函数注入而不是字段注入。您创建 UserUtil 的模拟(依赖项)。您使用new CustomServiceImpl(mockUserUtil) 创建一个真实的CustomServiceImpl 实例。然后你调用你的方法。顺便说一句,这个方法不应该接受任何参数,因为它不使用它们..
  • @JBNizet 是对的。您需要创建一个新的 CustomServiceImpl 实例并将其依赖项 (UserUtil) 作为模拟注入。此外,您不要使用@MockBean 进行单元测试,而是使用@Mock。 MockBean 用于集成测试。
  • @JBNizet 我应该在 CustomServiceImpl 中编写构造函数来接受 UserUtil 争论吗?我试图将模拟放在测试方法中,所以:UserUtil userUtil = Mockito.mock(UserUtil.class); KpiServiceImpl kpiService = new KpiServiceImpl(userUtil); 但是构造函数部分显示为错误。
  • 是的,你应该这样做。它使依赖关系显式,并允许轻松注入模拟,而不必依赖反射和模拟注释。

标签: java spring-boot junit mockito assertj


【解决方案1】:

我已经对我的原始代码进行了适当的更改,并且以下设置运行良好。

@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");
}

【讨论】:

    【解决方案2】:

    另一种方法是:

    @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);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-05
      • 2010-10-26
      • 2015-02-27
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2017-12-01
      相关资源
      最近更新 更多