mercredi 15 mars 2017

Dependent functional JUnit tests can't share static fields

I have the following test (simplified for this question):

@FixMethodOrder(MethodSorters.JVM)
public class ArticleTest {

    private static Article article;

    @Test
    public void testCreateArticle() {
        articleService.create("My article");
        article = articleService.findByTitle("My article");
        assertNotNull(article);
    }

    @Test
    public void testUpdateArticle() {
        article.setTitle("New title");
        articleService.save(article);
        assertNull(articleService.findByTitle("My article"));
        article = articleService.findByTitle("New title");
        assertNotNull(article);
    }

}

testCreateArticle passes successfully, but testUpdateArticle fails at the first line because article is null, throwing thus an NPE (although the first test asserted that article wasn't null).

Anyone understands why? Note that I run the test with Play Framework (which loves bytecode manipulations), so this may be related somehow...

Also, I know that having dependent tests is a bad practice, but IRL, this isn't a unit test but a kind of test scenario, so I just wanted to give dependent tests a try to understand by myself why people don't like them ;)

But anyway, static fields are supposed to be shared between tests, am I wrong?

Aucun commentaire:

Enregistrer un commentaire