0

Here is what I have so far:

def "If there are results then return true otherwise return false"() {
    given:
    ResultSet resultSet = Mock()

    expect:
    resultSet.next()
}

I am trying to test a boolean method checkIfRowExists(int id, int foreignKey) in class CheckActuateProjectSetServiceImpl. It returns true if row exists and false otherwise.

How would I go about solving this?

public boolean checkIfRowExists(int id, int foreignKey){
    Resultset resultSet = checkIfRowExistsResultSet(id, foreignKey)

    return false;
}

The above method doesn't have the correct implementation as of yet because I'm trying to write the test first before I implement the solution.

Thanks

ShadyBears
  • 3,955
  • 13
  • 44
  • 66
  • Think we would need to know what `CheckActuateProjectSetServiceImpl` looked like... How does the method have a resultset? There doesn't seem to be anything passed in the method definition – tim_yates Jan 05 '19 at 08:46
  • It calls a private method named checkIfRowExistsResultSet(int id, int foreignkey) I'll update... thanks for your input – ShadyBears Jan 05 '19 at 08:55

1 Answers1

2

If I were on your place I would do the next TDD steps.

  1. Start with a test for one test case only:

    def "If there not are results then return false"() {
        given:
        def service = new CheckActuateProjectSetServiceImpl()
    
        expect:
        !service.checkIfRowExists(1, 2)
    }
    
  2. Implement the method to satisfy the test:

    boolean checkIfRowExists(int id, int foreignKey) {
        return false;
    }
    
  3. Add new test for case when there are some result:

    def "If there are results then return true"() {
        given:
        def service = new CheckActuateProjectSetServiceImpl()
    
        expect:
        service.checkIfRowExists(1, 2)
    }
    
  4. Now we are forced to implement our method. The method will do a DB query and check the actual result set for emptiness. Because the DB query is out of unit test scope we extract it to a separate method and will override it in the test later:

    boolean checkIfRowExists(int id, int foreignKey) throws SQLException {
        ResultSet resultSet = getResultSet(id, foreignKey);
        return resultSet.next();
    }
    
    ResultSet getResultSet(int id, int foreignKey) {
        return null; // TODO should be implemented
    }
    
  5. Now our test is failed with NullPointerException because getResultSet() returns null. Lets return a mocked ResultSet which returns true on next() call:

    def "If there are results then return true"() {
        given:
        ResultSet mockedResultSet = Mock(ResultSet)
        mockedResultSet.next() >> true
    
        def service = new CheckActuateProjectSetServiceImpl() {
            @Override
            def ResultSet getResultSet(int id, int foreignKey) {
                return mockedResultSet;
            }
        }
    
        expect:
        service.checkIfRowExists(1, 2)
    }
    

    The test is green now.

  6. The first test is to be fixed as well, mock to return false on next() call:

    def "If there not are results then return false"() {
        given:
        ResultSet mockedResultSet = Mock(ResultSet)
        mockedResultSet.next() >> false
    
        def service = new CheckActuateProjectSetServiceImpl() {
            @Override
            def ResultSet getResultSet(int id, int foreignKey) {
                return mockedResultSet;
            }
        }
    
        expect:
        !service.checkIfRowExists(1, 2)
    }
    

Hope it will help. These steps are just an orientation to move forward in TDD style. Sure things your reality is different and may require something more specific that I proposed above.

Dmytro Maslenko
  • 2,247
  • 9
  • 16