JUnit 4.11 get test result in @After

后端 未结 5 1267
梦毁少年i
梦毁少年i 2020-12-19 07:09

Is there any way I can get the test result in the teardown (@After) method? I\'d like to do clean up after the tests depending on the result.

Could not

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 07:22

    Why not set the result of a test in a class member and then act on it in the @After method?

    public enum TestResult {
        ...
    }
    
    public class TestClass {
    
        private TestResult result;
         ...
        @Test
        public void aTest() {
        // set up test
        // call class under test
        // assert something and set result based upon outcome
        this.result = ...; 
        }
        ...
       @After
        public void teardown() {
         // clean up based upon this.result
        }
    }
    

    I suspect you would not have too many different results and a finite set will suffice.

提交回复
热议问题