Loading...
Logo
Processing Forum
Hi all,

I'm trying develop some sketchs with P5 library with JUnit Test Case framework
(i think that be useful), but i'm have some difficulties, especially when I boot up a class inherited from PApplet
or using their methods, as follow:

Copy code
  1. iimport static org.junit.Assert.*;

    import org.junit.After;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;

    import processing.core.PApplet;

    public class MondrianDrawTest {

        private static MondrianProcessing mondrian;
        private String[] args = new String[] {"--present", "Mondrian"};

        @BeforeClass
        public static void setUp() throws Exception {
            mondrian = new MondrianProcessing();
        }
       
        @Before
        public void init(){
            mondrian.init();
        }

        @After
        public void tearDown() {
            mondrian.destroy();
        }
       
        @Test
        public void testInit(){
            assertEquals("I'm not under linux?", PApplet.LINUX, PApplet.platform);
            assertEquals("Not Online yet", true, mondrian.online);   
        }
       
        @Test
        public void testRunSketch() {
            //TODO how show applet??   
            //assertEquals(true, mondrian.focused);
        }
    }
I receive for second test (in testInit() method) that the variable online is false;
I verifyed in PApplet source code that variable online is setted to true during init() method;

Anyone knows what's happened?

Thanks

Replies(6)

You are misreading the source code.
It is:
Copy code
  1.     try {
  2.       getAppletContext();
  3.       online = true;
  4.     } catch (NullPointerException e) {
  5.       online = false;
  6.     }
But if the sketch isn't in a browser, getAppletContext() will throw a null pointer exception, so online = true will never be executed, and the online = false will enforce the default value anyway.
mmm, now understood my mistake ... thanks phi.lho!!!

anyways, i have some problems in run the sketch inside JUnit code:

1) If i Run main method, i think that JUnit still run test, but is pauses after execute main()

Processing Code:

Copy code
  1. public class MondrianProcessing extends PApplet {
  2.    
  3.     private static String[] args = new String[] {"Mondrian"};
  4.     private XmlWindowConfig window;
  5.    
  6.     public MondrianProcessing(){
  7.         super();
  8.         window = new XmlWindowConfig(this, "data/window_config.xml");
  9.     }
  10.    
  11.     public static void main(){
  12.         PApplet.main(args);
  13.     }
JUnit Code

Copy code
  1. public class MondrianDrawTest {
  2.     private static MondrianProcessing mondrian;

  3.     @BeforeClass
  4.     public static void setUp() throws Exception {
  5.         mondrian = new MondrianProcessing();
  6.     }
  7.    
  8.     @Before
  9.     public void init(){
  10.         MondrianProcessing.main(); // >> JUnit paused here
  11.     }

2) If I use init() method in JUnit code, Processing Code initializes correctly, but how can I run PApplet (ie, the GUI) correctly during tests? I tried this, and the JUnit is paused as above:

Copy code
  1.    @Before
  2.     public void init(){
  3.         mondrian.init();
  4.     }
  5.     @After
  6.     public void tearDown() {
  7.         mondrian.destroy();
  8.     }
  9.    
  10.     @Test
  11.     public void testInit(){
  12.         assertEquals("I'm not under linux?", PApplet.LINUX, PApplet.platform);
  13.     }
  14.    
  15.     @Test
  16.     public void testRunSketch() {
  17.         PApplet.runSketch(new String[] {"Mondrian"}, mondrian); // >> JUnit pauses here
  18.         assertEquals(true, mondrian.focused);
  19.     }
Unlike the previous example, a java process is started on Linux, verified in xterm:
Copy code
  1. $ ps -A | grep java
     1966 ?        00:00:01 java
But without contents of sketch coded in setup() and draw() methods.
is there another option or trick to run in the PApplet inside JUnit code?

Thanks

I can be wrong, but I think that JUnit isn't designed to test interactive/GUI code. How can you make asserts on graphical code anyway? By checking the color of some pixels?

A better use case for JUnit is to check some functions you made in your sketch. For example if values of a mathematical function are correct, if after an iteration on an array list, the right number of elements have been deleted, etc.
Ie., as the name implies, it is designed to do unit test, testing independently small units of code.

So you won't test the whole lifecycle of a PApplet, but instead some elements of it.
You can be correct...

But i think in this way:

You can Test Android Code in JUnit (with some hard tricks, at least for me)
to verify some lifecycles of Activitys, etc...


So you won't test the whole lifecycle of a PApplet, but instead some elements of it.


I think
would be interesting if we can do the same with our sketchs (i.e.
test some lifecycles,
automation of simulations of interactivity with users,
stress test, check positions of some PGraphics, etc.,  and possible, color in some pixels ;)

this can be possible with other framework?

thanks



I think this could be helpful (updated)
Yes, I thought of recommending FEST, which is a fine library to test Swing applications, but I wasn't sure whether it works well on a pure AWT applet.