2016-12-20 41 views
1

mvn測試不適用於Apache Poi。mvn測試不適用於Apache Poi

  1. 如果我排除我的框架內,相關到Apache POI代碼:MVN測試工作。

  2. 但是我的代碼需要訪問Excel工作表中的特定數據,因此我需要將'mvn test'和Apache POI一起使用。

  3. ,似乎可以用MVN測試時將收到以下異常: enter image description here

  4. 例如,如果我的評論下面的代碼「例子」 MVN測試成功執行我的測試用例: enter image description here

enter image description here

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

<groupId>uk.co.uat.pizzahut</groupId> 
<artifactId>PhFramework</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>jar</packaging> 

<name>PhFramework</name> 
<url>http://maven.apache.org</url> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 

<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.3</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.19.1</version> 
       <configuration> 
        <suiteXmlFiles> 
         <!-- TestNG suite XML files --> 
         <suiteXmlFile>testng.xml</suiteXmlFile> 
        </suiteXmlFiles> 
       </configuration> 
      </plugin> 

     </plugins> 
    </pluginManagement> 
</build> 


<dependencies> 
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-java</artifactId> 
     <version>2.53.1</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> 
    <dependency> 
     <groupId>commons-io</groupId> 
     <artifactId>commons-io</artifactId> 
     <version>2.4</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> 
    <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi</artifactId> 
     <version>3.15-beta2</version> 
    </dependency> 


    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> 
    <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi</artifactId> 
     <version>3.15</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-collections4</artifactId> 
     <version>4.0</version> 
    </dependency> 

    <dependency> 
     <groupId>commons-collections</groupId> 
     <artifactId>commons-collections</artifactId> 
     <version>3.2.2</version> 
    </dependency> 

    <dependency> 
     <groupId>com.danielflower.apprunner</groupId> 
     <artifactId>app-runner</artifactId> 
     <version>0.1.8</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/log4j/log4j --> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.17</version> 
    </dependency> 

    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.testng</groupId> 
     <artifactId>testng</artifactId> 
     <version>6.8</version> 
    </dependency> 

    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-server</artifactId> 
     <version>3.0.1</version> 
    </dependency> 

    <dependency> 
     <groupId>com.google.guava</groupId> 
     <artifactId>guava</artifactId> 
     <version>20.0</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans --> 
    <dependency> 
     <groupId>org.apache.xmlbeans</groupId> 
     <artifactId>xmlbeans</artifactId> 
     <version>2.6.0</version> 
    </dependency> 

</dependencies> 

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server --> 

的Excel讀者代碼:

private XSSFSheet sheet = null; 
private XSSFRow row = null; 
private XSSFCell cell = null; 
String path = null; 


public ExcelReader() throws IOException { 
    path = System.getProperty("user.dir") + "\\src\\main\\java\\PhFramework\\testData\\pricebandPrices.xlsx"; 
    fis = new FileInputStream(path); 
    workbook = new XSSFWorkbook(fis); 


    sheet = workbook.getSheetAt(0); 
} 


public int getSheetRows() throws IOException { 
    Properties p = new Properties(); 
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties"); 
    p.load(fi); 
    int index = workbook.getSheetIndex(p.getProperty("priceband")); 
    sheet = workbook.getSheetAt(index); 
    return (sheet.getLastRowNum() + 1); 
} 


public int getSheetColumns(String sheetName) throws IOException { 
    Properties p = new Properties(); 
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties"); 
    p.load(fi); 
    int index = workbook.getSheetIndex(p.getProperty("priceband")); 
    sheet = workbook.getSheetAt(index); 
    row = sheet.getRow(0); 
    return (row.getLastCellNum()); 
} 


public String getCellDataString(int colNum, int rowNum) throws IOException { 
    Properties p = new Properties(); 
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties"); 
    p.load(fi); 
    int index = workbook.getSheetIndex(p.getProperty("priceband")); 
    sheet = workbook.getSheetAt(index); 
    row = sheet.getRow(rowNum); 
    cell = row.getCell(colNum); 
    return (cell.getStringCellValue()); 
} 

public double getCellDataDouble(int colNum, int rowNum) throws IOException { 
    Properties p = new Properties(); 
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties"); 
    p.load(fi); 
    int index = workbook.getSheetIndex(p.getProperty("priceband")); 
    sheet = workbook.getSheetAt(index); 
    row = sheet.getRow(rowNum); 
    cell = row.getCell(colNum); 
    return (cell.getNumericCellValue()); 
} 

public String getCellDataText(String colName, int rowNum) throws IOException { 
    Properties p = new Properties(); 
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties"); 
    p.load(fi); 
    int colNum = -1; 
    int index = workbook.getSheetIndex(p.getProperty("priceband")); 
    sheet = workbook.getSheetAt(index); 
    for (int i = 0; i < getSheetColumns(p.getProperty("priceband")); i++) { 
     row = sheet.getRow(0); 
     cell = row.getCell(i); 
     if (cell.getStringCellValue().equals(colName)) { 
      colNum = cell.getColumnIndex(); 
      break; 
     } 
    } 
    row = sheet.getRow(rowNum); 
    cell = row.getCell(colNum); 
    return (cell.getStringCellValue()); 
} 

public double getCellDataNumber(String colName, int rowNum) throws IOException { 
    Properties p = new Properties(); 
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties"); 
    p.load(fi); 
    int colNum = -1; 
    int index = workbook.getSheetIndex(p.getProperty("priceband")); 
    sheet = workbook.getSheetAt(index); 
    for (int i = 0; i < getSheetColumns(p.getProperty("priceband")); i++) { 
     row = sheet.getRow(0); 
     cell = row.getCell(i); 
     if (cell.getStringCellValue().equals(colName)) { 
      colNum = cell.getColumnIndex(); 
      break; 
     } 
    } 
    row = sheet.getRow(rowNum); 
    cell = row.getCell(colNum); 
    return (cell.getNumericCellValue()); 
} 

public void setCellData(int colNum, int rowNum, String str) throws IOException { 
    Properties p = new Properties(); 
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties"); 
    p.load(fi); 
    int index = workbook.getSheetIndex(p.getProperty("priceband")); 
    sheet = workbook.getSheetAt(index); 
    row = sheet.getRow(rowNum); 
    cell = row.createCell(colNum); 
    cell.setCellValue(str); 
    try { 
     fileOut = new FileOutputStream(path); 

     try { 
      workbook.write(fileOut); 
      fileOut.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

} 

enter image description here

+0

apache poi依賴如何在pom.xml中定義? – Vladimir

+0

我想你錯過了pom.xml中Apache-poi jar的入口 – Amit1011

+0

'mvn dependency:tree'的輸出是什麼? –

回答

2

org.apache.poi.xssf...poi-ooxml神器提供的,你需要把它添加到您的pom.xml:

<dependency> 
    <groupId>org.apache.poi</groupId> 
    <artifactId>poi-ooxml</artifactId> 
    <version>3.15</version> 
</dependency> 

您可能還需要刪除3.15-beta2poi版本的神器

+0

,好的我將添加上面列出的以下依賴項並提供更新,再次感謝 – Gbru

+0

我已經刪除了手動依賴關係,並用上面的代碼更新了POM,只留下了一個版本的POI 3.15。即使有了這些更改,我的測試用例也會執行,但是在達到從Excel表讀取的方法時,我會得到'null.pointer異常'。請看我上面的Excel Reader代碼 – Gbru

+0

奇怪,當我手動添加poi3.15-beta2它能夠讀取excel數據,但然後MVN測試dosnt的工作,感謝您的幫助 – Gbru