Code documentation

Development tools

Code Structure

Techniques and Standards

How To

Functional Info

Background Info

JMRI Code: Unit testing with JUnit

JUnit is a system for building "unit tests" of software. Unit tests are small tests that make sure that individual parts of the software do what they're supposed to do. In a distributed project like JMRI, where there are lots of developers in only loose communication with each other, unit tests are a good way to make sure that the code hasn't been broken by a change.

For more information on JUnit, see the JUnit home page. We now use JUnit version 4 (JUnit4), although a lot of JMRI code originally had tests written in the previous version, JUnit3. For instructions on how to convert existing JUnit3 tests to JUnit4, see the "Migrating to JUnit4" section below.

A very interesting example of test-based development is available from Robert Martin's book.

All of the JMRI classes have JUnit tests available; we have decided that our Continuous Integration system will insist on that. It's good to add JUnit tests as you make changes to classes (they test your new functionality to make sure that it is working, and keeps working as other people change it later), when you have to figure out what somebody's code does (the test documents exactly what should happen!), and when you track down a bug (make sure it doesn't come back).

Running the Tests

To run the existing tests, say
   ant alltest
This will compile the test code, which lives in the "test" subdirectory of the "java" directory in our usual code distributions, and then run the tests under a GUI. (To make sure you've recompiled everything, you may want to do ant clean first)
If you know the name of your test class, or the test class for your package, you can run that directly with the "runtest" script:
   ant tests
   ./runtest.csh jmri.jmrit.powerpanel.PowerPanelTest
The first line compiles all the test code, and the second runs a specific test or test suite.
(Hint: How to set this up using IntelliJ)

Optional Checks

There are a number of run-time optional checks that can be turned on by setting environmental variables. We periodically run them to check on how the overall test system is working, but they're too time intensive to leave on all the time.
jmri.skipschematests
If true, JUnit tests will skip checking the schema of all the test XML files.
jmri.skipjythontests
If true, JUnit tests will skip running the jython/tests scripts.
jmri.log4jconfigfilename
Override the default "tests.lcf" logging control file name.
jmri.demo
When set true, leave certain windows open to demonstrate their capability
jmri.migrationtests
When set true, run some extra tests; usually used during code migration, where not everything is right yet but you want to be able to include tests for individual running.
jmri.util.JUnitUtil.printSetUpTearDownNames
If true, JUnit tests will print out each JUnitUtil.setUp() and JUnitUtil.teardown() call. This can be useful if i.e. the CI tests are hanging, and you can't figure out which test class is the problem.
jmri.util.JUnitUtil.checkSetUpTearDownSequence
If true, check for whether JUnitUtil.setUp() and JUnitUtil.teardown() follow each other int the proper sequence. Print a message if not. (This slows execution a bit due to the time needed to keep history for the message)
jmri.util.JUnitUtil.checkSequenceDumpsStack
If true, makes jmri.util.JUnitUtil.checkSetUpTearDownSequence more verbose by also including the current stack trace along with the traces of the most recent setUp and tearDown calls.
jmri.util.JUnitUtil.checkRemnantThreads
If true, checks for any threads that have not yet been terminated during the test tearDown processing. If found, the context is logged as a warning.
jmri.util.JUnitUtil.checkTestDuration
If true, issues a warning if a test takes too long. The default limit is 5000 msec, but you can change it defining the jmri.util.JUnitUtil.checkTestDurationMax environment variable.
For more on setting these, see the start-up scripts page.

A Note on Internationalization (I18N)

Tests check the correctness of text in GUI elements, warning messages, and other places. Many of these are internationalized, varying depending on the computer's Locale.

To avoid false failures, the Ant and Maven build control files set the locale to en_US before running tests. This covers continuous integration running, and running locally using e.g. "ant headlesstest" or "ant alltest".

The ./runtest.csh mechanism does not automatically set the locale. To do that, the easiest approach is to set the JMRI_OPTIONS environment variable via one of:

   setenv JMRI_OPTIONS "-Duser.language=en -Duser.region=US"
   export JMRI_OPTIONS="-Duser.language=en -Duser.region=US"
depending on what kind of OS and shell you're using. For more on how this works, see the page on startup scripts.

Continuous Integration Test Execution

The continuous integration environment senses changes in the code repository, rebuilds the code, performs a variety of checks. If no fatal issues are found, the continuous integration process executes the "alltest" ant target against the build to run the tests against the successful build of the code base.

Error Reporting

If a test fails during the continuous integration execution of "alltest", an e-mail is sent to the jmri-build e-mail list as well as to the developers who have checked in code which was included in the build.

You may visit the web site to subscribe to the jmri-builds e-mail list to get the bad news as quickly as possible, or monitor to view the archives of the e-mail list and see past logs. Or you can monitor the "dashboard" at the continuous integration web site.

(When the build succeeds, nothing is mailed, to cut down on traffic)

Code Coverage Reports

As part of running the tests, Jenkins accumulates information on how much of the code was executed, called the "code coverage". We use the JaCoCo tool to do the accounting. It provides detailed reports at multiple levels:

Writing Tests

By convention, we have a "test" class shadowing (almost) every real class. The "test" directory contains a tree of package directories parallel to the "src" tree. Each test class has the same name as the class to be tested, except with "Test" appended, and will appear in the "test" source tree. For example, the "jmri.Version" class's source code is in "src/jmri/Version.java", and it's test class is "jmri.VersionTest" found in "test/jmri/VersionTest.java".

There are additional classes which are used to group the test classes for a particular package into JUnit test suites.

Writing Additional Tests for an Existing Class

To write additional tests for a class with existing tests, first locate the test class. (If one doesn't exist, see the section below about writing tests for a new class)

If the test suite has not been converted to JUnit4 yet, one or more test methods can be added to the class using the JUnit conventions. Basically, each method needs a name that starts with "test", e.g. "testFirst", and has to have a "public void" signature. JUnit will handle everything after that.

If the test suite has been converted to JUnit4, the JUnit4 conventions require that the test be preceeded by the "@Test" annotation:

    @Test
    public void testSomething() {
        ...
    }

See the section on JUnit4 Migration for more information on JUnit4.

In general, test methods should be small, testing just one piece of the classes operation. That's why they're called "unit" tests.

Writing Tests for a New Class

To write a test for a new class, you need to create a file that shadows your new class. For our example, consider creating a test for a new class that appears in "src/jmri/jmrix/foo/Foo.java". The new test would be created in a file named "test/jmri/jmrix/foo/FooTest.java".

Assuming that the Foo class has a default constructor named foo(), Then the following would be minimal contents for the test/jmri/jmrix/foo/FooTest.java file:

    package jmri.jmrix.foo;

    import org.junit.After;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Ignore;
    import org.junit.Test;

    /*
     * Tests for the Foo Class
     * @author  Your Name   Copyright (C) 2016
     */
    public class FooTest {

       @Test
       public void testCtor() {
           Assert.AssertNotNull("Foo Constructor Return",new foo());
       }

      @Before
      public void setUp() {
         jmri.util.JUnitUtil.setUp();
      }

     @After
     public void tearDown() {
        jmri.util.JUnitUtil.tearDown();
     }
  }

Note that you should be invoking jmri.util.JUnitUtil.setUp() and jmri.util.JUnitUtil.tearDown() as above. Some older tests use calls to apps.tests.Log4JFixture.setUp() and apps.tests.Log4JFixture.tearDown(); these should be replaced by the corresponding calls to the JUnitUtil methods.

In addition, the tearDown() method should set all member variable references to null. This is because JUnit4 keeps the test class objects around until all the tests are complete, so any memory you allocate in one class can't be garbage collected until all the tests are done. Setting the references to null allows the objects to be collected. (If the allocated objects have a dispose() method or similar, you should call that too). You should not reset the InstanceManager or other managers in the tearDown method; any necessary manager resets will be done automatically, and duplicating those wastes test time.

You may also choose to copy an existing test file and make modifications to suite the needs of your new class. Please make sure you're copying a file in the new JUnit4 format, with the @Test statements, to keep us from having to update your new file later.

After the test class is created it needs to be added to the package test for the package. In the case of our example, that should be the file test/jmri/jmrix/foo/PackageTest.java

If the PackageTest has not been converted to JUnit4 format yet, the following line needs to be added to the list of test classes in the "suite" method:

suite.addTest(new junit.framework.JUnit4TestAdapter(FooTest.class));

If the PackageTest has been converted to JUnit4 format, then "FooTest.class" needs to be added to the list test classes in the @Suite.SuiteClasses annotation that appears before the beginning of the PackageTest class.

Writing Tests for a New Package

To write a tests for a new package, in addition to writing tests for each class, you need to create a "PackageTest.java file that calls your new tests. For our example, we will create the file "test/jmri/jmrix/foo/PackageTest.java" and call the tests in "test/jmri/jmrix/foo/FooTest.java".

The following would be minimal contents for the test/jmri/jmrix/foo/PackageTest.java file:
    package jmri.jmrix.foo;

    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;

    /**
     * tests for the jmri.jmrix.foo package
     *
     * @author Your Name Copyright (C) 2016
     */
    @RunWith(Suite.class)
    @Suite.SuiteClasses({
       FooTest.class
    })
    public class PackageTest{
    }

You may also choose to copy an existing test file and make modifications to suite the needs of your new class.

After the PackageTest class is created it needs to be added to the PackageTest for the enclosing package. In the case of our example, the enclosing package test would be the file test/jmri/jmrix/PackageTest.java

If the enclosing PackageTest has not been converted to JUnit4 format yet, the following line needs to be added to the list of test classes in the "suite" method:

suite.addTest(new junit.framework.JUnit4TestAdapter(jmri.jmrix.foo.PackageTest.class));

If the enclosing PackageTest has been converted to JUnit4 format, then "jmri.jmrix.foo.PackageTest.class" needs to be added to the list test classes in the @RunWithSuite annotation that appears before the beginning of the enclosing PackageTest class.

Key Test Metaphors

Handling Log4J Output From Tests

JMRI uses Log4j to handle logging of various conditions, including error messages and debugging information. Tests are intended to run without error or warning output, so that it's immediately apparent from an empty standard log that they ran cleanly.

Log4j usage in the test classes themselves has two aspects:

  1. It's perfectly OK to use log.debug(...) statements to make it easy to debug problems in test statements. log.info(...) can be used sparingly to indicate normal progress, because it's normally turned off when running the tests.
  2. In general, log.warn or log.error should only be used when the test then goes on to trigger a JUnit assertion or exception, because the fact that an error is being logged does not show up directly in the JUnit summary of results.

On the other hand, you might want to deliberately provoke errors in the code being tested to make sure that the conditions are being handled properly. This will often produce log.error(...) or log.warn(...) messages, which must be intercepted and checked.

To allow this, JMRI runs it's using tests with a special log4j appender, which stores messages so that the JUnit tests can look at them before they are forwarded to the log. There are two aspects to making this work:

  1. All the test classes should include common code in their setUp() and tearDown() code to ensure that log4j is properly initiated, and that the custom appender is told when a test is beginning and ending.
        // The minimal setup for log4J
        protected void setUp() throws Exception { 
            super.setUp(); // Note: skip this line when using JUnit4
            jmri.util.JUnitUtil.setUp(); 
        }
        protected void tearDown() throws Exception {
            jmri.util.JUnitUtil.tearDown();
            super.tearDown(); // Note: skip this line when using JUnit4
        }
    
  2. When a test is deliberately invoking a message, it should then use JUnitAppender class methods to check that the message was created. For example, if the class under test is expected to do
        log.warn("Provoked message");
    
    the invoking test case should follow the under-test calls that provoke that with the line:
        jmri.util.JUnitAppender.assertWarnMessage("Provoked message");
    

    It will be a JUnit error if a log.warn(...) or log.error(...) message is produced that isn't matched to a JUnitAppender.assertWarnMessage(...) call.

  3. The Log4JUtil.warnOnce(..) requires some special handling in tests. We want each test to be independent, so we reset the "want only once" logic early in the JUnitUtil.setUp() that's routinely invoked @Before the tests. This means that the first invocation, and only the first invocation, for each message will be logged.
  4. We want to make it easy to add a Log4JUtil.deprecationWarning call when a method is deprecated. This will log a message the first time it's invoked. We want to warn that deprecated code is being invoked during normal operation, so this is normally becomes a Log4JUtil.warnOnce(..) call. When you see those warnings messages, you should remove them by completing the migration away from the deprecated method.

    The one exception is during unit and CI testing of the actual deprecated method. We want to keep those tests around until the deprecated method is finally removed. That ensures it keeps working until it's deliberately removed, and not inadvertently broken in the meantime. In this case, you should turn off the Log4JUtil.deprecationWarning in just that test method using Log4JUtil.setDeprecatedLogging(false) before invoking the deprecated method. (You can also do an JUnitAppender.assertWarn for all the messages emitted, but it's easier to just turn them off.)

Note: Our CI test executables are configured to fail if any FATAL or ERROR messages are emitted instead of being handled. This means that although you can run your tests successfully on your own computer if they're emitting ERROR messages, but you won't be able to merge your code into the common repository until those are handled. It's currently OK to emit WARN-level messages during CI testing, but that will also be restricted (cause the test to fail) during tne 4.15.* development series, so please suppress or handle those messages too.

Resetting the InstanceManager

If you are testing code that is going to reference the InstanceManager, you should clear and reset it to ensure you get reproducible results.

Depending on what managers your code needs, your setUp() implementation could start with:

    super.setUp(); // Note: skip this line when using JUnit4
    jmri.util.JUnitUtil.setUp();
    jmri.util.JUnitUtil.resetInstanceManager();
    jmri.util.JUnitUtil.resetProfileManager();
    jmri.util.JUnitUtil.initConfigureManager();
    jmri.util.JUnitUtil.initShutDownManager();

    jmri.util.JUnitUtil.initDebugCommandStation();
    
    jmri.util.JUnitUtil.initInternalTurnoutManager();
    jmri.util.JUnitUtil.initInternalLightManager();
    jmri.util.JUnitUtil.initInternalSensorManager();
    jmri.util.JUnitUtil.initReporterManager();
(You can omit the initialization managers not needed for your tests) See the jmri.util.JUnitUtil class for the full list of available ones, and please add more if you need ones that are not in JUnitUtil yet.

Your tearDown() should end with:

    jmri.util.JUnitUtil.tearDown();
    super.tearDown();  // Note: skip this line when using JUnit4

Working with Listeners

JMRI is a multi-threaded application. Listeners for JMRI objects are notified on various threads. Sometimes you have to wait for that to take place.

If you want to wait for some specific condition to be true, e.g. receiving a reply object, you can use a waitFor method call which looks like:

    JUnitUtil.waitFor(()->{reply!=null}, "reply didn't arrive");
The first argument is a lambda closure, a small piece of code that'll be evaluated repeatedly until true. The String second argument is the text of the assertion (error message) you'll get if the condition doesn't come true in a reasonable length of time.

Waiting for a specific result is fastest and most reliable. If you can't do that for some reason, you can do a short time-based wait:

    JUnitUtil.releaseThread(this);
This uses a nominal delay. But you might want to consider the structure of either your code (that you're testing) or the test itself: If you can't tell whether it succeeded, what's the purpose of the operation?

Note that this should not be used to synchronize with Swing threads. See the Testing Swing Code section for that.

In general, you should not have calls to sleep(), wait() or yield() in your code. Use the JUnitUtil and JFCUtil support for those instead.

Working with Threads

(See a following section for how to work with Swing (GUI) objects and the Swing/AWT thread)

Some tests will need to start threads, for example to test signal controls or aspects of layout I/O.

General principles your tests must obey for reliable operation:

For example, if creating a thread based on AbstractAutomat, you can check the start with:

    AbsractAutomat p = new MyThreadClass();
    p.start();
    JUnitUtil.waitFor(()->{return p.isRunning();}, "logic running");
and ensure termination with
    p.stop();
    JUnitUtil.waitFor(()->{return !p.isRunning();}, "logic stopped");

Please make sure your unit tests clean up after themselves! They should not leave any threads running. Any threads they start should have either terminated normally by the end of the test (don't let them just time out and crash later during some other test!) or you should add code to terminate them.

You can check whether you've left any threads running by setting the jmri.util.JUnitUtil.checkRemnantThreads environment variable to true, with i.e.

setenv JMRI_OPTIONS -Djmri.util.JUnitUtil.checkRemnantThreads=true 
or the equivalent for your computer type. This tells the jmri.util.JUnitUtil.tearDown() method to check for any (new) threads that are still running at the end of each test. This check is a bit time-intensive, so we don't leave it on all the time.

Testing I/O

Some test environments don't automatically flush I/O operations such as streams during testing. If you're testing something that does I/O, for example a TrafficController, you'll need to add "flush()" statements on all your output streams. (Having to wait a long time to make a test reliable is a clue that this is happening somewhere in your code)

Temporary File Creation in Tests

Testcases which create temporary files must be carefully created so that there will not be any problems with file path, filesystem security, pre-existence of the file, etc. These tests must also be written in a way that will operate successfully in the continuous integration build environment. And the temporary files should not become part of the JMRI code repository. This section discusses ways to avoid these types of problems.

If you need a temporary file or directory, and your test uses JUnit4, you can use a Rule to create a file or directory before each test runs.

    import org.junit.Rule;
    import org.junit.rules.TemporaryFolder;
    ...
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

You then reference "folder" in your test code:

        // create a temporary file
        File randomNameFile = folder.newFile();
        // create a temporary directory
        File randomNameDir = folder.newFolder();
JUnit4 will make sure the file or folder is removed afterwards regardless of whether the test succeeds or fails. For more information on this, see the Javadoc for TemporaryFolder.

If you're not using JUnit4, consider converting the test class (see below) and use the technique above. But if you can't do that, and have to stick with JUnit3:

JUnit Rules

JUNit4 added the concept of "Rules" that can modify the execution of tests. They work at the class or test level to modify the context JUnit4 uses for running the classes tests. Generally, you start by creating one:
    @Rule
    public final TemporaryFolder tempFolder = new TemporaryFolder();

Some standard JUnit4 rules you might find useful:

Timeout - limit duration

The Timeout rule imposes a timeout on all test methods in a class.
    @Rule
    public org.junit.rules.Timeout globalTimeout = org.junit.rules.Timeout.seconds(10);

Note that you can also add a timeout option to an individual test via an argument to the @Test annotation. For example,

    @Test(timeout=2000)
will put a 2 second (2,000 milliseconds) timeout on that test. If you use both the rule and the option, the option will control the behavior. For a bit more info, see the JUnit4 Timeouts for Tests page.

RetryRule - run test several times

JMRI has jmri.util.junit.rules.RetryRule which can rerun a test multiple times until it reaches a limit or finally passes. Although it's better to write reliable tests, this can be a way to make the CI system more reliable while you try to find out why a test isn't reliable.

For a working example, see java/test/jmri/jmrit/logix/LearnWarrantTest.java

Briefly, you just add the lines

    import jmri.util.junit.rules.RetryRule;
    
    @Rule
    public RetryRule retryRule = new RetryRule(3);  // allow 3 retries
to your test class. This will modify how JUnit4 handles errors during all of the tests in that class.

Tools for Controlling JUnit4 tests

Testing Swing Code

AWT and Swing code runs on a separate thread from JUnit tests. Once a Swing or AWT object has been displayed (via show() or setVisible(true)), it cannot be reliably accessed from the JUnit thread. Even using the listener delay technique described above isn't reliable.

For the simplest possible test, displaying a window for manual interaction, it's OK to create and invoke a Swing object from a JUnit test. Just don't try to interact with it once it's been displayed!

Because we run tests in "headless" mode during the continuous integration builds, it's important that tests needing access to the screen start with:

Assume.assumeFalse(GraphicsEnvironment.isHeadless());

This will run the test only when a display is available.

GUI tests should close windows when they're done, and in general clean up after themselves. If you want to keep windows around so you can manipulate them, e.g. for manual testing or debugging, you can use the jmri.demo system parameter to control that:

        if (!System.getProperty("jmri.demo", "false").equals("false")) {
            myFrame.setVisible(false);
            myFrame.dispose();
        }

For many tests, you'll both make testing reliable and improve the structure of your code by separating the GUI (Swing) code from the JMRI logic and communications. This lets you check the logic code separately, but invoking those methods and checking the state them update.

For more complicated GUI testing, two tools are generally used. The older one, JFCUnit is no longer maintained, so we recommend that new tests be written with Jemmy. Note: Only use one of these in your test class! Don't try to mix them.

Using Jemmy

Recently, e.g. since 2016, some tests have been developed using the jemmy tool. See e.g. the samples on the NetBeans pages.

For more information on Jemmy, please see the tutorial Wiki page and the Jemmy Javadoc.

Locating GUI Items using Jemmy

Jemmy must be able to find the objects on the screen. Jemmy Operators are generally used to both locate and manipulate items on the screen.

Here are a few tips for locating items with Jemmy:

Example of Closing a Dialog Box

If you want to test a method that pops a have a JDialog box with a title (in top bar of the dialog window) of "Foo Warning" and an "OK" button to close it, put this in your JUnit test:
        new Thread(() -> {
            // constructor for d will wait until the dialog is visible
            JDialogOperator d = new JDialogOperator("Foo Warning");
            JButtonOperator bo = new JButtonOperator(d,"OK");
            jmri.util.ThreadingUtil.runOnGUI(() -> {bo.push();});
        }).start();
        showTheDialog();
The thread is started before your code runs and starts Jemmy looking for the Dialog box. Once it appears, Jemmy will push the "OK" button. You can't put the Jemmy calls in advance: They'll wait forever for the dialog to appear, and never proceed to your code to show the dialog. And you can't put them after the call, because your call won't exist until somebody presses "OK".

Jemmy timeouts

Jemmy has an extensive system of built-in timeouts. It'll wait to perform an operation until e.g. the requested button or menu is on the screen, but will eventually timeout if it can't find it. In that case it throws a org.netbeans.jemmy.TimeoutExpiredException with some included diagnostic test. Please don't catch this exception: the problem here is not the exception, it's that Jemmy wasn't able to do what you asked. That's the thing that needs to be debugged.

If you want to change one of Jemmy's timeouts, do

        myMenuOperator.getTimeouts().setTimeout("JMenuOperator.WaitBeforePopupTimeout", 30L);
where "myMenuOperator" is a reference to a Jemmy operator object, 30L is the new value (a long) in milliseconds, and the particular timeout name comes from the Javadoc. Sometimes, setting the "WaitBeforePopupTimeout" from it's default of zero to a few milliseconds can improve the reliability of tests. Also, setting "JMenuOperator.WaitPopupTimeout" and "ComponentOperator.WaitComponentTimeout" to a lower value from their defaults of 60000L (a minute) can speed work when you're trying to debug the cause of a timeout.

Jemmy hints and issues

Actions like operator.click() work like clicking on a real screen: if the click target isn't the foremost component when the click happens, the click will go to some other component. This is particularly annoying when you're running a long series of tests on your own computer will doing other work, as pushing test windows to the background will likely cause (false-negative) failures.

If your test thread invokes a method that causes a lot of Swing/AWT activity, that might not all be complete when the method returns. For example, if you create a JFrame and either explicitly or implicitly call pack(), that starts the Swing thread working on that frame; that can proceed in parallel to the return to the test thread. If the test thread will continue to do more Swing operations, like create and pack another frame, you'll have problems unless you either:

Using JFCUnit

You should not write new test classes using JFCUnit. Use Jemmy instead. This section is to document test classes we already have.

The JFCUnit library can control interactions with Swing objects. For a very simple example of the use of JFCUnit, see the test/jmri/util/SwingTestCaseTest.java file.

To use JFCUnit, you first inherit your class From SwingTestCase instead of TestCase. This is enough to get basic operation of Swing tests; the base class pauses the test thread until Swing (actually, the AWT event mechanism) has completed all processing after every Swing call in the test. (For this reason, the tests will run much slower if you're e.g. moving the mouse cursor around while they're running)

For more complex GUI testing, you can invoke various aspects of the interface and check internal state using test code.

Testing Script Code

JMRI ships with sample scripts. This section discussions how you can write simple tests for those to ensure they keep working.

Testing Jython sample scripts

Test scripts can be placed in jython/test are automatically invoked by java/test/jmri/jmrit/jython/SampleScriptTest.java.

See the jmri_bindings_test.py sample for syntax, including examples of how to signal test failures.

In the future, this could be extended to pick up files automatically, to support xUnit testing, etc.

Issues

JUnit uses a custom classloader, which can cause problems finding singletons and starting Swing. If you get the error about not being able to find or load a class, suspect that adding the missing class to the test/junit/runner/excluded.properties file would fix it.

As a test only, you can try setting the "-noloading" option in the main of whichever test class you're having trouble with:

    static public void main(String[] args) {
            String[] testCaseName = {"-noloading", LogixTableActionTest.class.getName()};
            junit.swingui.TestRunner.main(testCaseName);
    }

Please don't leave "-noloading" in place, as it prevents people from rerunning the test dynamically. Instead, the right long-term fix is to have all classes with JUnit loader issues included in the test/junit/runner/excluded.properties file. JUnit uses those properties to decide how to handle loading and reloading of classes.

Migrating to JUnit4

JUnit4 is a significant upgrade of the JUnit tool from our previous JUnit3 standard. It brings new capabilities, but also changes how tests are structured. As of this writing (Fall 2018), we're committed to migrating all our tests to JUnit4, though the work is being done on a time-available basis (i.e. slowly).

The rest of this section discusses some aspects of moving to and using JUnit4.

Example of JUnit4 test and corresponding PackageTest.java file; note lack of main() procedure and other previously-present boilerplate code. These files are the result of this commit that migrated a test package to JUnit4

Example of JUnit4 tests with a main() procedure

A possible set of steps for conversion:

  1. Change the imports. Typically, these can be removed:
    import junit.framework.Assert;
    import junit.framework.Test;
    import junit.framework.TestCase;
    import junit.framework.TestSuite;
    

    and you'll typically need
    import org.junit.After;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Ignore;
    import org.junit.Test;
    

  2. The test class no longer inherits from TestCase, so replace public class MyClassTest extends TestCase with public class MyClassTest.
  3. Mark test methods with the "@Test" annotation:
        @Test
        public void testSomething() {
            ...
        }
    
    
  4. If you have bare assert calls, like
        assertEquals(k, ind);
    
    change them to the JUnit4 form:
        Assert.assertEquals(k, ind);
    
  5. More of our Test classes have a "from here down is testing infrastructure" comment, followed by setup code. Some of that can be removed. First, remove the class constructor, e.g.
        public MyClassTest(String s) {
               super(s);
        }
    
  6. Next, remove the main method if there is one, e.g.:
        // Main entry point
        static public void main(String[] args) {
            apps.tests.Log4JFixture.initLogging();
            String[] testCaseName = {"-noloading", MyClassTest.class.getName()};
            junit.textui.TestRunner.main(testCaseName);
        }
    
  7. Annotate the setUp() and tearDown() methods. Note: They have to be "public" methods, not "protected" or "private". They should end up looking like (plus your own content, of course):
        @Before
        public void setUp() {
              jmri.util.JUnitUtil.setUp();
        }
    
        @After
        public void tearDown() {
              jmri.util.JUnitUtil.tearDown();
        }
    

    Make sure no more references to the super. method remain in either setUp() or tearDown(). Just remove those lines.

    A note on nesting: If you have @Before and/or @After in both a class and one its superclasses, they will be reliably invoked in the useful way: First the @Before of the super class, then the @Before of your class, then the test, then the @After of your class, and finally the @After of the superclass.

    In rare cases, you might want to use @BeforeClass and @AfterClass methods to wrap the entire test class. These have to be static and are invoked around (i.e. before and after) anything else in the test class. This can be used to e.g. wrap static initializers in the class under test.

  8. Finally, replace the test suite definition. JUnit3 was normally used with "run everything in this class automatically" by having a method that looked like this:
        // test suite from all defined tests
        public static Test suite() {
            TestSuite suite = new TestSuite(Z21MessageTest.class);
            return suite;
        }
    

    If that's what you've got, just remove the whole block.
    If you've got more logic in the suite() routine, ask for help on the jmri-developers list. An example of the result of migrating a more complex case:
    @RunWith(Suite.class)
    @Suite.SuiteClasses({
       CMRISystemConnectionMemoTest.class,
       jmri.jmrix.cmri.serial.PackageTest.class})
    
  9. In the PackageTest.java for this test package, under Test suite(), replace the line
    suite.addTest(MyClassTest.suite()); by
    suite.addTest(new junit.framework.JUnit4TestAdapter(MyClassTest.class));
    leaving the order of tests unchanged to prevent possible side effects.
  10. Run the tests and check that all your tests were successfully included and run.

That's it! You've successfully migrated to native JUnit4.