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

  • Home
  • SEARCH
  • 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 8805095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:46:32+00:00 2026-06-14T01:46:32+00:00

I want to unit test an API that parses a file for specific patterns.

  • 0

I want to unit test an API that parses a file for specific patterns.
The fragments of text returned, may be multiline and contain tabs etc. I.e. the text in the file is formatted with new lines and tabs to be readable by a user (same as we nicely indent an xml file).
Problem: Since Java does not offer the option to define such strings (which will be the expected outcome in an Assert check of the API output) how are such problems handled?
I thought e.g. to save all the expected output in a file with some kind of special character to mark beginning and end of each expected fragment but I thought that perhaps there is a standard approach to such a problem.
Is there a better option?

Update:
Example:

This is an example String.    
     This is an inner part of the string.        Another part.           Another also.    
                      This is also an inner part.   
     Now an outer.   This is the outer example       string.
  • 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-14T01:46:33+00:00Added an answer on June 14, 2026 at 1:46 am

    UPDATE: Based on @Code-Guru’s answer (which kudos for him for saying the obvious): You can easily make it so that Eclipse will insert and auto escape your literals correctly if you want to go the inline route: Windows->Preferences->Java->Editor->Typing->Escape text when pasting literals.

    The sorry state of affairs is that Java does not have multiline Strings like Scala or Groovy.

    The best solution I have found is to put a text file in your class path and do a

    getClass().getResourceAsStream("filename");
    //Then convert to string.
    

    That will load a file named filename from the classpath relative to the getClass() class (ie in the same package).

    You can use my gist here: https://gist.github.com/4041855 which uses Guava to make it easier to load files as Strings from the classpath and does some sexy caching.

    If your using Maven or Gradle you will want to put those files in src/test/resources and not src/test/java.

    You could obviously do some parsing of the string (ie XML) if you don’t want to make many files. The other slightly overkill option is to make a readonly SQLite database or even a CSV. I have seen people do that if they have 1000s of different parameters they want to test. If thats your problem you should google JUnit Parameterized tests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method CreateAccount(...) that I want to unit test. Basically it creates
I want to write a unit test for a Django manage.py command that does
HI, I want to have set configuration settings for a unit test project that
I've written a specialized HTML parser, that I want to unit test with a
I want to write a unit test that performs HTTP requests directly (instead of
I have some Web API methods that I want to write unit tests for.
I want to Unit Test my application which use MSMQ but i found no
I want to unit test a RESTful interface written with Apache CXF. I use
I'm writing small XMPP server using boost::asio and I want to unit-test my code.
I want to create a unit test for integration testing. What class should I

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.