0

I am new to JUnit and this is a sample JUnit testing implementation. When I run the test cases I am getting a Null Pointer Exception at the below-mentioned line. I debugged the code and noticed that values passed through the test case are accessible inside the controller function and it's printing the values as expected but still, it shows Null Pointer exception. Can someone tell me what is wrong with the implementation?

Dtos

class A{
    
         B b;
         
         //getter & setter
    
    }
    
    class B extends C{
    
         int first;
         
         //getter & setter
    
    }
    
    class C{
    
         int second;
         
         //getter & setter
    
    }

Controller

@Controller
class MyController{

    @Autowired
    MyService service;   

   void checkIt( A obj ){
       sysout ( obj ); // values are printed 
       service.validate( obj.getB() ) //Null pointer Exception at this line
    }
}

Service

@Service
class MyService {

   protected void validate(B obj){
      sysout( obj );
    }
}

Testing

Class ValidateTest{

   private A testValues(){

      A obj=new A();
    
      B b=new B();
      b.setFirst(1);
      b.setSecond(2);

      obj.setB(b);
      return obj;
   }

   @Test
   void testIt(){
      A obj = testValues();
      MyController controller=new MyController();
      controller.checkIt(obj);    

   }
}

0 Answers0