【问题标题】:JAR - Selenium Java TestNG Maven project's JAR file returns failureJAR - Selenium Java TestNG Maven 项目的 JAR 文件返回失败
【发布时间】:2017-10-11 06:31:16
【问题描述】:

我的 TestNG 项目仅包含一个包含两个 @Test 方法的类,下面是我的 main 类中的 main 方法。

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();
    }

}

问题是运行 main 方法,因为 Eclipse 中的 Java 应用程序运行良好,但是当我使用 Eclipse 中的“导出”选项从我的项目生成 JAR 文件并尝试使用 java -jar MyJar.jar 运行 JAR 文件时它在控制台上返回以下错误:

[TestNG] Running:
  Command line suite


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

下面是 test-output 目录中生成的 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();
    }
}

请帮忙。

【问题讨论】:

  • 能否提供MyTestNGClass.java的完整代码?
  • @optimist_creeper 我已经用引发异常的类的代码更新了帖子,而不是 MyTestNGClass.java。希望对您有所帮助。
  • 我记不太清了,但是有两种导出 JAR 的方法,一种有依赖关系,另一种没有。如果你已经导出了 wtihout,你需要在某个地方的类路径中有其他依赖的罐子。或尝试导出所有依赖项。
  • 是的,但我相信如果我在没有依赖关系的情况下导出它,那么 JAR 应该会给出不同的错误。就我而言,我看到了一个 NPE。

标签: java maven selenium jar testng


【解决方案1】:

我认为 NPE 来自InputStream in = GoogleSheetsInit.class.getResourceAsStream("/client_secret.json");

首先,检查 client_secret.json 是否存在于您的 jar 中的预期位置。

然后,您可以尝试将行替换为...getResourceAsStream("client_secret.json");

【讨论】:

  • 我试图将 Maven 项目构建成可运行的 JAR 的方式似乎基本上有问题。我将发布我的解决方案作为答案。还是谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
相关资源
最近更新 更多