【问题标题】:Is it possible to run a Java Application and Junit Test in one Java Class?是否可以在一个 Java 类中运行 Java 应用程序和 Junit 测试?
【发布时间】:2012-07-17 07:17:23
【问题描述】:

我是这里的新手。

我的问题是:是否可以在一个 Java 类中运行这些类?

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

注意:
Test.class -> 这是一个 Junit 测试类
Chart.class -> 这是一个 Java 应用程序类

我希望我的问题很清楚。我的英语不太好。

此代码适用于 Java 应用程序: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);

            } 

    }

此代码用于 JUnit 测试代码: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);
    }   

}

【问题讨论】:

  • *.com/questions/2543912/… 也许它与这个问题有关,但是我只想在一个 java 类中编译它们 - 这样我就可以调用一个类来运行它们,但是我收到一条错误消息:java.lang.Exception:测试类在 Java 应用程序(Chart.class)上应该有一个公共零参数构造函数
  • 你的图表也是单元测试?为什么要以这种方式将您的实际应用程序代码与单元测试代码混合在一起?
  • 我想混合它的目的,因为在 (Test.class) 上,它是 JUnit 测试类,由一系列测试用例组成,并且具有全局变量(失败和通过)只是为了确定失败/通过的项目,我将获取这些变量的值并将其传递给(Chart.class) - 这样它将基于失败和通过的项目生成报告并将其保存在文件夹中。希望你明白我的意思。
  • 看了你的评论3遍,还是没明白你的意思。 Chart 是您定制的某种测试运行器吗?还是被测系统?
  • 在我的例子中,Test.class 是我的 JUnit 测试,它包含一系列测试,例如(@Test、@Before、@After)。执行 Test.class 后,我需要运行 Chart.class 这是我的 java 应用程序来生成饼图。如何在 Test.class(Junit 代码)中运行 Chart.class(Java 应用程序代码)?或者我如何将这两个类合并为一个 java 类,该类将首先运行 Test.class,然后运行 ​​Chart.class?

标签: java class selenium junit


【解决方案1】:

不,这是不可能的。

@Suite.SuiteClasses({}) 下列出的类必须是有效的 Junit4 测试类。这些类至少有一个方法用@Test 注释。

【讨论】:

  • 我明白了,你有什么建议我应该怎么做?我有一个 Java 应用程序和 JUnit 测试,因为类(Chart.class)只会获取失败的值并传入类(Test.class),所以它会生成一个饼图。注意: - 我在命令提示符下使用 ANT 运行(Test.class)。
【解决方案2】:

在我看来,您只是想获得测试结果并根据它生成一些特殊的报告。为什么不简单地编写一个小程序来读取 JUnit 生成的测试报告并创建您想要的图表?


(已添加)虽然我认为您所做的方向不正确并且没有意义,但您可能可以研究一下。

您说要调用Chart根据Test中的信息生成饼图,可以考虑使用@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 将在该类中的所有测试被调用后被调用。

但是,这样做仍然毫无意义,因为单元测试的整个想法是自动完成验证等,您不应该对过程进行手动检查或测试结果。生成图表无助于告诉计算机什么是成功/失败。而且,你的代码对我的建议只不过是普通的单元测试报告,告诉你有多少测试用例成功/失败。为什么要做一些没有实际价值的额外事情?

【讨论】:

  • 我们的目标是创建一个类来运行这两个类(Test.class 和 Chart.class),只需调用它们,而不是用这两个类的长脚本创建一个类类。我们试图从 Chart.class 调用 Test.class 但我们似乎有错误的语法。我将附上我的两个班级的代码,以便您更好地理解它。这两个类在每次运行时都能成功运行,但我们正在寻找一个可以同时运行这两个类的类。 :)
  • 它仍然没有回答我的批评:如果您使用图表生成单元测试报告,为什么不运行测试生成测试结果,然后使用图表生成报告从测试结果?而且,老实说,我在您使用 JUnit 的过程中感到非常奇怪。我敢打赌,你要实现的目标是错误的。
  • 好的,我将把它放在另一个问题上以说明清楚? :) 你能帮我如何在 JUnit 中使用 PieChart 生成报告(失败/通过)项目吗?
  • @haroldcuellar 不赞成大幅改变问题的含义。您应该保持原样并为后续问题创建第二个。为什么不直接调用 call Chart.main("");来自@AfterClass?
  • 对不起,我很难用英语真正解释我的意思或表达我自己。无论如何,为了简短起见,我们使用 selenium 记录了一个测试用例,并将该代码转换为 JUnit 代码并将其粘贴到 Eclipse 上,所以现在我们的一般目的是,我们需要计算所有失败/通过的项目并制作报告带有饼图。
最近更新 更多