简体   繁体   中英

junit test case for my api

i am new to writing junits.I have my below java api which gets a unique value every time from database.It contains just a single query.I need to write junit for below api.can anybody give some suggestions how should i approach??

public static int getUniqueDBCSequence() throws Exception
    {

        int w_seq = 0;
        QueryData w_ps = null;
        ResultSet w_rs = null;

        try
        {
            w_ps = new QueryData("SELECT GETUNIQUENUMBER.NEXTVAL FROM DUAL");
            w_rs = SQLService.executeQuery(w_ps);

            while ( w_rs.next() )
            {
                w_seq = w_rs.getInt(1);
            }

        }
        catch (Exception a_ex)
        {
            LOGGER.fatal("Error occured : " + a_ex.getMessage());
        }
        finally
        {
            SQLService.closeResultSet(w_rs);
        }
        return w_seq;
    }

You are using only static methods : in the class under test but also in the dependencies of it.
It is really not a testable code with JUnit.

Besides, what you do you want to test unitary ?
Your test has no substantive logic.

You could make SQLService.executeQuery() a method instance to be able to mock it. But really which interest to mock it ?
To assert that the result is returned w_seq = w_rs.getInt(1); ?
It looks like technical assertions that have few value and maintaining unit tests with few value should be avoided.

Now, you could test with DBunit or tools to populate a in memory database and executes the code against.
But the query executed have a strong coupling with Oracle sequences.
So, you could have some difficulties to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM