The Artima Developer Community
Sponsored Link

ScalaTest/ScalaUtils Forum
Testing private Methods

2 replies on 1 page. Most recent reply: Nov 6, 2008 8:23 PM by Bill Venners

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 2 replies on 1 page
Lukas Rytz

Posts: 1
Nickname: lukasrytz
Registered: May, 2008

Testing private Methods Posted: May 23, 2008 1:12 AM
Reply to this message Reply
Advertisement
As in JUnit, is there no direct way to test private methods?
http://www.artima.com/suiterunner/private.html


Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Testing private Methods Posted: Jun 1, 2008 8:32 PM
Reply to this message Reply
> As in JUnit, is there no direct way to test private
> methods?
> http://www.artima.com/suiterunner/private.html
>
Yes, that's the case. What you can do is make a "Friend" class that exposes methods with the same API, which wrap the class under test and uses reflection to invoke the private methods. I've done that in a few places inside ScalaTest itself. Someday I may make a codegen tool that creates such friend classes (though maybe they should be called Pimp classes, now that I think about it.) But for now, you'd have to do that by hand.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Testing private Methods Posted: Nov 6, 2008 8:23 PM
Reply to this message Reply
Hi Lukas,

An Update: I just checked in a Pimp trait to the trunk that offers a nice syntax for invoking private methods dynamically. It will be in the next release. To use it you mix trait Pimp into your test class. In tests people usually invoke the same method several times, so first you'd create a PrivateMethod instance and store it in a val of the name of the method. The PrivateMethod construct takes a Symbol of the name of the private method and a Class instance representing its result type. You can then invoke the private method by using the "invokePrivate" operator instead of a dot. Here's an example method that uses the trait:


def testDecorateToStringValue() {

val decorateToStringValue = PrivateMethod('decorateToStringValue, classOf[String])

expect("1") {
FailureMessages invokePrivate decorateToStringValue(1.toByte)
}
expect("1") {
FailureMessages invokePrivate decorateToStringValue(1.toShort)
}
expect("1") {
FailureMessages invokePrivate decorateToStringValue(1)
}
expect("10") {
FailureMessages invokePrivate decorateToStringValue(10L)
}
expect("1.0") {
FailureMessages invokePrivate decorateToStringValue(1.0f)
}
expect("1.0") {
FailureMessages invokePrivate decorateToStringValue(1.0)
}
expect("false") {
FailureMessages invokePrivate decorateToStringValue(false)
}
expect("true") {
FailureMessages invokePrivate decorateToStringValue(true)
}
expect("<(), the Unit value>") {
FailureMessages invokePrivate decorateToStringValue(())
}
expect("\"Howdy!\"") {
FailureMessages invokePrivate decorateToStringValue("Howdy!")
}
expect("'c'") {
FailureMessages invokePrivate decorateToStringValue('c')
}
expect("Hey!") {
FailureMessages invokePrivate decorateToStringValue(new AnyRef { override def toString = "Hey!"})
}
}


One cool thing is the result of the method invocation will be the result type of the private method, the type represented by the Class instance passed into the 2nd arg of PrivateMethod's constructor. So you need not cast to use the result. In other words, because decorateToStringValue's result type is String, the type of val s here will be inferred to have type String:


val s = FailureMessages invokePrivate decorateToStringValue(false)

Flat View: This topic has 2 replies on 1 page
Topic: Ignore annotation under scala 2.7.2 Previous Topic   Next Topic Topic: PropSuite gives RuntimeException in scalac

Sponsored Links



Google
  Web Artima.com   

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