You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 13 Next »

JUnitParams can be very useful in any test type (UnitTest, IntegrationTest, DistributedTest).

1) Add these imports:

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import junitparams.naming.TestCaseName;

2) Add this to your test class:

@RunWith(JUnitParamsRunner.class)

3) Use both @Parameters and `@TestCaseName` on test method(s) using parameters:

@Test
@Parameters({"true", "false"})
@TestCaseName("{method}({params})")
public void testSomeBehavior(final boolean value) throws Exception {

This results in nicer test names in the results (ex: ClientDeserializationCopyOnReadRegressionTest):

ClientDeserializationCopyOnReadRegressionTest (org.apache.geode.cache)
    testCopyOnReadWithBridgeServer(true)
    testCopyOnReadWithBridgeServer(false)

You can also use Strings that represent enums (ex: CreateAndLocalDestroyInTXRegressionTest):

private enum LocalOperation {
  LOCAL_DESTROY((region) -> region.localDestroy(KEY),
      TXStateStub_LOCAL_DESTROY_NOT_ALLOWED_IN_TRANSACTION),
  LOCAL_INVALIDATE((region) -> region.localInvalidate(KEY),
      TXStateStub_LOCAL_INVALIDATE_NOT_ALLOWED_IN_TRANSACTION);
  ...
}
 
@Test
@Parameters({"LOCAL_DESTROY", "LOCAL_INVALIDATE"})
@TestCaseName("{method}({params})")
public void createAndLocalOpShouldCreateEventWithNewValue(final LocalOperation operation) {

And the results of running with enums look like this:

CreateAndLocalDestroyInTXRegressionTest
    createAndLocalOpShouldCreateEventWithNewValue
        CreateAndLocalDestroyInTXRegressionTest.createAndLocalOpShouldCreateEventWithNewValue(LOCAL_DESTROY)
        CreateAndLocalDestroyInTXRegressionTest.createAndLocalOpShouldCreateEventWithNewValue(LOCAL_INVALIDATE)
  • No labels