Skip to content

org.junit.rules.Timeout does skip @After execution #1231

@arcuri82

Description

@arcuri82

Hi,
I noticed that in my tests the @after methods were not executed in the case of test timeouts. However, that only happens when the Timeout rule is used. For example, the following test is fine (in the sense After method is called as expected):

public class CheckTimeoutProblem {
    @Before
    public void before(){ System.out.println("Called @Before");}

    @After
    public void after(){System.out.println("Called @After");}

    @Test(timeout = 10)
    public void doFailWithTimeout(){
        while(true){ int x = 0; x++;}
    }

    @Test
    public void emptyTest(){}
}

When running this, I get:

Called @before
Called @after
Called @before
Called @after

which is as expected. However, if I use Timeout Rule like:

public class CheckTimeoutProblem {
    @Rule
    public org.junit.rules.Timeout globalTimeout = new org.junit.rules.Timeout(200);

    @Before
    public void before(){ System.out.println("Called @Before");}

    @After
    public void after(){System.out.println("Called @After");}

    @Test
    public void doFailWithTimeout(){
        while(true){ int x = 0; x++;}
    }

   @Test
    public void emptyTest(){}
}

Called @before
Called @after
Called @before

where the After of the timed out test is not executed :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions