Using JUnit @Rule with ScalaTest (e.g. TemporaryFolder)

后端 未结 3 449
温柔的废话
温柔的废话 2020-12-20 12:09

I would like to be able to use JUnit rules such as TemporaryFolder or other TestRules we have already developed in-house. What is the best method

3条回答
  •  别那么骄傲
    2020-12-20 12:26

    You could solve the problem by creating a member field of type TemporaryFolder and returning this field value by the @Rule function.

    class MyTest extends JUnitSuite {
    
      val _temporaryFolder = new TemporaryFolder
    
      @Rule
      def temporaryFolder = _temporaryFolder
    
      @Test
      def test: Unit = {
        assert(temporaryFolder.newFile() !== null)
      }
    }
    

提交回复
热议问题