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 7959929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:38:56+00:00 2026-06-04T04:38:56+00:00

I want to write a test case for a case class , that has

  • 0

I want to write a test case for a case class, that has a toXML method.

import java.net.URI

case class Person(
  label: String = "author",
  name: String,
  email: Option[String] = None,
  uri: Option[URI] = None) {

  // author must be either "author" or "contributor"
  assert(label == "author" ||
    label == "contributor")

  def toXML = {
    val res =
      <author>
        <name>{ name }</name>
        {
          email match {
            case Some(email) => <email>{ email }</email>
            case None => Null
          }
        }
        {
          uri match {
            case Some(uri) => <uri>{ uri }</uri>
            case None => Null
          }
        }
      </author>

    label match {
      case "author" => res
      case _ => res.copy(label = label) // rename element
    }
  }
}

Now, I want to assert, that the output is correct. Therefor I use scala.xml.Utility.trim

import scala.xml.Utility.trim

val p1 = Person("author", "John Doe", Some("jd@example.com"),
  Some(new URI("http://example.com/john")))
val p2 =
  <author>
    <name>John Doe</name>
    <email>jd@example.com</name>
    <uri>http://example.com/john</uri>
  </author>

assert(trim(p1.toXML) == trim(p2))

But this will cause an assertion error. If I try to assert equality by comparing the String representations

assert(trim(p1.toXML).toString == trim(p2).toString)

there’s no assertion error.

What am I doing wrong?

  • 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-04T04:38:57+00:00Added an answer on June 4, 2026 at 4:38 am

    First of all there’s something wrong with your code: you have uri: Option[URI] (which I’m assuming is java.net.URI) as a constructor argument, but then you call the constructor with an Option[String].

    This is also the source of your problem:

    scala> val uri = "http://stackoverflow.com/q/10676812/334519"
    uri: java.lang.String = http://stackoverflow.com/q/10676812/334519
    
    scala> <u>{new java.net.URI(uri)}</u> == <u>{uri}</u>
    res0: Boolean = false
    
    scala> <u>{new java.net.URI(uri)}</u>.toString == <u>{uri}</u>.toString
    res1: Boolean = true
    

    What’s happening is that the element’s child is an Atom, which has a type parameter for the data value it carries. In your p1 this is a URI (assuming we’ve corrected the test so that it matches the constructor), and in p2 it’s a String.

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

Sidebar

Related Questions

Give a base class Base , I want to write a method Test, like
I want to write a test that calls a remote server and validates the
I want to write a unit test for a Django manage.py command that does
I want to write an integration test for a library that handles streamed http
Using Nunit, I want to be able to write a test fixture that will
I want to write a C++ class that offers set operations that work on
I am trying to write code to test a method by mocking the class
I want to write some test cases for my web app, but stuck at
I want to test a Ajax based web application. I want to write the
i just join phonegap, i want a built test application. i write some code

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.