...
- Use svn to checkout the testrunner project from SVN (location at top of this page) and place it in your sakai source directory and then build it using maven 1 or maven 2
- Create a test by extending either JUnit TestCase or TestRunner SpringTestCase
- NOTE: You must use SpringTestCase if you want the ability to access Sakai services
- Here is a simple test that extends the TestRunner SpringTestCase and gets the Sakai UserDirectoryService (using Autoiwiring automatic bean injection)
Code Block java java public class SampleTestSakaiUser extends SpringTestCase { private UserDirectoryService userDirectoryService; @Autowired public void setUserDirectoryService(UserDirectoryService userDirectoryService) { this.userDirectoryService = userDirectoryService; } public void testCanGetSakaiUDSBean() { assertNotNull(userDirectoryService); } public void testCanUseUDS() { assertTrue(userDirectoryService.countUsers() > 1); assertTrue(userDirectoryService.getUsers(1, 1).size() == 1); User user = null; try { user = userDirectoryService.getUser(UserDirectoryService.ADMIN_ID); } catch (UserNotDefinedException e) { fail("Exception: " + e.getMessage()); } assertNotNull(user); assertEquals(UserDirectoryService.ADMIN_ID, user.getId()); } }
- Add the needed maven dependencies to a project which will run the tests
- Maven 2 dependencies for your impl (where you put your tests)
Code Block xml xml <dependency> <groupId>org.sakaiproject</groupId> <artifactId>sakai-testrunner-logic-api</artifactId> <version>${sakai.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>provided</scope> </dependency>
- Maven 2 dependencies for your pack (where you put your spring config)
Code Block xml xml <dependency> <groupId>org.sakaiproject</groupId> <artifactId>sakai-testexecutor</artifactId> <version>1.0</version> <scope>runtime</scope> </dependency>
- Maven 2 dependencies for your impl (where you put your tests)
- Register the test with the TestRunner
- Register via Spring (using a TestExecutor bean definition in your components.xml file)
Code Block xml xml <bean class="org.sakaiproject.testrunner.util.TestExecutor"> <property name="testClassname" value="org.sakaiproject.testrunner.impl.tests.SampleTestSakaiUser" /> <property name="testType" value="testrunner.integration" /> <property name="registerTest" value="true" /> </bean>
- Register programmatically via a service
Code Block java java public class SampleTestRunner { private final static Log log = LogFactory.getLog(SampleTestRunner.class); private TestRunnerService testRunnerService; public void setTestRunnerService(TestRunnerService testRunnerService) { this.testRunnerService = testRunnerService; } private String myTestsId = "aaronz"; public void init() { // register some tests testRunnerService.registerTest(myTestsId, TestRunnerService.TESTING_TESTS_INTEGRATION, SimpleTest.class); testRunnerService.registerTest(myTestsId, TestRunnerService.TESTING_TESTS_INTEGRATION, SampleTestSakaiUser.class); // call a method to run the tests if (runMyTests()) { log.info("My Sample tests running within a service passed!!"); } // unregister my tests since I don't need them anymore testRunnerService.unregisterTests(myTestsId, null); } /** * This runs my tests and returns a boolean which indicates if they passed or failed, * you would probably want to do something useful with the output * @return true if tests passed, false otherwise */ private boolean runMyTests() { Map<Class<? extends TestCase>, TestResult> m = testRunnerService.runTests(myTestsId, null); return TestRunnerUtils.checkTestsSuccess(m); } }
- This would also require a simple spring bean definition to initialize the SampleTestRunner
Code Block xml xml <bean class="org.sakaiproject.testrunner.impl.SampleTestRunner" init-method="init"> <property name="testRunnerService" ref="org.sakaiproject.testrunner.TestRunnerService" /> </bean>
- This would also require a simple spring bean definition to initialize the SampleTestRunner
- Register via Spring (using a TestExecutor bean definition in your components.xml file)
- Startup Sakai and the TestExecutor will run the test automatically (unless the TestRunnerService is configured to disable tests)