0

How would I test this public static method using Junit?

I want to test the ResetFactory static method using Junit 4 but I am not sure how to. Any tips? Any help would be appreciated. I have tested the Ticketing constructor and it works perfectly. I had to write the question in a little strange way since it wanted more words for the question.

/**
     * Resets the factory.
     */
    public static void resetFactory() {
        time = 0;
        randomNumber = new Random(10);
    }



    public class Ticketing {

    /** Absolute time for passengers created for the simulation.  The time starts at zero
     * and increases by up to MAX_PASSENGER_GENERATION_DELAY for each passenger created. */
    private static int time = 0; 
    /** Random object with seed that allows for testing of simulation */
    private static Random randomNumber = new Random(10); 
    /** Maximum delay between creation of passengers */
    private static final double MAX_GENERATION_DELAY = 15; 
    /** Percentage of time a  Fast Track passenger should be created */
    private static double pctExpedite = .50;  // initialize with default value
    /** Percentage of time a trusted traveler/TSA PreCheck passenger should be created */
    private static double pctTrust = .05;  // initialize with default value

    /**
     * Set the proportions of fast track, trusted traveler, and (by inference) ordinary passengers
     * that should be generated. Proportion of ordinary passengers is 1 - (pctFast + pctTrusted).
     * @param pctTrusted - proportion of passengers that are TrustedTravelers
     * @param pctFast - proportion of passengers that are FastTrackPassengers
     */
    public static void setDistribution(int pctTrusted, int pctFast) {
        pctExpedite = pctFast * .01;
        pctTrust = pctTrusted * .01;
    }

    /**
     * Generate a new passenger as described in the class comments. 
     * @param log - where the passenger will log his/her data 
     * @return the passenger created
     */
    public static Passenger generatePassenger(Reporter log) {
        // Update the overall time with up to the floor of MAX_PASSENGER_GENERATION_DELAY seconds.
        // The value is cast to an int, which is the floor of the original double.
        time += (int)(1 + randomNumber.nextDouble() * MAX_GENERATION_DELAY);

        // Random number x determines which type of passenger will be created. The generated number
        // is between 0 and 1.0.  By splitting across the range of numbers generated, you can simulate
        // creation of different passengers of appropriate types.
        double x = randomNumber.nextDouble();
        if (x < pctExpedite) {  // Create a Fast Track passenger 
            int ftMin = FastTrackPassenger.MIN_PROCESS_TIME;
            int ftMax = FastTrackPassenger.MAX_EXPECTED_PROCESS_TIME;
            // If the generated number is less than pctExpedite, create a passenger with expedited security
            // with a process time between FastTrack.MIN_PROCESS_TIME and FastTrack.MAX_PROCESS_TIME.
            return new FastTrackPassenger(time, // FastTrackPassenger.MIN_PROCESS_TIME + 
                    (int) (randomNumber.nextDouble() * (ftMax - ftMin)) + ftMin,
                    log); 
        }
        else if (x < pctExpedite + pctTrust) {  // Create a Trusted Traveler 
            int tsaMax = TrustedTraveler.MAX_EXPECTED_PROCESS_TIME;
            int tsaMin = TrustedTraveler.MIN_PROCESS_TIME;
            // Else if the generated number is less than pctExpedite + pcTrust, create a trusted
            // traveler with a process time between TrustedTraveler.MIN_PROCESS TIME and TrustedTraveler.MAX_PROCESS_TIME.
            return new TrustedTraveler(time, // TrustedTraveler.MIN_PROCESS_TIME + 
                    (int) (randomNumber.nextDouble() * (tsaMax - tsaMin)) + tsaMin,
                    log);
        }
        else {   // Create an ordinary passenger
            int ordMax = OrdinaryPassenger.MAX_EXPECTED_PROCESS_TIME;
            int ordMin = OrdinaryPassenger.MIN_PROCESS_TIME;
            // Otherwise, create an ordinary passenger with a process time between OrdinaryPassenger.MIN_PROCESS TIME 
            //  and OrdinaryPassenger.MAX_PROCESS_TIME
            return new OrdinaryPassenger(time, // OrdinaryPassenger.MIN_PROCESS_TIME + 
                    (int) (randomNumber.nextDouble() * (ordMax - ordMin)) + ordMin,
                    log);
        }
    }
    /**
         * Resets the factory.
         */
        public static void resetFactory() {
            time = 0;
            randomNumber = new Random(10);
    }


}
AHunt
  • 67
  • 8
  • [It is impossible to answer your question because you do not provide a specification of what your code ought to do](https://stackoverflow.com/a/53757321/545127). – Raedwald Dec 13 '18 at 08:51

1 Answers1

0

In order to test a static method, you need a static context on Junit.

JUnit includes these useful methods.

@BeforeClass
public static void beforeClass() {}
@AfterClass
public static void afterClass() {}  

Trying something like this,

import your.own.project.package;
import static org.junit.Assert.assertEquals;

@BeforeClass
public static void beforeClass() {
    Ticketing.resetFactory()
}

Your static method would be executed before or after the whole test. Keep in mind that likely this isn't the desired behavior. You can also try coding a wrapper class as this answer shows. Are static methods offered by wrapper classes often used?

bests.

Rene Mr
  • 1
  • 1