A test suite worth testing?



  • Found this JUnit test class for some Java application:

     

    public class APITestUtil
    {
        List myAssets;
        Document dom;

        private HttpURLConnection connection;
        private static ResourceBundle bundle = null;

        private static APITestUtil singletonUtil = null;

        public static APITestUtil getInstance()  throws IOException {
            if (null == singletonUtil) {
                singletonUtil = new APITestUtil();
            }
            return singletonUtil;
        }

        public APITestUtil() throws IOException
        {

            myAssets = new ArrayList();

            if(bundle == null) bundle = ResourceBundle.getBundle("TEST_PROPS_FILE");
            String uriStr = bundle.getString("API_URI");

            URL url = new URL(uriStr);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
        }

        public static boolean isNumeric(String str, Class<? extends Number> clazz)
        {
            try
            {
                if (clazz.equals(Byte.class)) {
                    Byte.parseByte(str);
                }
                else if (clazz.equals(Double.class)) {
                    Double.parseDouble(str);
                }
                else if (clazz.equals(Float.class))  {
                    Float.parseFloat(str);
                }
                else if (clazz.equals(Integer.class)) {
                    Integer.parseInt(str);
                }
                else if (clazz.equals(Long.class)) {
                    Long.parseLong(str);
                }
                else if (clazz.equals(Short.class)) {
                    Short.parseShort(str);
                }
            }
            catch (NumberFormatException nfe)
            {
                return false;
            }

            return true;
        }

        public long getfileSystemRootFolderFromDOM(Document dom,String testfileSystemRootFolder,APITestUtil test)
        {

            int fileSystemRootFolderId=0;
            NodeList nodes = dom.getElementsByTagName("response");
            Element element = (Element) nodes.item(0);

            NodeList rootfolders = element.getElementsByTagName("fileSystemRootFolder");

            for (int j = 0; j < rootfolders.getLength(); j++) {
                Element line = (Element) rootfolders.item(j);

                if(line.getAttribute("name").equals(new String(testfileSystemRootFolder))) {
                    fileSystemRootFolderId = Integer.parseInt(line.getAttribute("id"));
                    assertTrue(test.isNumeric(line.getAttribute("id"),Integer.class));
                }
            }

            return fileSystemRootFolderId;

        }

        ...


    }



  • Okay, so what are the WTFs?  (I have no real experience in Java design, so I usually can't spot anything higher-level than "omg he's using a bubble sort" without having it pointed out.)

     



  • This seems to be a weirdly written test  not to test own code but to test Java's api...

     



  • The APITestUtil is *supposed* to be a singlton class. Notice the singletonUtil member and getInstance method. Now take a look at the constructor :)

    Also in the method getfileSystemRootFolderFromDOM an instance of APITestUtil "test" is passed in. Can anyone remind that guy what is the use of "this" pointer/reference again? And after all what is the instance used for? You guessed it, to invoke a static method isNumeric.

    The isNumeric checks whether the given string is a valid Interger along with other types. But *before* call to that method, an unguarded Integer.parseInt call is made which can throw an exception.

    Finally the getfileSystemRootFolderFromDOM is supposed to check if a root folder for a given folder/directory hierarchy is specified. How many root folders do you think an hierarchy can have, the test case thinks otherwise and goes on merrily in a loop.



  • @nikhil said:

    Also in the method getfileSystemRootFolderFromDOM an instance of APITestUtil "test" is passed in. Can anyone remind that guy what is the use of "this" pointer/reference again? And after all what is the instance used for? You guessed it, to invoke a static method isNumeric.

    Nice.  The thought process must have been... oops, I need to call that static method.  Darn, I wish I could call that it directly without an instance.  Guess I'll need an instance.  Wish I could create it myself, inside this method, too bad that's not possible.  Hey!  I'll just let the caller pass me an instance of this class!  Damn I'm good.

     



  • I'm so confused here.  Is this thing supposed to be a utility class or a test case?  That getFileSystemBlahBlah function makes an assertion AND returns a value?! 

    Clearly some junior programmer (maybe even just the QA guy forced into development) on the loose here.   Looks like its only purpose is to make sure that the ID attribute of some XML node is an integer...but...wouldn't it make more sense to write tests for the thing that generates this node to begin with.  Maybe they're using JUnit to verify the structure of an existing XML document?!  Please tell us more!



  • The guy who has written this has more than five years of experience in Java QA work. He *claims* to have written test suites for entire Java projects, a project he cites is a JDBC driver.

    The class I have mentioned was supposed to be a utility class for an actual test class but somehow the two classes have become one.

    A Java developer had originally  given him some utility and test classes with limited functionality as a starting point for the QA guys. The class you see is a result of *simplification* of the original code. You can imagine the plight of the developer who saw this resulting simplification.



  • What's up with having QA guys write JUnit tests?  I though they were supposed to be "programmer tests;" i.e. written by the programmer.  Seems to me like in order to write good tests, you need to be skilled enough to understand the API and figure out test cases for each method.  If you can do that, doesn't it make you a programmer anyway?
     



  • The only coders I know who use 'clazz' for an instance are indians.

     Am I right.
     


Log in to reply