0

I have a DAO class called PersonDAO and I am using it to get info about People from Database. I want to write unit test to check the database connection. This is what I have so far

@Test( expected= SQLException.class)
public void testDatabaseConnection()
    throws Exception {



}

Also, how do I write unit test for findAll() method? This is what I have if I am storing people info in a map. But, I would like to know what changes if I have a database instead of a map

@Test
public void testFindAll()
    throws Exception {

    Map< Integer, PersonDTO > people = new LinkedHashMap<>();
    people = PersonDAO.findAll();

    assertEquals( PersonDTO.getTotalDept(), people.size() );
}
7
  • unit test is not a different language or method. You test for a database connection the same way you establish a connection in your main program. Commented Jun 9, 2015 at 18:24
  • to test findAll first test how many values you have currently in database, then add one value...then count again. The test must be previousCount + 1 = currentCount Commented Jun 9, 2015 at 18:25
  • But I am not sure about the syntax for testing database connection. I am assuming we need to use Connection class. Could you show me the syntax? Commented Jun 9, 2015 at 18:27
  • okay i get your doubt. There is no SQL statement to check if connection is established or not. Simply try and fetch a record. If there is no connection, Error will be thrown Commented Jun 9, 2015 at 18:29
  • Do I need to write SQL queries for that? Ex: get all person using "SELECT *"? Commented Jun 9, 2015 at 18:31

2 Answers 2

3

Basically, you are talking about integration tests if you want to test your DAO on "live" database.

PersonDTO personDto;
Connection connection;

@Before
public void setUp() {
    personDto = new PersonDTO();
    connection = ConnectionUtil.someMethodThatReturnsConnection();
}

@Test
public void testIfConnectionNotNull() {
    assertNotNull(connection);
}

@Test
public void testIfDAONotNull() {
    assertNotNull(personDto);
}

@Test
public void testFindAll() {
    // Let's presume you have 4 records
    assertEquals(4, personDto.findAll().size());
}

In case you want to stay on JUnit, Mockito, EasyMock... are an answer.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you show me how I write test case for checking database connection?
Just call assertNotNull and pass connection.
Can I also check if I can access the database by using a query? For example, I try to retrieve all the data from database using "SELECT * FROM PersonDatabase" and if that operation is not successful, there is an issue with the connection. If yes, how do I actaully write the code for that?
@user3528213 If that operation is not successful, it doesn't mean it's an issue with connection. If connection is successful, then you can access database (testIfConnectionNotNull method does that). If your query doesn't return expected values, then it's an issue with that query. Not with connection.
Suppose if a method has something like this: try { // db call } catch ( Exception e ) { return false } what would be a good test case to check this method?
|
0

Take a look at this question to answer your first question - how to create a database connection. JUNIT test case for connection with database

To answer your second question, on how to test "findAll", you need to ask yourself what does this method does. E.g. it brings all user records, then first check how many users are there in your table and store that in a variable. Then insert another user and again make a call to findAll and count how many rows are returned. Then test if you have previousCount + 1 == currentCount

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.