title: Unit Testing(Mocking) Databases, How To Verify Database Methods With Mocking?
body: <p>As a person has no experience before in unit testing and mocking, I followed beginner tutorials about JUnit with Mockito and I do some practice.</p>
  
  <p>Now, I need to unit test some class methods which do basic database operations on a MySQL database. I don't want to make real changes on the database.</p>
  
  <p>In my code:</p>
  
  <pre><code>@InjectMocks private DBConnection dbConnection;
  @Mock private DBConnection mockDBConnection;
  
  @Test
  public void testDropTable() {
  
      Mockito.when(mockDBConnection.dropTable()).thenReturn(0);
  
      // dropTable() returns 0 if table dropped
      int result = dbConnection.dropTable();
      Assert.assertEquals(result, 0);
  }
  </code></pre>
  
  <p>To verify(with assertEquals() ) result of the test, I am calling real dropTable() method, and it really drops table( Actually it is obvious, because I'm calling on real DBConnection instance)</p>
  
  <p>Is there anyway to verify the methods like that, without accessing the real database? Or did I misunderstand the concept of Unit Testing and Mocking?</p>