【问题标题】:No main method error when using @Test [duplicate]使用@Test时没有主要方法错误[重复]
【发布时间】:2019-02-01 18:52:57
【问题描述】:
import org.junit.Test;
import static org.junit.Assert.*;

public class TestPalindrome {
    // You must use this palindrome, and not instantiate
    // new Palindromes, or the autograder might be upset.
    static Palindrome palindrome = new Palindrome();

    @Test
    public void testWordToDeque() {
        Deque d = palindrome.wordToDeque("persiflage");
        String actual = "";
        for (int i = 0; i < "persiflage".length(); i++) {
            actual += d.removeFirst();
        }
        assertEquals("persiflage", actual);
    } 
}

public class Palindrome{

    public Deque<Character> wordToDeque(String word){
        ArrayDeque<Character> c = new ArrayDeque(); 
        for (char d : word.toCharArray()){
            c.addLast(d);
        }

        return c; 
    }
}

从技术上讲,wordToDeque 方法可以包含任何内容(例如返回 null),但它仍然只是给我“未找到主要错误”。这有点令人困惑。编译器识别 junit,因为我已将它添加到我的环境变量中。有谁知道为什么会这样?

【问题讨论】:

  • 它仍然只是给我“没有发现主要错误”。你什么时候做什么?
  • 您应该在您的 ide 中使用“Run as Test”或等效项来运行 TestPalindrome。运行测试不像运行可执行类。
  • @spi 你知道如何在 git bash 中作为命令执行此操作吗?我没有使用 ide。
  • 查看链接的副本

标签: java junit


【解决方案1】:
  1. 如果您使用的是 maven/gradle 等构建系统,则运行 test 目标应该会运行您的测试。
  2. 如果您使用javac 进行编译,您可以使用JUnit 控制台启动器。有关详细信息,请参阅JUnit site

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2018-08-02
    • 1970-01-01
    • 2015-04-20
    • 2013-06-25
    相关资源
    最近更新 更多