Server Tests

Server Tests are similar to unit tests. Both are based on the concepts of unit testing and both use JUnit as the main testing engine. Server tests however utilize Apache's Cactus framework to allow for testing within an EJB container. This removes a number of the limitations found on plain unit tests. They are designed to test code running within a running server. The downside is the time needed to start a server. It is best to test all you can with plain unit tests and only use server unit tests when a full server is needed.

Server tests work similarly to unit tests insofar as much of the normal testing boilerplate code is generated for you via Ant tasks. In the case of server tests, you'd normally have additional steps in configuring Cactus. Studio takes care of all this for you as long as you follow a set of conventions. The following guidelines should be observed when creating server tests.

  • Server tests require a server to be running. As such you can write server tests that access the database, use EJBs, etc.

  • Server tests are created in the same location as unit tests (see the above section on unit tests for details)

  • Server test class names must end with "ServerTest". For example myFirstServerTest.java would be a valid class file name.

  • Server test classes must extend com.onenetwork.platform.tools.test.AbstractServerTest.

  • Test method names must begin with the word "test", just as they did in plain unit tests.

The process for creating server tests is almost identical to creating simple JUnit tests. Note the differences in how the classes are named, and the different base class. Aside from that, the biggest functional difference is the Ant task used to run the server tests.

To run the server tests, locate the Ant task titled server-test as seen in the figure below.

images/download/attachments/144836443/montest_16-version-1-modificationdate-1645139623000-api-v2.png

Here is a sample server test.

import com.onenetwork.platform.tools.test.AbstractServerTest;
 
public class MyServerTest extends AbstractServerTest {
public void testUsecase1(){
fail("write your test code here. You can access models, the database, etc. from server tests.");
}
}

As you can see, it is very similar to our earlier discussion of simple JUnit tests. Remember, these tests require the server to be running. If you execute the server-test Ant target and no server is running, the test suite will fail and you'll receive an error stating no connection to the server could be made.