Test cases in inner classes with JUnit

前端 未结 5 620
故里飘歌
故里飘歌 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:23

    I've had success with Nitor Creation's Nested Runner as well.

    How to use Nitor Creation's Nested Runner

    There is a post explaining it here:

    Add this dependency:

    
        com.nitorcreations
        junit-runners
        1.2
        test
    
    

    And a @RunWith to your test:

    import com.nitorcreations.junit.runners.NestedRunner
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import static org.junit.Assert.*;
      
    @RunWith(NestedRunner.class)
    public class RepositoryUserServiceTest {
               
        public class RegisterNewUserAccount {
         
            public class WhenUserUsesSocialSignIn {
                 
                public class WhenUserAccountIsFoundWithEmailAddress {
                     
                    @Test
                    public void shouldThrowException() {
                         assertTrue(true);
                    }
                }
             
            }
        }
    }
    

    PS: The example code has been taken and modified from the above blog post

提交回复
热议问题