java - 是否可以在一个 Java 类中运行 Java 应用程序和 Junit 测试?

标签 java class selenium junit

我是这里的新手。

我的问题是:是否可以在一个 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);
    }   

}

最佳答案

不,这是不可能的。

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

关于java - 是否可以在一个 Java 类中运行 Java 应用程序和 Junit 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11517696/

相关文章:

java - 安装 Java 时在 Ubuntu 上安装 Elastic Search 失败

java - DateFormat UnitTest 在 Jenkins 中失败,但在本地失败

java - 从JAVA调用RPG程序

python - 如何覆盖父类中的字段

java - 如何使用 Selenium WebDriver 在 Chrome 中激活 AdBlocker?

Java:对 map 进行排序

c++ - C++ 11遍历 vector 的新方法?

javascript - 在 Coffeescript 中调用方法并传递适当的上下文

java - 无法在 'evaluate' 上执行 'Document' ,错误的 xpath 表达式

python - 无法单击并按住 Selenium 中的元素