Well I fooled with Unit Testing in Sakai for a few hours and kind of came to a dead end. It was pretty fun and the basic stuff worked. My problem is that I just don’t know where to stop when creating mock versions of stuff. I looked at unit tests in the gradebook and common and there is not much interaction witht he rest of Sakai – I want to create unit tests that can interact with the rest of Sakai. But that means firing up components somehow (or mocking that up), firing up component manager (or mocking that up) etc etc. The problem is that with the structure of unit tests it seems that everyone who writes a unit test is responsible for mocking up the entire rest of the system. This is icky when dealing with a system as large as Sakai with a goal of a rich framework that *everything* depends on. So we will need to develop a way to make unit testing easier. Someone other than me will need to figure this out.
But here are my notes. Thanks to Josh and Lance for their examples.
This page is a god-send:
http://bugs.sakaiproject.org/confluence/display/ENC/JUnit+Testing
I followed this cookbook and got to the point that the I had a simple unit test working, making its local beans, finding its local beans, using its local beans and then falling on its face.
I adapted Lance’s code:
common-composite-component/src/test/org/sakaiproject/component/junit/spring/ApplicationContextBaseTest.java
I removed everything Hibernate from it and got a pure spring version. This worked once you set the variable on the maven call:
maven test -Dsakaiproject.basedir=/Users/csev/dev/sakai
Here is how it looks when it runs:
test:compile: [javac] Compiling 1 source file to /Users/csev/dev/sakai/osid/component/target/test-classes ApplicationContext=org.springframework.context.support.ClassPathXmlApplicationContext: display name [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=2790855]; startup date [Wed Oct 05 23:18:11 EDT 2005]; root of context hierarchy org.osid.id.IdManager ApplicationContext=org.springframework.context.support.ClassPathXmlApplicationContext: display name [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=1183826]; startup date [Wed Oct 05 23:18:11 EDT 2005]; root of context hierarchy org.osid.id.IdManager test:test: [junit] Running org.sakaiproject.osid.test.IdManagerTest Hello from my unit test... Here we go.. idm=org.sakaiproject.component.osid.id.IdManager@d4461b Exception... [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.042 sec BUILD SUCCESSFUL Total time: 6 seconds