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.
CacheRegionClearStatsDUnitTest
1) change "extends JUnit4DistributedTestCase" to "implements Serializable"
Code Block |
---|
@Category(DistributedTest.class)
@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 DistributedTestRule and CacheRule
Code Block |
---|
@ClassRule
public static DistributedTestRule distributedTestRule = new DistributedTestRule();
@Rule
public CacheRule cacheRule = new CacheRule();
|
6) replace overridden postSetUp method with new setUp method
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
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
Code Block |
---|
cacheRule.createCache(props); |
11) change any references to cache to use CacheRule
Code Block |
---|
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 Awaility
14) delete any dead code or commented out code
15) (optional) delete any unnecessary boxing or unboxing
16) (optional) change from primitive wrappers to primitives (example: Integer to int)
17) (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.
18) (optional) replace use of org.junit.Assert with use of AssertJ
19) 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