繁体   English   中英

如何调用驻留在不同项目中的方法?

[英]How to call an method which resides in different project?

我在Eclipse工作空间中有两个单独的项目是相同的,但是两个项目不同。 假设有两个项目A和B,我已经调用了一个类,假设Test.java具有项目b的主要方法和一个简单的显示方法,而我需要从项目B中调用。在主方法中,我正在调用Test.java类的显示方法。

并假设我在项目A中有另一个类Execute.java ,所以我需要从项目B到Execute.java类中的项目A的Execute.java类,我该怎么做?

任何建议将不胜感激

文件:TestNGCreator.java

package testDrivers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import projlib.Globals;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;




public class TestNGCreator 
{

    public void runTestNGTest() throws IOException 
    {

            //Create an instance on TestNG
            TestNG myTestNG = new TestNG();

            //Create an instance of XML Suite and assign a name for it.
            XmlSuite mySuite = new XmlSuite();
            mySuite.setName(Globals.SUITE_NAME);

            //Create a list of XmlTests and add the Xmltest you created earlier to it.
            List<XmlTest> myTests = new ArrayList<XmlTest>();

            XSSFSheet excelWSheet = null;
            XSSFWorkbook excelWBook = null;
            XSSFCell cell = null;
            XSSFRow row = null;

            Double dblCellVal;
            String strCellVal = null;
            Boolean blnCellVal;

            FileInputStream excelFile = new FileInputStream(Globals.CONFIG_FILE_DIR);
            excelWBook = new XSSFWorkbook(excelFile);
            //excelWSheet = excelWBook.getSheet(Globals.CONFIG_SHEET_NAME);


            Iterator <Row> rowIterator = excelWSheet.iterator(); 
            //Iterator <Cell> cellIterator = row.cellIterator();
            String testName = null;
            int colCount;

            if (rowIterator.hasNext())
            {
                row = (XSSFRow) rowIterator.next();
                colCount = row.getPhysicalNumberOfCells();
            }
            while (rowIterator.hasNext())
            {
                row = (XSSFRow) rowIterator.next();
                Iterator <Cell> cellIterator = row.cellIterator();
                int curCell = 0;
                while (cellIterator.hasNext())
                {
                    cell = (XSSFCell) cellIterator.next();
                    curCell++;
                    switch (cell.getCellType())
                    {
                    case Cell.CELL_TYPE_NUMERIC:
                        dblCellVal = cell.getNumericCellValue();
                        strCellVal = dblCellVal.toString();
                        break;
                    case Cell.CELL_TYPE_STRING:
                        strCellVal = cell.getStringCellValue();
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        blnCellVal = cell.getBooleanCellValue();
                        strCellVal = blnCellVal.toString();
                        break;
                    }

                    //If it is first cell then store the Test Name
                    if (cell.getColumnIndex()== 0)
                    {
                        testName = strCellVal;
                    }

                    if (curCell == 5) {
                        if (strCellVal.equals("1.0")) {
                            //Adding to suite
                            //Create an instance of XmlTest and assign a name for it.
                            XmlTest myTest = new XmlTest(mySuite);
                            myTest.setName(testName);

                            //Add any parameters that you want to set to the Test.
                            Map<String, String> testngParams = new HashMap<String,String> ();
                            testngParams.put("testId", testName);
                            myTest.setParameters(testngParams);

                            //Create a list which can contain the classes that you want to run.
                            List<XmlClass> myClasses = new ArrayList<XmlClass> ();
                            myClasses.add(new XmlClass("TestDriver"));

                            //Assign that to the XmlTest Object created earlier.
                            myTest.setXmlClasses(myClasses);

                            //Adding the test to test list created earlier
                            myTests.add(myTest);
                            break;

                        }
                    }
                }

            }

            excelWBook.close();

            //add the list of tests to your Suite.
            mySuite.setTests(myTests);

            //Add the suite to the list of suites.
            List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
            mySuites.add(mySuite);

            //Set the list of Suites to the testNG object you created earlier.
            myTestNG.setXmlSuites(mySuites);

            File file = new File(Globals.TESTNG_FILE_NAME);
            System.out.println("File is: " + file);

            FileWriter writer = new FileWriter(file);
            writer.write(mySuite.toXml());
            writer.close();

            //invoke run() - this will run your class.
            //myTestNG.run();




    }
    public static void main(String args[]) throws IOException
    {
        TestNGCreator testDriver = new TestNGCreator();
        testDriver.runTestNGTest();
    }
}

所以我需要调用main方法。

您可以按照以下步骤进行操作。

  1. 生成第一个项目并创建jar
  2. 右键单击第二个项目->构建路径->配置构建路径->库->添加外部Jar
  3. 使用第一个项目中的课程

要么

如果要从任何Web服务发送/接收数据,请以REST API的方式公开您的方法,以使Web服务可以接受它。

您可以使用Apache HttpComponents将您的方法公开给Web服务,请找到以下示例代码。

CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(config)
                    .disableContentCompression().setSSLSocketFactory(sslsf).build();

HttpPost postRequest = new HttpPost(url);
client.execute(postRequest);

如果您不使用Maven或Gradle,则可以执行以下步骤以包含项目:

属性->构建路径->项目->添加,添加所需的项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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