Note: Please see The Green Report for latest recommendations in writing DistributedTests.

Updating Old Client/Server DUnit Test to Use Rules

These tests typically use a Cache instead of ClientCache. They also typically use NORMAL or DISTRIBUTED_ACK Region. The following example updates the test to use Rules without upgrading the client APIs.

Example: CacheRegionClearStatsDUnitTest

1) change "extends JUnit4DistributedTestCase" to "implements Serializable"

@SuppressWarnings("serial")
public class CacheRegionClearStatsDUnitTest implements Serializable {

2) delete serialVersionUID if it exists

3) (optional) add @SuppressWarnings("serial")

4) (optional) remove any other uses of @SuppressWarnings

5) add DistributedRule (and optionally CacheRule and ClientCacheRule)

@Rule
public DistributedRule distributedRule = new DistributedRule();

@Rule
public CacheRule cacheRule = new CacheRule();

6) replace overridden postSetUp method with new setUp method

@Before
public void setUp() throws Exception {
  server1 = getVM(0);
  client1 = getVM(1);
}

7) delete any lines that instantiate CacheRegionClearStatsDUnitTest

8) delete any variables of type Cache, GemFireCacheImpl, InternalCache

9) change all variables to private

10) change creation of system and cache to use CacheRule

cacheRule.createCache(props);

11) change any references to cache to use CacheRule

Region region = cacheRule.getCache().createRegion(REGION_NAME, attrs);

12) change all static methods and variables to non-static

13) replace any Thread.sleep or pause calls with Awaitility

CacheRegionClearStatsDUnitTest contained unnecessary Thread.sleeps that I simply deleted without needing to use Awaitility.

14) replace any WaitCriterion with Awaitility

15) delete any dead code or commented out code

16) delete any unnecessary try-catch blocks and add "throws Exception" to test methods

JUnit provides much better error messages with detailed call stack than try-catch blocks with use of fail. Less code is also better.

17) (optional) delete any unnecessary boxing or unboxing

18) (optional) change from primitive wrappers to primitives (example: Integer to int)

19) (optional) replace use of deprecated APIs with newer APIs if possible

In CacheRegionClearStatsDUnitTest, I kept the use of Cache for the client. I also left the test using deprecated Cache.createRegion, AttributesFactory and CacheServer.setNotifyBySubscription. Updating it to use ClientCache and non-deprecated APIs is desirable but out of scope for this how-to. 

20) (optional) update from raw types to generics if possible

21) (optional) update all variable names and method names to avoid single-letter, confusing names or abbreviations

22) (optional) replace use of org.junit.Assert with use of AssertJ

AssertJ provides much better failure messages that will make dealing with test failures much easier in the future. It also provides richer handling of expected exceptions than JUnit.

23) consider using CacheServer.setPort(0) instead of getRandomAvailablePort and then use CacheServer.getPort() to discover what port it randomly used

24) reorganize test so that vars and Rules are at the top, Before and After methods come before tests, Test methods come next, then any private methods used by the tests and finally any inner classes at the bottom of the class

25) (optional) rename the test to be more descriptive and prefer *DistributedTest to *DUnitTest

I renamed to CacheRegionClearStatsDUnitTest to RegionClearStatsDistributedTest.

  • No labels