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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:02:52+00:00 2026-06-01T07:02:52+00:00

I’m trying to test a model method using Play Framework 2.0 and Specs2. Global.scala

  • 0

I’m trying to test a model method using Play Framework 2.0 and Specs2. Global.scala populates the DB on first run with data. In one test, I can successfully test it using code like below:

def dateHelper(str: String): Date = new SimpleDateFormat("MM/dd/yyyy").parse(str)

"Food model" should {
  "be retrieved by id" in {
    val Some(mashedPotatoes) = Food.findById(1000)

    mashedPotatoes.name must equalTo("Mashed Potatoes")
    mashedPotatoes.eaten must equalTo(false)
    mashedPotatoes.id must equalTo(Id(1000))
    mashedPotatoes.owner must equalTo(Id(1))
    mashedPotatoes.expiry must equalTo(dateHelper("05/21/2012"))
  }
}

That test passes. But if I try to select more than one item from the model, and test it as a list:

"return food for test user in " in {

  running(FakeApplication()) {
    val testFoods: Seq[Food] = Food.findFoodFor(Id(1)) // Test user's ID is 1

// This test fails        
    testFoods must equalTo(
      List(
        Food(Id(1001), "Fried Green Tomatoes", false, Id(1), dateHelper("04/21/2012")),
        Food(Id(1000), "Mashed Potatoes", false, Id(1), dateHelper("05/21/2012"))
      )
    )

// This test passes
    testFoods.head.expiry must equalTo(dateHelper("04/21/2012"))
  }
}

The error output tells me that the date fields are not equal:

[error] x return food for test user in
[error]     'Food(1001,Fried Green Tomatoes,false,1,2012-04-21 00:00:00.0), Food(1000,Mashed Potatoes,false,1,2012-05-21 00:00:00.0)' is not equal to 'Food(1001,Fried Green Tomatoes,false,1,Sat Apr 21 00:00:00 EDT 2012), Food(1000,Mashed Potatoes,false,1,Mon May 21 00:00:00 EDT 2012)' (ModelSpec.scala:66)
[error] Expected: ...se,1,[Sat Apr ]21...00[ EDT 2]0[12]),...1,[Mon May ]21...00[ EDT 2]0[12])
[error] Actual:   ...se,1,[2012-04-]21...00[.]0[]),...1,[2012-05-]21...00[.]0[])

Is there something I’m missing, here?

Edit: It looks like it was the DB schema, which had set the expiry column to be of type timestamp, instead of type date.

More helpful-looking info here: java.util.Date vs java.sql.Date

  • 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-01T07:02:54+00:00Added an answer on June 1, 2026 at 7:02 am

    The problem is likely because of how the data is loaded from the database. I bet it’s using java.sql.Timestamp instead of java.util.Date.

    scala> val date = dateHelper("05/12/1974")
    d: java.util.Date = Sun May 12 00:00:00 CDT 1974
    
    scala> val dbDate = new java.sql.Timestamp(d.getTime)
    dbDate: java.sql.Timestamp = 1974-05-12 00:00:00.0
    
    scala> date == dbDate
    res6: Boolean = true
    
    scala> dbDate == date // prepare to be amazed!
    res5: Boolean = false
    

    If you read the fine print in the Javadoc, you’ll find this nice little statement:

    Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.

    There may be something wrong in your ORM mapping, or your database schema. Is the column a DATE or TIMESTAMP or TIMESTAMP WITH TIMEZONE? If it’s DATE, you ought to map that to java.sql.Date, which should compare correctly. If it’s TIMESTAMP WITH TIMEZONE, then your dateHelper ought to build Timestamps. If it’s TIMESTAMP, good luck, since the database will have lost timezone information.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.