The start project automatically adds a JUnit dependency. This how to shows some sample test cases.
Your pom.xml must add a dependency to a start implementation. Within this how to we will assume that you are using the version 0.9.6 of the project darkstar gameserver.
Add the following lines to your pom.xml:
<dependencies> <dependency> <groupId>com.pentagaia.tb.start</groupId> <artifactId>start-0_9_6</artifactId> <version>0.1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.pentagaia.tb.start</groupId> <artifactId>start-api</artifactId> <version>0.1.0-SNAPSHOT</version> <type>test-jar</type> <scope>test</scope> </dependency> </dependencies>
These lines will add a dependency to the correct start implementation. And it will add a dependency to the test-jar containing useful helper classes.
Inherit the class AbstractTestCase . Have a look at the public and protected methods. There is a method to generate a new gameserver instance. And there are various helper methods to assert the gameserver. A simple test method may look like this:
this.generateKernel(this.getSimpleConfig()); this.assertRunning(); // TODO do your testings here this.assertCleanShutdown();
What will happen if you run this test? There will be a new clean gameserver instance and it is stored in field testKernel. The server will startup asynchronous and assertRunning will wait for at least 5 seconds to finish the initialization. That means that all services will be ready. That's fairly enough but if you expect problems because of complicated startup code you should implement your own startup assertion.
this.generateKernel(this.getSimpleConfig()); this.testKernel.joinState(IPdsKernel.KernelStateType.STATE_RUNNING, 25000); // wait for 25 seconds // TODO do your testings here this.testKernel.shutdown(false); this.testKernel.joinState(IPdsKernel.KernelStateType.STATE_TERMINATED, 50000); // wait for 50 seconds this.assertNoLoggingEntries();
The method assertNoLoggingEntries will probe for any severe error message. We do not assume that the test case succeeds if there are any errors.
So what do we see at this point? There is a clean gameserver running on a clean database. You noticed the method getSimpleConfig? This method will return a small configuration that uses a temporary directory as data storage. It will cleanup this database at the end of your test case too.
We assume that you are not planning to use mock services/managers. To test against expected data you will have to populate your gameserver at first. There is another variant of generateKernel that takes an array of kernel listeners. You can implement your own kernel listener to fetch the servers lifecycle. And of course this listener can be used to populate the gameserver before running your tests. Look at the start project documentation to read details.
To install custom services/managers edit the configuration or install a property refactor.
The start project will use class loading to create a small local gameserver instance for every test case it runs. So we do not have any chance to access managers or services. They won't be found. But there is a solution. Create a small runnable that contains your test case.
public class MyRunnable implements Runnable { /** * {@inheritDoc} * * @see java.lang.Runnable#run() */ public void run() { // managers final DataManager dataManager = (DataManager) KernelManager.getKernel().getAppKernelAppContext().getDataManager(); // alternative: AppContext.getDataManager(); Assert.assertNotNull("The data manager must not be null", dataManager); //$NON-NLS-1$ } }
Now invoke this test runnable:
this.generateKernel(this.getSimpleConfig()); this.assertRunning(); this.runTest(MyRunnable.class, 1000); // wait for 1 second this.assertCleanShutdown();
The test class must be static public so that it can be instantiated. To run a test case inside transactions use the method runTestTransactionally.