0

// Trying to write a junit test for this. It's probably really obvious but i cannot think of a way to achieve it.

public void connectToDB() {
    conn = new QBasicConnection(host, port, user, password);
    try {
      conn.open();
    } catch (IOException e) {
      throw new IllegalStateException("", e);
    } catch (QException e) {
      throw new IllegalStateException("", e);
    }
  }
Masum
  • 1,037
  • 15
  • 32

1 Answers1

3

I would suggest you to read this post first: What is unit testing and how do you do it?

In your test-case you don´t want to open a real connection to the database. This is not a part of your programm but part of the framework you are using to access the database functions.

A good way to avoid this is to use mocks. What is Mocking? gives you a good overview about mocks and why you should use them and not rely on the whole dependency of the DB connection.

In fact, the only test you could write, would check your exception handling. In the test you would check that the exception, thrown by conn.open() is handled correctly.

WUUUGI
  • 316
  • 2
  • 9