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

Compare with Current View Page History

« Previous Version 18 Current »

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

1) Add these junitparams imports:

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

2) Add {{@RunWith(JUnitParamsRunner.class)}} to your test class:

@Category(DistributedTest.class)
@RunWith(JUnitParamsRunner.class)
@SuppressWarnings("serial")
public class CreateAndLocalDestroyInTXRegressionTest implements Serializable {

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

@Test
@Parameters({"true", "false"})
@TestCaseName("{method}({params})")
public void userFilesAreBackedUp(boolean useRelativePath) throws Exception {

This results in nicer test names in the results:

FileSystemBackupWriterTest (org.apache.geode.internal.cache.backup)
    userFilesAreBackedUp(true)
    userFilesAreBackedUp(false)

You can also use Strings that represent enums:

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)

Some good examples:

  • ClientDeserializationCopyOnReadRegressionTest
  • CreateAndLocalDestroyInTXRegressionTest
  • FileSystemBackupWriterTest
  • No labels