Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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"

Code Block
@Category(DistributedTest.class)
@SuppressWarnings("serial")
public class CacheRegionClearStatsDUnitTest implements Serializable {

...

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

5) add DistributedTestRule add DistributedRule (and optionally CacheRule and ClientCacheRule)

Code Block
@ClassRule@Rule
public staticDistributedRule DistributedTestRule distributedTestRuledistributedRule = new DistributedTestRuleDistributedRule();

@Rule
public CacheRule cacheRule = new CacheRule();

...

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

7) delete any lines that instantiate CacheRegionClearStatsDUnitTest

...

14) replace any WaitCriterion with AwailityAwaitility

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

1517) (optional) delete any unnecessary boxing or unboxing

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

1719) (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

2218) (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

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