The Artima Developer Community
Sponsored Link

Weblogs Forum
First Impression: Scala Goodies

4 replies on 1 page. Most recent reply: Jan 24, 2012 6:20 PM by Dale Asberry

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Dale Asberry

Posts: 161
Nickname: bozomind
Registered: Mar, 2004

First Impression: Scala Goodies (View in Weblogs)
Posted: Jan 23, 2012 11:35 AM
Reply to this message Reply
Summary
I'm test-infected and that's why one of my first exciting Scala discoveries was ScalaTest
Advertisement
Actually, the first big thing I noticed about Scala was the crispness I could write my code. Put it together with the ScalaTest framework and you get the following highly-readable and obvious test snippet (excerpted from ScalaTest Testing with Mock Objects: Using Borachio):
val mockCollaborator = mock[Collaborator]

mockCollaborator expects 'documentAdded withArgs ("Document")
mockCollaborator expects 'documentChanged withArgs ("Document") repeated 3 times

classUnderTest.addDocument("Document", new Array[Byte](0))
classUnderTest.addDocument("Document", new Array[Byte](0))
classUnderTest.addDocument("Document", new Array[Byte](0))
classUnderTest.addDocument("Document", new Array[Byte](0))

Clean.


Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: First Impression: Scala Goodies Posted: Jan 24, 2012 3:30 PM
Reply to this message Reply
This looks nice. But it brings up some of my doubts about unit tests. As the test is written, you force the implementation because you are testing the wrong "requirement". (If I understand correctly) What if changing the document is very time consuming, say it needs to be stored as some overly-complex XML over a network? One smart implementation might be to check if the new value is the same as the old one, and, if so, do nothing. That implementation possibly fails the "This happens 3 times" part of the unit test.

Testing that a setter got called 3 times is very nearly pointless. Yes, I know, arguably it's officially needed to catch when somebody screws up horribly, but, 99.99% of the time it's just plain pointless. Other than when you first write it, how many times have you seen a unit test that a getter/setter was called fail? IMO, the real requirement isn't that the setter got called 3 times, it's that that the value of the Document is correct[i/], i.e. in this example the value is
Array[Byte](0)

Dale Asberry

Posts: 161
Nickname: bozomind
Registered: Mar, 2004

Re: First Impression: Scala Goodies Posted: Jan 24, 2012 4:40 PM
Reply to this message Reply
Of course, this is just a contrived unit test to demonstrate principles of the tool, not unit tests in general. So, your point about what a unit test should do is correct.

I'm sure that if you want to improved the documentation for the ScalaTest tool, that Bill would welcome your efforts!

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: First Impression: Scala Goodies Posted: Jan 24, 2012 6:03 PM
Reply to this message Reply
Sorry, on rereading, what I said does sound a bit harsh - not meant to be a serious criticism of your contrived example.

Out of interest, how hard would it be to test that the contents are correct?

Dale Asberry

Posts: 161
Nickname: bozomind
Registered: Mar, 2004

Re: First Impression: Scala Goodies Posted: Jan 24, 2012 6:20 PM
Reply to this message Reply
Actually not "my" contrived example. I pulled it from the documentation.

What I would do is create an expected "Document" and use methods on the class to get the actual "Document" and compare the two. I also wouldn't test getter/setter functionality unless my employer wanted documented code coverage.

Flat View: This topic has 4 replies on 1 page
Topic: Java 7: Too Little, Too Late? Previous Topic   Next Topic Topic: Generic functions vs mixins: a case study

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use