简体   繁体   中英

Why does my JFrame not appear in Unit Test?

I'm trying to make a JFreeChart to compare the problem size of a tested method to its running time.

Here is the class that makes the scatter plot:

public class TestScatterPlot extends JFrame {
    public TestScatterPlot(String title, XYSeriesCollection dataset){
        super(title);
        JFreeChart chart = ChartFactory.createScatterPlot(
                "Time to problem size",
                "problem size",
                "time",
                dataset);
        XYPlot plot = (XYPlot)chart.getPlot();
        plot.setBackgroundPaint(new Color(255,228,196));


        // Create Panel
        ChartPanel panel = new ChartPanel(chart);
        setContentPane(panel);
    }
}

Here is the test method:

   @Test
   public void testHierholzersAlgorithm() {
        Map<Integer,Long> timeToProblemSize = new HashMap<>();
        for(int trial = 0;trial<1000;trial++) {
            //generate the test data
            long startTime = System.nanoTime();
            //run the method
            long runTime = System.nanoTime() - startTime;
            int dataSize = dataSize();
            //test the data
            timeToProblemSize.put(dataSize,runTime);


        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series = new XYSeries("TimeToProblemSize");
        for(Integer probSize:timeToProblemSize.keySet()){
            series.add(probSize,timeToProblemSize.get(probSize));
        }
        dataset.addSeries(series);
        SwingUtilities.invokeLater(() -> {
            TestScatterPlot example = new TestScatterPlot("",dataset);
            example.setSize(800, 400);
            example.setLocationRelativeTo(null);
            example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            example.setVisible(true);
        });
    }

When I run this, the chart frame seems to begin to appear then closes immediately.

How do I get my chart to show?

Note:

This is not a repeat of this question because the questioner is using a scanner reading user input. There is no scanner in this test method; all the input is generated randomly.

It is not a repeat of this question either. The questioner there had a Thread.sleep happening. There is no Thread.sleep here.

I am assuming as you are running this as a unit test the test environment stops the test as soon as it reaches the end of your test method. The invokeLater will probably not have run yet at this point.

You can test this with a simple test such as:

void test()
    {
        SwingUtilities.invokeLater(() -> {
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                System.out.println("interrupt");
            }
            System.out.println("went through");
        });
    }

which will do exactly nothing as the Thread gets shut down before it can print the went through.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM