Problem with processing library in java JUnit

Hello im having a lot of problems with creating a SlideShow in processing, but i have 2 big questions:

How to implement JUnit is a processing library in java.

How to save a sketch as a image with all the contents that the draw() draws in a java test.

I’m honestly not sure, as I’ve never tried to do this myself.

Just thinking out loud, Processing lets you save the content of your frame using the save() or saveFrame() functions. You can also use the get() function to look at each pixel of your frame, without saving an image. In theory you could use those and then compare the results to some previously saved image.

Another option would be to mock out the underlying Processing environment and make sure your code is calling the right Processing functions.

Both of those are going to be pretty brittle though. Do you really want your test to fail if one pixel changes? Do you get value from confirming that your test calls specific Processing functions?

If you want to go down the rabbit hole of writing unit tests for Processing, this discussion looks like a good starting point: Pixel test for simple sketches -- feedback request - Processing 2.x and 3.x Forum

I saved the image with save and then i compared pixel by pixel and if the difference is more than 10% it hits fail, the problem is that to make the save y have to run the frame(i think), so i created a new class that i run and make the sketch to appear so i can save it, i tried in the Junit tests but it fails.

this is the the class with no Junit
@Override
public void draw() {
theSlideShow.draw();
this.save(“input.jpg”);

}

public static void main(String[] args) {
	PApplet.main(new String[] { "test.LibraryProcessing"});
	
	
	
}

this is the Junit

@Test
public void cmpSketch() {
	LibraryProcessing.main(args);
	double diff = LibraryProcessing.checkDifference();
	if(diff > 10.0) {			
	fail("There is more than 10% difference, try to do the capture again.");
	}

In general that sounds like the right approach to me. What line is your test failing on?

One thing I’d recommend is splitting this up into smaller steps. Instead of trying to get your slide show working in a JUnit test, can you try something smaller like drawing a single circle? Get that working in JUnit before you try adding in the rest of your library’s functionality.

If you’re still having trouble with that smaller example, it’ll be a lot easier to help you debug!

the problem is JUnit if i do this as a normal method in a main of a class it works, but if i do in a test of Junit it opens the sketch and close instantly.

You might need to wait for Processing to start up and draw the first frame? Maybe try adding a wait in your test function? Or to confirm that this is the case, can you add a print statement in your draw() function to check that it’s actually being called?