Gradle找不到JUnit测试

时间:2017-10-20 19:50:59

标签: java unit-testing gradle junit junit5

在Java中,我有以下测试:

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

abstract class MyClassTest {
    @Test
    static void testTwoPlusTwoIsFour() {
        assertEquals(4, 2 + 2);
    }

    @Test
    static void testMinusOneThatsThree() {
        assertEquals(3, -1);
    }
}

运行gradle build会给我以下输出:

Test run finished after 95 ms
        [         1 containers found      ]
        [         0 containers skipped    ]
        [         1 containers started    ]
        [         0 containers aborted    ]
        [         1 containers successful ]
        [         0 containers failed     ]
        [         0 tests found           ]
        [         0 tests skipped         ]
        [         0 tests started         ]
        [         0 tests aborted         ]
        [         0 tests successful      ]
        [         0 tests failed          ]

Gradle似乎无法在我拥有的任何测试类中找到任何测试。

1 个答案:

答案 0 :(得分:2)

事实证明,

Gradle无法找到staticprivateabstract类的测试。

以下方法访问修饰符对测试方法有效:

  • package-private(无修饰符)
  • protected
  • public

以下访问修饰符对测试类有效:

  • package-private(无修饰符)
  • public

以下关键字对测试有效:

  • abstract
  • static
  • private

只需从测试中移除abstractstatic关键字即可解决所有问题:

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class MyClassTest {
    @Test
    void testTwoPlusTwoIsFour() {
        assertEquals(4, 2 + 2);
    }

    @Test
    void testMinusOneThatsThree() {
        assertEquals(3, -1);
    }
}

Gradle输出:

Test run finished after 158 ms
        [         2 containers found      ]
        [         0 containers skipped    ]
        [         2 containers started    ]
        [         0 containers aborted    ]
        [         2 containers successful ]
        [         0 containers failed     ]
        [         2 tests found           ]
        [         0 tests skipped         ]
        [         2 tests started         ]
        [         0 tests aborted         ]
        [         1 tests successful      ]
        [         1 tests failed          ]