Class Ask


  • public class Ask
    extends java.lang.Object
    It simulates string input for doing Test Driven Development (TDD) with JUnit. If we wants to verify the behaviour of any actor or world when it is stopped waiting Greenfoot.ask("..."), the test can act as user and generates a possible out string.
    Author:
    Francisco Guerra (francisco.guerra@ulpgc.es)
    • Constructor Summary

      Constructors 
      Constructor Description
      Ask()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void set​(java.lang.String nextAsk)
      It queues a string in FIFO order for any next Greenfoot.ack("...") operation.
      static void set​(java.lang.String nextAsk, java.lang.Class<?> callerClass)
      It queues a string in FIFO order for any next Greenfoot.ack("...") 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

      • Ask

        public Ask()
    • Method Detail

      • set

        public static void set​(java.lang.String nextAsk)
                        throws GreenfootRunnerExpected
        It queues a string in FIFO order for any next Greenfoot.ack("...") operation.

        For the next actor behaviour:
          
             public class ExampleActorAsk extends Actor {
                 private String ask;
                
                 @Override
                 public void act(){
                     ask = Greenfoot.ask("");
                 }
        
                 public String getAsk(){
                     return ask;
                 }
             } 
         
         
        The next test verifies the string that has been read by Greenfoot.ask("") operation:
          
             import ...
             import greenfoot.TestRunnerState.Ask;
             import greenfoot.TestRunnerState.WorldCreator;
             
             @RunWith(GreenfootRunner.class)
             public class ExampleActorAskTest {
                 @Test
                 public void testAskAnyString() throws Exception {
                     // GIVEN 
                     World world = WorldCreator.getWorld(400, 300, 1);
                     ExampleActorAsk actor = new ExampleActorAsk();
                     world.addObject(actor, 1, 1);
                     
                     // WHEN 
                     Ask.set("AnyString");
                     WorldCreator.runOnce(world);
                     
                     // THEN 
                     assertEquals("AnyString", actor.getAsk());
                 }
             }
         
         
        Parameters:
        nextAsk - will be the string returned by the operation Greenfoot.ask("").
        Throws:
        GreenfootRunnerExpected - is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
      • set

        public static void set​(java.lang.String nextAsk,
                               java.lang.Class<?> callerClass)
                        throws GreenfootRunnerExpected
        It queues a string in FIFO order for any next Greenfoot.ack("...") operation from objects of specific class.

        For the next actor behaviour:
          
             public class ExampleActorAsk extends Actor {
                 private String ask;
                
                 @Override
                 public void act(){
                     ask = Greenfoot.ask("");
                 }
        
                 public String getAsk(){
                     return ask;
                 }
             } 
         
         
        The next test verifies the string that has been read by Greenfoot.ask("") operation:
          
             import ...
             import greenfoot.TestRunnerState.Ask;
             import greenfoot.TestRunnerState.WorldCreator;
             
             @RunWith(GreenfootRunner.class)
             public class ExampleActorAskTest {
                 @Test
                 public void testAskAnyString() throws Exception {
                     // GIVEN 
                     World world = WorldCreator.getWorld(400, 300, 1);
                     ExampleActorAsk actor = new ExampleActorAsk();
                     world.addObject(actor, 1, 1);
                     
                     // WHEN 
                     Ask.set("AnyString", ExampleActorAsk.class);
                     WorldCreator.runOnce(world);
                     
                     // THEN 
                     assertEquals("AnyString", actor.getAsk());
                 }
             }
         
         
        Parameters:
        nextAsk - will be the string returned by the operation Greenfoot.ask("").
        callerClass - is the class of object that can receive the string 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.