title: JUnit 5 - JDBC statements testing
body: <p>I've begun learning unit tests. I'm working with JUnit 5 and I'd like to test my method that inserts some data into my database (using JDBC). Here's my code:</p>
  
  <p><strong>Datasource.java</strong></p>
  
  <pre><code>import java.sql.*;
  
  public class Datasource {
  
      public static final String CONNECTION = "jdbc:mysql://127.0.0.1:3306/java";
  
      private Connection connection;
  
      public boolean open() {
          try {
              connection = DriverManager.getConnection(CONNECTION, "root", "");
              return true;
  
          } catch (SQLException e) {
              System.out.println(e.getMessage());
              return false;
          }
      }
  
      public boolean insertTable() {
          try {
              String query = "INSERT INTO artists(name) VALUES(?)";
              PreparedStatement stmt = connection.prepareStatement(query);
              stmt.setString(1, "Test");
              int result = stmt.executeUpdate();
  
              if (result == 1) return true;
              return false;
  
          } catch (SQLException e) {
              System.out.println(e.getMessage());
              return false;
          }
      }
  
  }
  </code></pre>
  
  <p><strong>DatasourceTest.java</strong></p>
  
  <pre><code>import static org.junit.jupiter.api.Assertions.*;
  
  class DatasourceTest {
  
      private Datasource datasource;
  
      @org.junit.jupiter.api.BeforeEach
      void setUp() {
          datasource = new Datasource();
  
          if (!datasource.open()) {
              System.exit(-1);
          }
      }
  
      @org.junit.jupiter.api.Test
      void insertTable() {
  
          assertTrue(datasource.insertTable());
  
      }
  
  }
  </code></pre>
  
  <p>It works fine but it actually inserts a record into my database, but I what I want to do is to simulate it. Is it possible to achieve that using only JUnit? If not, what do I need? And a simple implementation would be highly appreciated.</p>
  
  <p>EDIT</p>
  
  <p>I found out about a tool called <strong>Mockito</strong>, is it what I need? If so, could anyone show me how to deploy a simple test of my method insertTable()?</p>