Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在AWS Lambda上运行JUnit测试:java.lang.Exception:无可运行方法_Java_Amazon Web Services_Junit_Aws Lambda - Fatal编程技术网

在AWS Lambda上运行JUnit测试:java.lang.Exception:无可运行方法

在AWS Lambda上运行JUnit测试:java.lang.Exception:无可运行方法,java,amazon-web-services,junit,aws-lambda,Java,Amazon Web Services,Junit,Aws Lambda,我已经编写了代码来下载一个测试用例,并在Java8环境下的AWSLambda中运行它 private static Class<?> loadClass(String className) throws ClassNotFoundException, MalformedURLException { // Load compiled class. File root = new File("..."); URLClassLoader classLoader =

我已经编写了代码来下载一个测试用例,并在Java8环境下的AWSLambda中运行它

private static Class<?> loadClass(String className) throws ClassNotFoundException, MalformedURLException {
    // Load compiled class.
    File root = new File("...");
    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{root.toURI().toURL()});
    return Class.forName(className, true, classLoader);
}

public static String run(RunRequest req) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos, true, "UTF-8");

    try {
        // Download test case
        CloudStorage.downloadChallenge(req.getChallengeId());
        // Get test case class
        String className = Database.getChallengeTestClass(req.getChallengeId());
        // Load test case
        Class<?> test = loadClass(className);

        System.out.println("Test: " + test.getName());

        // Run test case
        JUnitCore junit = new JUnitCore();
        junit.addListener(new TextListener(ps));

        junit.run(test);
    } catch (Exception e) {
        e.printStackTrace(ps);
    } finally {
        return new String(baos.toByteArray(), StandardCharsets.UTF_8);
    }
}
但是,当我在Lambda上运行相同的代码时,会产生以下错误:

Time: 0.014
There was 1 failure:
1) initializationError(com.test.util.UnitTest)
java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    ...

FAILURES!!!
Tests run: 1,  Failures: 1

在打印
com.test.util.UnitTest
时,该类被正确导入。我解决了问题

发生的情况是,我正在使用静态变量
userCode
引用另一个类
Runner
,但我正在使用新的类加载器加载这个类。对于这个类加载器,类
Runner
不存在,因此Java没有加载引用这个静态变量的方法。这与兰姆达无关。我从中了解到了这一点,从而解释了Java类加载器的一些行为

我唯一的问题是为什么它在我的机器上本地工作?我无法解释这里的矛盾


谢谢

我解决了这个问题

发生的情况是,我正在使用静态变量
userCode
引用另一个类
Runner
,但我正在使用新的类加载器加载这个类。对于这个类加载器,类
Runner
不存在,因此Java没有加载引用这个静态变量的方法。这与兰姆达无关。我从中了解到了这一点,从而解释了Java类加载器的一些行为

我唯一的问题是为什么它在我的机器上本地工作?我无法解释这里的矛盾


谢谢

通常,您会创建一个RequestHandler:对于AWS Lambda设置,告诉Lambda要执行什么。我在另一个文件中完成了此操作,为简单起见,我只展示了与问题相关的内容。我验证了在使用
sam local
命令时发生了相同的问题。似乎是lambda Docker图像导致了这种情况。我尝试切换到JUnit 5,但得到了相同的结果。找到了测试容器,但没有找到/运行其中的测试。通常,您会创建一个RequestHandler:对于AWS Lambda设置,告诉Lambda要执行什么。我在另一个文件中完成了此操作,为简单起见,我仅展示与问题相关的内容。我验证了在使用
sam local
命令时发生的相同问题。似乎是lambda Docker图像导致了这种情况。我尝试切换到JUnit 5,但得到了相同的结果。找到测试容器,但未找到/运行其中的测试。
Time: 0.014
There was 1 failure:
1) initializationError(com.test.util.UnitTest)
java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    ...

FAILURES!!!
Tests run: 1,  Failures: 1
package com.test.util;

import junit.framework.TestCase;

public class UnitTest {
  public UnitTest() {}

  @org.junit.Test
  public void test() {
    System.out.println("Runnning test...");

    TestCase.assertTrue(Runner.userCode.toString().equals("Hello World!"));
  }
}