title: Hibernate domain object 'not null' field - mock test should throw error if field is null within object
body: <p>There is an Hibernate domain object that have 'not null' field which have annotation <code>@Column(nullable=false)</code>, a DAO class method which is saving this object in DB.</p>
  
  <p>I am mocking the create DAO call using <code>PowerMockito</code>, mock call is working fine but if i am passing null for the field, mock test is not throwing error that field is null.</p>
  
  <p>Below is the code, tools/techs (java, spring, hibernate, sqlserver, junits, powermockit). Few code changes omitted related to spring context and hibernate session configurations.</p>
  
  <pre><code>public class Entity{
      private String id;
      @Column(nullable=false)
      private String field;
      //setters and getters goes here
  }
  
  public class HibernateDAO{
      private SessionFactory sessionFactory;
  
      public void create(Entity entity){
          getSession().save(entity);
      }
  
      public void setSessionFactory(SessionFactory sf){
          sessionFactory = sf;
      }
  }
  
  public class HibernateDAOTest{
      HibernateDAO hibernateDAO = new HibernateDAO();
      public SessionFactory mockedSessionFactory; 
      public Session mockedSession; 
      public Query query;
      public SQLQuery sqlQuery;
      public Transaction mockedTransaction;
      @Before
      public void setup() {
          mockedSessionFactory =  PowerMockito.mock(SessionFactory.class);
          mockedSession = PowerMockito.mock(Session.class);
          mockedTransaction = PowerMockito.mock(Transaction.class);
          query = PowerMockito.mock(Query.class);
          sqlQuery = PowerMockito.mock(SQLQuery.class);
  
          PowerMockito.when(mockedSessionFactory.openSession()).thenReturn(mockedSession);
          PowerMockito.when(mockedSessionFactory.getCurrentSession()).thenReturn(mockedSession);
          PowerMockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
      }
  
      @Test
      public void testCreate(){
          Entity entityToSave = new Entity();
          entityToSave.setId("123");
          entityToSave.setField(null);
          hibernateDAO.setSessionFactory(mockedSessionFactory);
          hibernateDAO.create(entityToSave);//this call should throw error that "field" is null but its not.          
      }
  }
  </code></pre>