2017-05-12 134 views
0

我的TestNG項目只包含一個包含兩個@Test方法的類,以下是我的Runner類中的main方法。JAR - Selenium Java TestNG Maven項目的JAR文件返回失敗

public class Runner { 

    public static void main(String[] args) { 
     TestListenerAdapter tla = new TestListenerAdapter(); 
     TestNG testng = new TestNG(); 
     testng.setTestClasses(new Class[] { MyTestNGClass.class }); 
     testng.addListener(tla); 
     testng.run(); 
    } 

} 

問題正在運行的主要方法,從Eclipse的Java應用程序運行完全正常,但是當我生成我的項目中使用Eclipse中的「導出」選項的JAR文件,並嘗試運行JAR文件使用java -jar MyJar.jar它返回下面的錯誤上控制檯:

[TestNG] Running: 
    Command line suite 


=============================================== 
Command line suite 
Total tests run: 1, Failures: 1, Skips: 0 
=============================================== 

下面是結果在內部測試輸出目錄生成TestNG的-results.xml。

<exception class="java.lang.RuntimeException"><message>java.lang.NullPointerException</message><full-stacktrace>java.lang.RuntimeException: java.lang.NullPointerException 
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:143) 
    at org.testng.internal.Parameters.handleParameters(Parameters.java:426) 
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1383) 
    at org.testng.internal.Invoker.createParameters(Invoker.java:1075) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
    at org.testng.TestRunner.privateRun(TestRunner.java:767) 
    at org.testng.TestRunner.run(TestRunner.java:617) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123) 
    at org.testng.TestNG.run(TestNG.java:1031) 
    at com.agrostar.pushnotifications.Runner.main(Runner.java:13) 
Caused by: java.lang.NullPointerException 
    at java.io.Reader.<init>(Reader.java:78) 
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72) 
    at utils.GoogleSheetsInit.authorize(GoogleSheetsInit.java:68) 
    at utils.GoogleSheetsInit.getSheetsService(GoogleSheetsInit.java:88) 
    at utils.GoogleSheetsReader.getRowWiseData(GoogleSheetsReader.java:41) 
    at com.agrostar.pushnotifications.PushNotificationSender.googleSheetsDataProvider(PushNotificationSender.java:85) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) 
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:117) 
    ... 18 more 
</full-stacktrace></exception> 

下面是拋出異常的類的代碼。

package utils.Google; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.Arrays; 
import java.util.List; 

import com.google.api.client.auth.oauth2.Credential; 
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; 
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; 
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; 
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; 
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.json.JsonFactory; 
import com.google.api.client.json.jackson2.JacksonFactory; 
import com.google.api.client.util.store.FileDataStoreFactory; 
import com.google.api.services.sheets.v4.Sheets; 
import com.google.api.services.sheets.v4.SheetsScopes; 

public class GoogleSheetsInit { 
    /** Application name. */ 
    private static final String APPLICATION_NAME = 
     "Google Sheets API Java Quickstart"; 

    /** Directory to store user credentials for this application. */ 
    private static final java.io.File DATA_STORE_DIR = new java.io.File(
     System.getProperty("user.home"), ".credentials/sheets.googleapis.com-java-quickstart"); 

    /** Global instance of the {@link FileDataStoreFactory}. */ 
    private static FileDataStoreFactory DATA_STORE_FACTORY; 

    /** Global instance of the JSON factory. */ 
    private static final JsonFactory JSON_FACTORY = 
     JacksonFactory.getDefaultInstance(); 

    /** Global instance of the HTTP transport. */ 
    private static HttpTransport HTTP_TRANSPORT; 

    /** Global instance of the scopes required by this quickstart. 
    * 
    * If modifying these scopes, delete your previously saved credentials 
    * at ~/.credentials/sheets.googleapis.com-java-quickstart 
    */ 
    private static final List<String> SCOPES = 
     Arrays.asList(SheetsScopes.SPREADSHEETS_READONLY); 

    static { 
     try { 
      HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
      DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); 
     } catch (Throwable t) { 
      t.printStackTrace(); 
      System.exit(1); 
     } 
    } 

    /** 
    * Creates an authorized Credential object. 
    * @return an authorized Credential object. 
    * @throws IOException 
    */ 
    public static Credential authorize() throws IOException { 
     // Load client secrets. 
     InputStream in = 
      GoogleSheetsInit.class.getResourceAsStream("/client_secret.json"); 
     GoogleClientSecrets clientSecrets = 
      GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 

     // Build flow and trigger user authorization request. 
     GoogleAuthorizationCodeFlow flow = 
       new GoogleAuthorizationCodeFlow.Builder(
         HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
       .setDataStoreFactory(DATA_STORE_FACTORY) 
       .setAccessType("offline") 
       .build(); 
     Credential credential = new AuthorizationCodeInstalledApp(
      flow, new LocalServerReceiver()).authorize("user"); 
     return credential; 
    } 

    /** 
    * Build and return an authorized Sheets API client service. 
    * @return an authorized Sheets API client service 
    * @throws IOException 
    */ 
    public static Sheets getSheetsService() throws IOException { 
     Credential credential = authorize(); 
     return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) 
       .setApplicationName(APPLICATION_NAME) 
       .build(); 
    } 
} 

請幫忙。

+0

你能提供MyTestNGClass.java的完整代碼嗎? –

+0

@optimist_creeper我用引發異常的類的代碼更新了帖子,而不是MyTestNGClass.java。希望有所幫助。 –

+0

我不記得確切,但有兩種方法可以導出JAR,一種是依賴項,一種是沒有。如果你已經導出wtihout,你需要在其他類路徑中有其他依賴的Jars。或嘗試導出所有依賴關係。 –

回答

0

我解決了這個問題,通過使用MVN建設我的硒TestNG的Maven項目(而不是使用Eclipse的「導出」選項)的方式如下:

  1. 確保具有可運行代碼的類(即具有類在項目中的src/main/java(而不是src/test/java)目錄中的main方法)。具有main方法的類將如下所示 - https://paste.ofcode.org/xVBWhHB9eEfVpTsqUd9S9n
  2. 在項目的根目錄中創建一個名爲「lib」的目錄,並複製該目錄中的所有相關JAR。有關更多詳細信息,請參閱this
  3. 將此代碼添加到pom.xml文件中 - https://paste.ofcode.org/37gUpGU54siJGDQ6455FWnd
  4. 使用mvn clean compile assembly:single構建JAR。
  5. 使用java -jar myJar.jar運行罐子。請注意,由於我的代碼不支持從JAR內加載資源,因此我將它們放在JAR所在的同一目錄中。

希望這會有所幫助。

1

我認爲NPE來自InputStream in = GoogleSheetsInit.class.getResourceAsStream("/client_secret.json");

首先,檢查您的罐子裏是否有client_secret.json在預期的地方。

然後,您可以嘗試用...getResourceAsStream("client_secret.json");替換該行。

+0

我試圖將我的Maven項目構建到可運行JAR中的方式似乎有些基本錯誤。我將發佈我的解決方案作爲答案。不管怎麼說,還是要謝謝你! –