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.
The following can be replaced:
JUnit 4 / 5 |
Spock |
@Test
public void assertOnly() {
assertNotNull(book);
} |
def "assert only"() {
expect:
book != null
} |
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() { } |
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" |
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)
} |
The following is not (yet) possible:
-
Spock is not added by the plugin to your build system (
pom.xml
orbuild.gradle
). Have a look at spock-example/pom.xml at spock-1.x · spockframework/spock-example or the more minimalistic version in this test project: java-groovy-sample/pom.xml. -
Junit 5:
assertThrows
-
Java lambdas are not converted to Groovy closures
-
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
-