LifeStartsAtHelloWorld
LifeStartsAtHelloWorld

Reputation: 979

Pass mock objects as JUnit parameter to test methods with parameters

I am using junit parameterized approach to do unit testing.

Initially I used to follow the normal parameterized approach to test my workflow1. But later I found this document 2 which provides a better and concise approach to test using parameters. However, I cannot figure out a way to pass mock objects to the test method.

@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....
}

I am getting illegal argument exception:

java.lang.IllegalArgumentException: cannot interpret string "mock(B.class)" as a 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)

Does anyone know how can I pass mock objects as parameters to the test method? It will be a great help, I will keep trying to figure it out on my end.

Upvotes: 3

Views: 2396

Answers (1)

user5221125
user5221125

Reputation:

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).

So you need to write a coersion method.
Which will read class name and create the mock object.
https://github.com/piotrturski/zohhak/blob/master/Full-Guide.md#registering-coercions

Upvotes: 2

Related Questions