Test cases in inner classes with JUnit

前端 未结 5 622
故里飘歌
故里飘歌 2020-12-02 12:09

I read about Structuring Unit Tests with having a test class per class and an inner class per method. Figured that seemed like a handy way to organize the tests, so I tried

5条回答
  •  执念已碎
    2020-12-02 12:43

    You should annontate your class with @RunWith(Enclosed.class), and like others said, declare the inner classes as static:

    @RunWith(Enclosed.class)
    public class DogTests
      {
      public static class BarkTests
      {
        @Test
        public void quietBark_IsAtLeastAudible() { }
    
        @Test
        public void loudBark_ScaresAveragePerson() { }
      }
    
      public static class EatTests
      {
        @Test
        public void normalFood_IsEaten() { }
    
        @Test
        public void badFood_ThrowsFit() { }
      }
    }
    

提交回复
热议问题