Pregunta

I have list returning with Name and Value, I am interested in creating a Junit test by checking whether the return list is alphabetized or not. So far i have done this but i get result always = TRUE as i believe it's taking "MINTS...... " into consideration. So my goal would be to remove MINTS ( Name ) from the list and check the list ( VALUE ) for order.

list from table

enter image description here

Junit code

public void test() throws Exception
    {
        DataObj dao = new DataObj();

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put( "code", 100 );

        List<?> values = dao.getDataObj( params, conn );

        String previous = "";
        boolean result = false;

        for ( final Object vCurrent : values )
        {

            String current = vCurrent.toString();
            if ( current.compareTo( previous ) < 0 )
            {
                result = false;
                previous = current;

            }
            result = true;
        }


    }

if i do a print on values i would get something like " mints Dave mints Jackie mints Derek....."

¿Fue útil?

Solución 2

If this is supposed to be a JUnit test, then you should be using the JUnit "assertXxxx" methods to do the low-level tests; e.g. something like this

    import static org.junit.Assert.*;

    ....

    @Test
    public void testOrdered() {
        DataObj dao = new DataObj();

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put( "code", 100 );

        List<?> values = dao.getDataObj( params, conn );

        String previous = "";
        for (final Object vCurrent : values) {
            String current = vCurrent.toString();
            assertTrue("list not ordered correctly",  /* this arg is optional */
                       current.compareTo(previous) < 0);
            previous = current;
        }
    }

This entirely removes the need to produce a "result".

Otros consejos

You're always setting result = true at the end of each iteration of the loop.

You could do:

    String previous = "";
    boolean result = true;

    for ( final Object vCurrent : values )
    {

        String current = vCurrent.getValue();
        if ( current.compareTo( previous ) < 0 )
        {
            result = false;
            break;
        }
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top