Running Groovy test cases with JUnit 5

后端 未结 1 1642
有刺的猬
有刺的猬 2021-01-14 03:59

Maybe this is very simple, but I couldn\'t find any examples on the web:

I\'d like to use JUnit 5 to run a unit test implemented as a Groovy class. My current setup

相关标签:
1条回答
  • 2021-01-14 04:38

    JUnit requires all testing method to use return type void. Groovy's def keyword is compiled to an Object type, so your method compiles to something like this in Java:

    import org.junit.jupiter.api.Test
    
    public class UnitTest {
    
        @Test
        Object shouldDoStuff() {
            throw new RuntimeException();
        }
    }
    

    If you try this out as a Java test, it won't find the test case neither. The solution is very simple - replace def with void and your Groovy test case will be executed correctly.


    src/test/groovy/UnitTest.groovy

    import org.junit.jupiter.api.Test
    
    class UnitTest {
    
        @Test
        void shouldDoStuff() {
            throw new RuntimeException()
        }
    }
    

    Demo:

    0 讨论(0)
提交回复
热议问题