Class Random


  • public class Random
    extends java.lang.Object
    It simulate random numbers generator for Test Driven Development (TDD) with JUnit. If we wants to verify the behaviour of any actor or world when it requests Greenfoot.getRandomNumber(maximum), the test can act as system and generates a possible random number.
    Author:
    Francisco Guerra (francisco.guerra@ulpgc.es)
    • Constructor Summary

      Constructors 
      Constructor Description
      Random()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void set​(int nextNumber)
      It queues a number in FIFO order for any next Greenfoot.getRandomNumber(maximun) operation.
      static void set​(int nextNumber, java.lang.Class<?> callerClass)
      It queues a number in FIFO order for any next Greenfoot.getRandomNumber(maximun) operation from objects of specific class.

      For the next actor behaviour:
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Random

        public Random()
    • Method Detail

      • set

        public static void set​(int nextNumber)
                        throws GreenfootRunnerExpected
        It queues a number in FIFO order for any next Greenfoot.getRandomNumber(maximun) operation.

        For the next actor behaviour:
          
             public class ExampleActorRandom extends Actor {
                
                 @Override
                 public void act(){
                     if (Greenfoot.getRandomNumber(10) == 7) {
                         move(5);
                     }
                 }
             } 
         
         
        The next test verifies that the random number that has been generated produces the wanted behaviour:
          
             import ...
             import greenfoot.TestRunnerState.Random;
             import greenfoot.TestRunnerState.WorldCreator;
             
             @RunWith(GreenfootRunner.class)
             public class ExampleActorRandomTest {
                 @Test
                 public void testRandomNumber() throws Exception {
                     // GIVEN 
                     World world = WorldCreator.getWorld(400, 300, 1);
                     ExampleActorRandom actor = new ExampleActorRandom();
                     world.addObject(actor, 1, 1);
                     
                     // WHEN 
                     Random.set(7);
                     WorldCreator.runOnce(world);
                     
                     // THEN 
                     assertEquals(6, actor.getX());
                 }
             }
         
         
        Parameters:
        nextNumber - will be the number returned by the operation Greenfoot.getRandomNumber(maximun).
        Throws:
        GreenfootRunnerExpected - is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
      • set

        public static void set​(int nextNumber,
                               java.lang.Class<?> callerClass)
                        throws GreenfootRunnerExpected
        It queues a number in FIFO order for any next Greenfoot.getRandomNumber(maximun) operation from objects of specific class.

        For the next actor behaviour:
          
             public class ExampleActorRandom extends Actor {
             
                 @Override
                 public void act(){
                     if (Greenfoot.getRandomNumber(10) == 7) {
                         move(5);
                     }
                 }
             } 
         
         
        The next test verifies that the random number that has been generated produces the wanted behaviour:
          
             import ...
             import greenfoot.TestRunnerState.Random;
             import greenfoot.TestRunnerState.WorldCreator;
             
             @RunWith(GreenfootRunner.class)
             public class ExampleActorRandomTest {
                 @Test
                 public void testRandomNumber() throws Exception {
                     // GIVEN 
                     World world = WorldCreator.getWorld(400, 300, 1);
                     ExampleActorRandom actor = new ExampleActorRandom();
                     world.addObject(actor, 1, 1);
                     
                     // WHEN 
                     Random.set(7, ExampleActorRandom.class);
                     WorldCreator.runOnce(world);
                     
                     // THEN 
                     assertEquals(6, actor.getX());
                 }
             }
         
         
        Parameters:
        nextNumber - will be the number returned by the operation Greenfoot.getRandomNumber(maximun).
        callerClass - is the class of object that can receive the number queued. This class must be derived from Actor or World.
        Throws:
        GreenfootRunnerExpected - is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.