Hi Matt- The easiest thing to do is start a peer cache (loner, i.e. mcast-port = 0 and locators = "") in the test itself. In other words, the test VM is the peer cache. Unless you absolutely need to test a client/server Function invocation, there is no need to spawn/fork a server. In all cases, we are talking about "integration" tests here. However, it is also a simple matter to "mock" (without PowerMock (yuck), I might add) GemFire components (e.g. Regions, etc).
The *Spring Data GemFire* test suite <https://github.com/spring-projects/spring-data-gemfire/tree/master/src/test> [1] contains many test examples of both integration and unit tests with GemFire. In addition, I have a separate Spring GemFire Tests project on GitHub <https://github.com/jxblum/spring-gemfire-tests> [2] I use to test customer issues, build prototypes and work on customer POCs. This project mainly contains tests (Spring-based <https://github.com/jxblum/spring-gemfire-tests/tree/master/src/test/java/org/spring/data/gemfire> [3], pure GemFire <https://github.com/jxblum/spring-gemfire-tests/tree/master/src/test/java/org/pivotal/gemfire> [4], etc) I have written to prove my knowledge about a particular function/behavior or feature of GemFire or Geode. This project has been invaluable to me. So, by example, see ExceptionThrowingFunctionExecutionTest <https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/java/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest.java> class in the SDG test suite. This is an integration test and it uses a pretty elaborate process framework <https://github.com/spring-projects/spring-data-gemfire/tree/master/src/test/java/org/springframework/data/gemfire/process> [5] I created to fork GemFire Server processes <https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/java/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest.java#L81-L99> [6]. Note, this test class and the test VM is the client cache <https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/resources/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest-context.xml#L18-L22> [7]. Note, the server configuration <https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/resources/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest-server-context.xml> [8], which is used here <https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/java/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest.java#L91-L92> [9]. The SDG CacheFactoryBeanTest <https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/java/org/springframework/data/gemfire/CacheFactoryBeanTest.java> [10] is another good example of a mock, pure unit test. The PeerCacheFunctionCreationExecutionTest <https://github.com/jxblum/spring-gemfire-tests/blob/master/src/test/java/org/spring/data/gemfire/cache/PeerCacheFunctionCreationExecutionTest.java> [11] is an integration test using a peer cache <https://github.com/jxblum/spring-gemfire-tests/blob/master/src/test/resources/org/spring/data/gemfire/cache/PeerCacheFunctionCreationExecutionTest-context.xml#L27> [12]. More recently, I have resorted to other (similar) means to fork GemFire Servers, assert the Server is ready to receive client connections (the test VM) and used Java-based configuration instead of Spring XML, based on my work in the Spring Session GemFire support. Check out the Spring Session Data GemFire (integration) tests that extend the AbstractGemFireIntegrationTest <https://github.com/spring-projects/spring-session/blob/master/spring-session/src/integration-test/java/org/springframework/session/data/gemfire/AbstractGemFireIntegrationTests.java> [13] class. Nothing beats examples when it comes to testing GemFire/Geode. Hope this helps! Cheers! John [1] https://github.com/spring-projects/spring-data-gemfire/tree/master/src/test [2] https://github.com/jxblum/spring-gemfire-tests [3] https://github.com/jxblum/spring-gemfire-tests/tree/master/src/test/java/org/spring/data/gemfire [4] https://github.com/jxblum/spring-gemfire-tests/tree/master/src/test/java/org/pivotal/gemfire [5] https://github.com/spring-projects/spring-data-gemfire/tree/master/src/test/java/org/springframework/data/gemfire/process [6] https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/java/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest.java#L81-L99 [7] https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/resources/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest-context.xml#L18-L22 [8] https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/resources/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest-server-context.xml [9] https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/java/org/springframework/data/gemfire/function/ExceptionThrowingFunctionExecutionIntegrationTest.java#L91-L92 [10] https://github.com/spring-projects/spring-data-gemfire/blob/master/src/test/java/org/springframework/data/gemfire/CacheFactoryBeanTest.java [11] https://github.com/jxblum/spring-gemfire-tests/blob/master/src/test/java/org/spring/data/gemfire/cache/PeerCacheFunctionCreationExecutionTest.java [12] https://github.com/jxblum/spring-gemfire-tests/blob/master/src/test/resources/org/spring/data/gemfire/cache/PeerCacheFunctionCreationExecutionTest-context.xml#L27 [13] https://github.com/spring-projects/spring-session/blob/master/spring-session/src/integration-test/java/org/springframework/session/data/gemfire/AbstractGemFireIntegrationTests.java On Thu, Mar 3, 2016 at 8:56 AM, Matt Ross <[email protected]> wrote: > Ethan, > > The concept of what I'm trying to unit test is not what I'm asking about. > I know it's not the developers job to unit test whether a cache listener > properly fires. I'm saying let's say we have a cache listener that takes > events and does a transformation on them. Or I have a function that > returns to me the results of a specific Oql query. I'm asking how from a > technical perspective am I able to write unit tests so that I could run > them without bringing up a cluster. > > My ideal workflow involves being able to run these unit tests as part of > my Jenkins CI pipeline. > > > On Thursday, March 3, 2016, Eitan Suez <[email protected]> wrote: > >> hi matt, >> >> what helps me is to ask myself "what is it i want to test?" >> >> i should be confident, for example, that gemfire will call my >> cachelistener on a cacheevent. so i shouldn't need to test that (at least >> not at the unit level). but whether the code in the body of my >> afterCreate() or afterUpdate() is doing what i expect it to is my >> responsibility: that's what i need to unit test. so technically you >> shouldn't have to bring in the gemfire machinery to unit-test a >> cachelistener. >> >> now if the body of that method interacts with regions (for example), >> you'll want to mock that interaction. >> >> integration or functional testing is more involved: you have to stand >> up an instance of gemfire with the configuration that will install your >> cache listeners or functions, and have to setup some trigger that will >> exercise the function you're testing, and then be able to verify the >> outcome. >> >> / eitan >> >> >> On Thu, Mar 3, 2016 at 9:06 AM, Matt Ross <[email protected]> wrote: >> >>> Hi all >>> >>> A colleague and I have been talking about the best strategies to do >>> unit testing and TDD in Gemfire/Geode. Obviously writing unit tests for a >>> Gemfire/Geode Client is fairly straightforward, but how would I unit test a >>> function that I'm running on the cluster. Or how would I write tests for a >>> cache listener? I've been pointed in the direction of power mock but could >>> still use some more input from the community on what some of the better >>> strategies are. >>> >>> Are there any examples on Github of how to write these serverside unit >>> tests? Like I said at this point we're only able to do integration testing >>> and unit testing of the client so I've been fairly limited in being able to >>> do TDD. Thanks! >>> >>> -- >>> *Matthew Ross | Data Engineer | Pivotal* >>> *625 Avenue of the Americas NY, NY 10011* >>> *516-941-7535 <516-941-7535> | [email protected] * >>> >>> >> > > -- > *Matthew Ross | Data Engineer | Pivotal* > *625 Avenue of the Americas NY, NY 10011* > *516-941-7535 <516-941-7535> | [email protected] <[email protected]> * > > > -- -John 503-504-8657 john.blum10101 (skype)
