Java null pointer exception in using invokePrivate

69 views
Skip to first unread message

Eric Fowler

unread,
Apr 5, 2015, 9:53:24 PM4/5/15
to scalate...@googlegroups.com
I have a class:

class MyClass
{
def foo(s:String) : Unit = {/*nothing important*/}
}

And a scalatest that tests for null input data. This test worked back when foo was public: 
"My test" should "not choke on null data" in {

val mc = new MyClass

mc.foo(null) .... mumble .... whatever

}

All good. 

Now I make foo private, and rejigger the test to use privateInvoke:
"My test" should "not choke on null data" in {
import org.scalatest.PrivateMethodTester._

val mc = new MyClass
val foo = PrivateMethod[Unit]('foo)

mc invokePrivate foo(null) .... mumble .... whatever

}


For this, I get something that looks like this. 

The salient point is (1) it is expecting an InvalidArgumentException (why?) and (2)  it got a NullPointerException (why?)

Anyone know what is going on here?


java.lang.NullPointerException was thrown.
java.lang.NullPointerException
at org.scalatest.PrivateMethodTester$Invoker.org$scalatest$PrivateMethodTester$Invoker$$argMatchesParamType$1(PrivateMethodTester.scala:196)
at org.scalatest.PrivateMethodTester$Invoker$$anonfun$2.apply(PrivateMethodTester.scala:203)
at org.scalatest.PrivateMethodTester$Invoker$$anonfun$2.apply(PrivateMethodTester.scala:203)
at scala.collection.TraversableLike$WithFilter$$anonfun$withFilter$1.apply(TraversableLike.scala:789)
at scala.collection.TraversableLike$WithFilter$$anonfun$withFilter$1.apply(TraversableLike.scala:789)
at scala.collection.TraversableLike$WithFilter$$anonfun$map$2.apply(TraversableLike.scala:728)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:727)
at org.scalatest.PrivateMethodTester$Invoker.argsHaveValidTypes$1(PrivateMethodTester.scala:203)
at org.scalatest.PrivateMethodTester$Invoker.org$scalatest$PrivateMethodTester$Invoker$$isMethodToInvoke$1(PrivateMethodTester.scala:237)
at org.scalatest.PrivateMethodTester$Invoker$$anonfun$4.apply(PrivateMethodTester.scala:243)
at org.scalatest.PrivateMethodTester$Invoker$$anonfun$4.apply(PrivateMethodTester.scala:243)
at scala.collection.TraversableLike$WithFilter$$anonfun$map$2.apply(TraversableLike.scala:728)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:727)
at org.scalatest.PrivateMethodTester$Invoker.invokePrivate(PrivateMethodTester.scala:243)
at FeasibilityTest$$anonfun$11.apply$mcV$sp(FeasibilityTest.scala:115)
at FeasibilityTest$$anonfun$11.apply(FeasibilityTest.scala:106)
at FeasibilityTest$$anonfun$11.apply(FeasibilityTest.scala:106)
at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FlatSpecLike$$anon$1.apply(FlatSpecLike.scala:1647)
at org.scalatest.Suite$class.withFixture(Suite.scala:1122)
at org.scalatest.FlatSpec.withFixture(FlatSpec.scala:1683)
at org.scalatest.FlatSpecLike$class.invokeWithFixture$1(FlatSpecLike.scala:1644)
at org.scalatest.FlatSpecLike$$anonfun$runTest$1.apply(FlatSpecLike.scala:1656)
at org.scalatest.FlatSpecLike$$anonfun$runTest$1.apply(FlatSpecLike.scala:1656)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
at org.scalatest.FlatSpecLike$class.runTest(FlatSpecLike.scala:1656)
at org.scalatest.FlatSpec.runTest(FlatSpec.scala:1683)
at org.scalatest.FlatSpecLike$$anonfun$runTests$1.apply(FlatSpecLike.scala:1714)
at org.scalatest.FlatSpecLike$$anonfun$runTests$1.apply(FlatSpecLike.scala:1714)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:413)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:401)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401)
at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:390)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:427)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:401)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401)
at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:396)
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:483)
at org.scalatest.FlatSpecLike$class.runTests(FlatSpecLike.scala:1714)
at org.scalatest.FlatSpec.runTests(FlatSpec.scala:1683)
at org.scalatest.Suite$class.run(Suite.scala:1424)
at org.scalatest.FlatSpec.org$scalatest$FlatSpecLike$$super$run(FlatSpec.scala:1683)
at org.scalatest.FlatSpecLike$$anonfun$run$1.apply(FlatSpecLike.scala:1760)
at org.scalatest.FlatSpecLike$$anonfun$run$1.apply(FlatSpecLike.scala:1760)
at org.scalatest.SuperEngine.runImpl(Engine.scala:545)
at org.scalatest.FlatSpecLike$class.run(FlatSpecLike.scala:1760)
at org.scalatest.FlatSpec.run(FlatSpec.scala:1683)
at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:55)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$3.apply(Runner.scala:2563)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$3.apply(Runner.scala:2557)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:2557)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1044)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1043)
at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:2722)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1043)
at org.scalatest.tools.Runner$.run(Runner.scala:883)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)


Expected exception java.lang.IllegalArgumentException to be thrown, but java.lang.NullPointerException was thrown.

Eric Fowler

unread,
Apr 5, 2015, 10:14:00 PM4/5/15
to scalate...@googlegroups.com
Doing a little more investigating, it seems to be having trouble here:

case _ => paramType.isAssignableFrom(anyRefArg.getClass)

The type matching system is having trouble with that null parameter. 

I was lying earlier, I'm not passing a string. Actually I am passing one of these: 

class MyClass

Which should look familiar to all of you. 

How can I tell it what type to expect? 

I tried calling it like this: 

mc invokePrivate foo(null:String)

Which, amazingly, compiled, but also failed. 

Bill Venners

unread,
Apr 6, 2015, 1:04:23 AM4/6/15
to scalate...@googlegroups.com
Hi Eric,

What is the method supposed to do when null is passed?

Bill
> --
> You received this message because you are subscribed to the Google
> Groups "scalatest-users" group.
> To post to this group, send email to scalate...@googlegroups.com
> To unsubscribe from this group, send email to
> scalatest-use...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/scalatest-users?hl=en
> ScalaTest itself, and documentation, is available here:
> http://www.artima.com/scalatest
> ---
> You received this message because you are subscribed to the Google Groups
> "scalatest-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scalatest-use...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Bill Venners
Artima, Inc.
http://www.artima.com

Eric Fowler

unread,
Apr 6, 2015, 1:21:43 AM4/6/15
to scalate...@googlegroups.com
It handles it ... basically a null result. But no exceptions. 

The original function takes three vertices, and splices the third one between the first two. If the second is null, the new vertex is appended; if the first is null, it is prepended; if both are null, the third is added to a list as it's sole member. 

Eric

You received this message because you are subscribed to a topic in the Google Groups "scalatest-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scalatest-users/fhaMopquip0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scalatest-use...@googlegroups.com.

Bill Venners

unread,
Apr 6, 2015, 11:38:41 AM4/6/15
to scalate...@googlegroups.com
Hi Erik,

Yes, it looks like I never imagined someone passing in null. I think
we will have to enhance PrivateMethodTester to allow null to be
passed.

Bill
Reply all
Reply to author
Forward
0 new messages