2

I'm a newbie here.

My question is: Is it possible to run those classes in one Java class?

@RunWith(Suite.class)
@Suite.SuiteClasses({
   Test.class,
   Chart.class,
})

Note:
Test.class -> This is a Junit Test Class
Chart.class -> This is a Java Application Class

I hope my question is clear. I'm not totally good in English.

This Code is for Java Application: Chart.Class

public static class PieChart extends JFrame {


        private static final long serialVersionUID = 1L;

        public PieChart(String applicationTitle, String chartTitle) {
            super(applicationTitle);
            // This will create the dataset 
            PieDataset dataset = createDataset();
            // based on the dataset we create the chart
            JFreeChart chart = createChart(dataset, chartTitle);
            // we put the chart into a panel
            ChartPanel chartPanel = new ChartPanel(chart);
            // default size
            chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
            // add it to our application
            setContentPane(chartPanel);
            // it will save the chart on the specified location
            String fileLocation = "C:/temp/pieChartReport.jpg";
            saveChart(chart, fileLocation);   

        }

        /**
         * Creates a sample dataset 
         */
        ABMTLinks abmt = new ABMTLinks();


        private  PieDataset createDataset() {
            DefaultPieDataset result = new DefaultPieDataset();
            result.setValue("Failed:", abmt.Fail);
            result.setValue("Error:", 100);
            result.setValue("Passed:", abmt.Pass);
            return result;


        }

        /**
         * Creates a chart
         */

        private JFreeChart createChart(PieDataset dataset, String title) {

            JFreeChart chart = ChartFactory.createPieChart3D(title,                 // chart title
                dataset,                // data
                true,                   // include legend
                true,
                true);

            PiePlot3D plot = (PiePlot3D) chart.getPlot();
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {1} {2}")); //Shows the total count and percentage for Failed/Passed/Error
            plot.setStartAngle(480);
            plot.setDirection(Rotation.CLOCKWISE);
            plot.setForegroundAlpha(0.5f);
            return chart;

        }

        //This will store the chart on the specified location/folder
        public void saveChart(JFreeChart chart, String fileLocation) {
            String fileName = fileLocation;
            try {
                /**
                 * This utility saves the JFreeChart as a JPEG First Parameter:
                 * FileName Second Parameter: Chart To Save Third Parameter: Height
                 * Of Picture Fourth Parameter: Width Of Picture
                 */
                ChartUtilities.saveChartAsJPEG(new File(fileName), chart, 500, 270);
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println("Problem occurred creating chart.");
            }
        }



        public static void main(String[] args) {
            String pieChartTitle = "Harold's Pie Chart";
            String pieChartName = "Pie Chart";
            PieChart demo = new PieChart(pieChartName, pieChartTitle);
            demo.pack();
            demo.setVisible(true);

            } 

    }

This Code is for JUnit Test Code: Test.Class

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;


public class ABMTLinks extends SeleneseTestCase {

      public int Fail=10, Pass=10;
      public static String
       //Declaring of variables to store the Actual values for URLs
         HMT = "http://dev.abmt.igloo.com.au/GetInvolved/Hostamorningtea/tabid/165/Default.aspx",
         DMT = "http://dev.abmt.igloo.com.au/GetInvolved/Donatetoamorningtea/tabid/141/Default.aspx";   

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 1111, "*googlechrome", "http://dev.abmt.igloo.com.au/");
        selenium.start();
    }

    @Test
    public void testUntitled() throws Exception {
        selenium.open("/GetInvolved/tabid/114/Default.aspx");
        selenium.click("link=Get Involved");
        selenium.waitForPageToLoad("30000");

        if (URL.equals(HMT)
            && URL1.equals(DMT)
            ){

            Pass = Pass + 1;
            System.out.println("All pages redirects to each URL with no errors");           
        }
        else {
            Fail = Fail + 1;
            assertTrue("Test Case is Failed!", false);
            System.out.print("Failed");     
        }           
    }

    @After
    public void tearDown() throws Exception {   
        System.out.println(Fail + "+" + Pass);
    }   

}
  • 1
    _http://stackoverflow.com/questions/2543912/how-do-i-run-junit-tests-from-inside-my-java-application_ maybe it is related to this issue, however i just want to compile them in one java class - so that i will just call one class to run them, however i get an error messages saying: *java.lang.Exception: Test class should have exactly one public zero-argument constructor* on the Java Application (Chart.class). – harold cuellar Jul 17 '12 at 07:27
  • 3
    Your Chart is also a unit test? Why do you want to mix your actual application code with unit test code in such way? – Adrian Shum Jul 17 '12 at 09:10
  • The purpose why i want to mix that, because on (Test.class) which is JUnit Test Class, consists of series of test cases, and has global variables (Failed & Passed) just to determine the failed/passed items, and i will get the values for those variables and pass it on (Chart.class) - so that it will generate a report base on the Failed & Passed items and saves it on a folder. hope you get what i mean. – harold cuellar Jul 18 '12 at 01:01
  • After reading your comment for 3 times, I still don't get what you mean. Is Chart some kind of your customized test runner? or that the system under test? – Adrian Shum Jul 18 '12 at 02:27
  • In my case, Test.class is my JUnit Test, that consist series of test like (@Test, @Before, @After). After executing the Test.class, I need to run Chart.class which is my java application to generate a pie chart. How can I run the Chart.class(java application code) inside the Test.class(Junit code)? or how can i merge those two classes into one java class that will run the Test.class first then the Chart.class? – harold cuellar Jul 18 '12 at 02:58

2 Answers2

2

No it is not possible.

Classes that are listed under @Suite.SuiteClasses({}) have to be valid Junit4 Test-Classes. These are classes that have at least one method annotated with @Test.

oers
  • 18,436
  • 13
  • 66
  • 75
  • I see, you have any suggestions what should i do then? i have a Java Application and JUnit Test, because the class(Chart.class) will only get the value of failed and passed in class(Test.class) so it will generate a pie chart. Note: - I'm running the (Test.class) using ANT in command prompt. – harold cuellar Jul 17 '12 at 23:00
0

It seems to me that you are only trying to get the test result and generate some special report base on it. Why not simply write a little program to read the test report generated by JUnit and create the chart you want?


(Added) Though I believe what you are doing is heading incorrect direction and is not making sense, there are probably something you can look into.

You said you want to invoke Chart to generate a pie chart base on information in Test, you can consider using @AfterClass:

public class Test {
  private int success;
  private int fail;


  @AfterClass
  public void generateChart() {
    // generate your chart base on this.success and this.fail
  }
}

@AfterClass will be invoked after all tests in this class is invoked.

But still, it is quite meaningless in doing so, because the whole idea of unit test is to have validations etc all done automatically and you shouldn't do manual inspection with the process or result in test. Generating chart helps nothing in telling the computer what's success/failure. And, what's your code suggests to me is nothing more than normal unit test report, telling you how many test case succeeded/failed. Why do something extra without real value?

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • Our goal here is to create a single class that will run these two classes (Test.class and Chart.class) by just calling them and not to make a one class with the long scripts of these two classes. We're trying to call the Test.class from the Chart.class but we seem to have the wrong syntax for it. I will just attach my codes for two class so that you may understand it better. These two classes work successfully by each run but we're looking for a single class that will run these two classes simultaneously. :) – harold cuellar Jul 18 '12 at 03:32
  • It still don't answer my criticism: If you are using Chart for generating reports for unit test, why don't you run the test to generate test result, and then use your Chart to generate report from the test result? And, honestly I feel super weird on your way in using JUnit. I bet you are heading a wrong direction for what you are going to achieve. – Adrian Shum Jul 18 '12 at 04:23
  • Ok, i will just put it on another question to make it clear? :) Can you help me how can I generate a report (Failed/Passed) items with a PieChart in JUnit? – harold cuellar Jul 18 '12 at 05:27
  • @haroldcuellar it is frowned upon to change to meaning of a question drastically. You should have left this one as it was and created a second one for the follow up question. And why don you just call call Chart.main(""); from @AfterClass? – oers Jul 18 '12 at 05:47
  • sorry, it's hard for me to explain really what i mean or express my self in english. Anyway to make it short, we record a test case using selenium, and we convert that codes into JUnit code and paste it on Eclipse, so now our general purpose here is that, we need to count all failed/passed items and make a report with a pie chart. – harold cuellar Jul 18 '12 at 06:39
  • As I said in the answer, you can do so in @AfterClass. However what you described still don't require such a messy implementation. You can simply have each test case being one of your selenium test case. And after invocation, you can simply inspect the test result files (which contains no. of success and fail test) for generating the pie chart report. It sounds so weird that you have to keep the success/fail yourself while it is already available – Adrian Shum Jul 18 '12 at 06:59
  • @AdrianShum it seems that i already solve my issue.. haha i have used the inheritance. however I'm encountering the same as this: http://stackoverflow.com/questions/1451496/does-junit4-testclasses-require-a-public-no-arg-constructor and http://stackoverflow.com/questions/672466/junit-how-to-avoid-no-runnable-methods-in-test-utils-classes.. can you guide me? how? – harold cuellar Jul 19 '12 at 04:58
  • I got an error: Test class should have exactly one public zero-argument constructor java.lang.Exception: Test class should have exactly one public zero-argument constructor at java.lang.reflect.Constructor.newInstance(Unknown Source) No runnable methods java.lang.Exception: No runnable methods at java.lang.reflect.Constructor.newInstance(Unknown Source) – harold cuellar Jul 19 '12 at 06:44
  • The link you showed already give reasonable information on it. Still, I still think you are going a wrong direction and probably I should stop giving comment on this. – Adrian Shum Jul 19 '12 at 06:48