Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:46:53+00:00 2026-06-17T12:46:53+00:00

I have a class that consume an XML file and produce text output based

  • 0

I have a class that consume an XML file and produce text output based on the input. Both the input and output are rather complex and the output can also include things not in the input: e.g. include timestamps and the results from live data that are not controlled by the input – with other words: the class is not a pure input-output transformation.

I would like to test the resulting text output using JUnit. As the produced text can vary in many different ways based on the input, I would like to be able to match specific parts of the output against some sort of template in each test. Each template should allow for some simple text substitutions and also for ranges in the text that should not be matched.

The question is whether any such frameworks already exists?

One very low-level possibility would be to use some fancy regular expressions to match the text, but I think these will be a bit too limited for our use as you don’t have enough context in regular expressions…

EDIT: Two comments:

  • One of the functions of the class is the ability to do certain simple types of aggregation of data and calculations (e.g. sums) based on the input. This I would like to test, without testing the rest of the generated text output as well.
  • I wish it was possible to make changes to the existing code base, but it is a very large chuck of legacy code that I really don’t want to refactor. So introduction of mock services or testing of smaller pieces will not be possible.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:46:55+00:00Added an answer on June 17, 2026 at 12:46 pm

    I would use Groovy for writing the unit tests, because this is one of Groovy’s strength, see

    • Unit Testing
    • Using JUnit 4 with Groovy
    • Unit Testing – Groovy
    • Spock

    But Groovy is also superb for handling XML, see

    • Prozessing XML
    • Reading XML using Groovy’s XmlSlurper

    A little example for summing up some XML attributes:

    // multiline string, very complex XML content :-)
    def input = '''\
    <list>
        <summand value='13' time='10:40' text='Compare me!'/>
        <summand value='1' />
        <summand value='4' />
        <summand value='2' />
        <summand value='7' />
    </list>'''
    
    // reading XML via XmlSlurper
    def list = new XmlSlurper().parseText(input)
    
    // Prints 13
    println list.summand[0].@value
    
    // collect all summand values, prints 27
    println list.summand.collect { it.@value.toInteger() }.sum()
    

    You can find a good tutorial about testing in the MEAP Making Java Groovy or look at this presentation.

    Groovy has also template support. But with the XML support it is very easy to compare only certain attributes and not the whole tag content to skip some attributes like the timestamp you mentioned. Therefore you don’t need to compare templates. For an example add this source to the skript above:

    // compare the first summand tag, skipping the time attribute
    assert [list.summand[0].@value.toInteger(), list.summand[0].@text] == [13, 'Compare me!']
    

    To learn Groovy, I recommend the Groovy Koans. See also Adding Groovy Tests to a Maven Java Project.

    Update:
    I would not compare the XML against each other, instead I would unit test single values as desribed in my answer. But if you go the complete way I would use the following approach:

    • get the XML from your business logic
    • remove temporary (not comparable values with Groovy)
    • generate a template from GStringTemplateEngine (see link above) without the not comparable values
    • compare both XMLs via http://xmlunit.sourceforge.net/
      You can find an example on Updating XML with XmlSlurper the end of the page.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that I instantiate to save or load xml data. For
I need to have a Web service that can take any arbitrary xml string.
I have a class library which searches for an XML file in the working
So I have this service class that utilize Restkit to consume REST web services.
I have a SimpleProducerConsumer class that illustrates a consumer/producer problem (I am not sure
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class that represents users. Users are divided into two groups with different
I have class grades that I need to check if a specific grade is
I have class A such that: class A { static int i; A(); f1();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.