Skip to content

masooh/intellij-junit-to-spock-converter

Repository files navigation

JUnit to Spock Converter

GitHub Actions Build Pipeline
Build Status
Lines of Code

JUnit to Spock Converter is a plug-in for IntelliJ IDEA: https://plugins.jetbrains.com/plugin/12335-groovyfier.

It’s inspired by junit2spock and has the same goal: Facilitate the transition from JUnit to Spock. The advantage compared to the command line tool is the integration in the IDE. The project is in an early stage and has at the moment only a subset of the features of junit2spock.

After converting your JUnit test to a spock specification it probably will not be 100% correct, but it will save you a lot of stupid conversion work.

Features

The following can be replaced:

Table 1. test method → feature method

JUnit 4 / 5

Spock

@Test
public void assertOnly() {
    assertNotNull(book);
}
def "assert only"() {
    expect:
    book != null
}
Table 2. Replace Setup/Cleanup annotations

JUnit 4 / 5

Spock

@BeforeClass / @BeforeAll
public static void beforeClass() { }

@AfterClass / @AfterAll
public static void afterClass() { }

@Before / @BeforeEach
public void setUp() { }

@After / @AfterEach
public void tearDown() { }
def setupSpec() { }

def cleanupSpec() { }

def setup() { }

def cleanup() { }
Table 3. Replace assertions, messages convert to comments

JUnit 4 / 5

Spock

assertNotNull(book);
assertNull(book.getPages());
assertEquals((Integer) 33, book.getPages());
assertTrue(book.getPages() > 0);
assertFalse(book.getPages() < 0);
assertNotNull("book is present", book);
book != null
book.pages == null
book.pages == (Integer) 33
book.pages > 0
!(book.pages < 0)
book != null // "book is present"
Table 4. Expected exceptions → thrown

JUnit 4 (only)

Spock

@Test(expected = IllegalArgumentException.class)
public void expectArgumentException() {
    book.setPages(-1);
}
def "expect argument exception"() {
    when:
    book.pages = -1
    then:
    thrown(IllegalArgumentException)
}

Limitations

The following is not (yet) possible:

Roadmap

  • Documentation

    • Commit converted test to show what works and what not: BookTest

    • Plugin description with small video like IntelliJ new features

  • Quality

    • Unit Tests for plugin itself

  • Separate code, so that it can be easily extended with new features and it’s easier to provide pull requests.

  • Framework support

    • Mockito

    • Wicket

    • Hamcrest

  • Spock features

    • create data driven tests if test contains only similar assertions