How to unit test abstract classes: extend with stubs?

后端 未结 14 1730
有刺的猬
有刺的猬 2020-11-27 08:37

I was wondering how to unit test abstract classes, and classes that extend abstract classes.

Should I test the abstract class by extending it, stubbing out the abstr

14条回答
  •  情深已故
    2020-11-27 09:35

    First if abstract class contained some concrete method i think you should do this considered this example

     public abstract class A 
    
     {
    
        public boolean method 1
        {
            // concrete method which we have to test.
    
        }
    
    
     }
    
    
     class B extends class A
    
     {
    
          @override
          public boolean method 1
          {
            // override same method as above.
    
          }
    
    
     } 
    
    
      class Test_A 
    
      {
    
        private static B b;  // reference object of the class B
    
        @Before
        public void init()
    
          {
    
          b = new B ();    
    
          }
    
         @Test
         public void Test_method 1
    
           {
    
           b.method 1; // use some assertion statements.
    
           }
    
       }
    

提交回复
热议问题