简体   繁体   中英

Gradle cannot find JUnit tests

In Java, I have the following tests:

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

Running gradle build gives me the following output:

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          ]

It seems Gradle cannot find any tests in any test classes that I have.

It turns out,

Gradle cannot find tests which are static , private or in an abstract class.

The following method access modifiers are valid for a test method :

  • package-private (no modifier)
  • protected
  • public

The following class access modifiers are valid for a test class :

  • package-private (no modifier)
  • public

The following keywords are not valid for tests:

  • abstract
  • static
  • private

Just removing the abstract and static keywords from the tests would fix everything:

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 output:

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          ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM