将模拟对象作为JUnit参数传递给具有参数的测试方法

3

我正在使用Junit参数化方法进行单元测试。

最初,我遵循普通的参数化方法来测试我的工作流程1。 但后来我发现了这篇文档2,它提供了一个更好、更简洁的方法来使用参数进行测试。然而,我无法想出一种方法将模拟对象传递给测试方法。

@TestWith({
        "null, mock(B.class),mock(C.class)",
        "mock(A.class), null, mock(C.class)",
        "mock(A.class), mock(B.class), null"
})
public void test_workflow(final A Aclass,final B Bclass,final C Cclass)
{
    assertThat....
}

我遇到了非法参数异常:
java.lang.IllegalArgumentException: 将字符串“mock(B.class)”解释为类B.class时出错 at com.googlecode.zohhak.internal.coercing.CoercingService.coerceParameter(CoercingService.java:58) at com.googlecode.zohhak.internal.coercing.CoercingService.coerceParameters(CoercingService.java:33) at com.googlecode.zohhak.internal.Executor.calculateParameters(Executor.java:28) at com.googlecode.zohhak.internal.junit.ParametrizedFrameworkMethod.invokeExplosively(ParametrizedFrameworkMethod.java:22)
请问有人知道如何将模拟对象作为参数传递给测试方法吗?这将是很大的帮助,我会继续尝试在我的端上找到答案。

也许可以添加静态 mock() 方法所在的类,例如 "Mockito.mock(B.class)" - Carsten
我已经添加了静态导入,但即使添加了Mockito.mock,我仍然遇到相同的错误。问题在于,它将其解释为字符串,可能应该有一些内部转换器可以覆盖以适当地转换字符串(不确定,只是猜测)。 - LifeStartsAtHelloWorld
我猜我找到了答案,不过不确定,它使用默认的强制转换器将字符串转换。我想我需要进行一些更改或构建自定义强制转换器来转换模拟对象。https://github.com/piotrturski/zohhak/blob/master/src/main/java/com/googlecode/zohhak/api/DefaultCoercer.java - LifeStartsAtHelloWorld
1个回答

2

https://github.com/piotrturski/zohhak/blob/master/Full-Guide.md#basic-usage

Zohhak by default supports:

primitives and their wrappers
nulls
enums
String
BigInteger, BigDecimal (since 1.1.0)

and types assignable from them (eg. Number, CharSequence, Object). Parameters are separated with comma, edge white characters are trimmed (unless apostrophes are used).

因此,您需要编写一种强制方法。
该方法将读取类名并创建模拟对象。
https://github.com/piotrturski/zohhak/blob/master/Full-Guide.md#registering-coercions


是的,那个有效。我添加了自定义强制转换。谢谢。 - LifeStartsAtHelloWorld

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接