2

I used to run my unit tests using Android Unit Test but switched to Java Unit Test using Robolectric. I noticed a difference in using the SaxParser DefaultHandler. Basically, the Android sax parser would populate "localName" but when run under Robolectric using the Java VM localName is empty, and qname seems to contain the value I should use.

Here is the method I am refering too:

import org.xml.sax.helpers.DefaultHandler;

public abstract class BaseParser extends DefaultHandler
{   
    @Override
    public abstract void startElement(String uri, String localName, String qName, Attributes attributes);
}

Anyone else notice this strange occurence or can fix it?

GOLDEE
  • 2,318
  • 3
  • 25
  • 49
SpecialEd
  • 473
  • 5
  • 17

1 Answers1

3

When parsing your xml, try setting the SAXParserFactory to be namespace aware.

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);

Adding this flag worked well for me when running the unit tests with Robolectric.

donkey
  • 204
  • 1
  • 7
  • Wow, just want to add the following error message, so others can find the solution easier (took me a while, to find that): android.sax.BadXmlException; lineNumber: xx; columnNumber: xx; Line xx: Root element name does not match. Expected: '...namespace...:...rootTag...', Got: '' – user2808624 Feb 05 '21 at 16:41