7

When compiling following code snippet:

class MyTest {

  @org.junit.Test
  def `test test`() {

  }
}

Method test test is being put to bytecode as test$u0020test.

Why it happens and how can this be disabled?

Space is valid identifier for method name according to JVM spec. Also, there is no mention of such encoding in Scala language specification. Moreover, other JVM languages like Groovy and Groovy-based Spock Framework do not encode spaces.

Why do i need this: Human-friendly JUnit test names and test reports.

Java 1.8.0_45, Scala 2.11.6

Sergey Alaev
  • 3,851
  • 2
  • 20
  • 35
  • 2
    I guess the only reason for this is uniformity. They probably did not want to special-case the character classes that are not valid identifiers. Technically I do not see a necessity either. – Gábor Bakos May 17 '15 at 10:52
  • 1
    I'm sure sure why you'd expect to find this in the language specification; it's a specification of *the language*, not the compiler implementation, right? – Chris Martin May 17 '15 at 16:56

2 Answers2

4

Look at https://github.com/sbt/junit-interface

-s Try to decode Scala names in stack traces and test names. Fall back silently to non-decoded names if no matching Scala library is on the class path.

So we can say

testOptions += Tests.Argument(TestFrameworks.JUnit, "-q", "-v", "-s")

in build.sbt file, then for a method

def `get one hundred if fifty plus fifty started` {}

the test result shows

[info] Test ScalaTest.get one hundred if fifty plus fifty started

not

[info] Test ScalaTest.get$u0020one$u0020hundred$u0020if$u0020fifty$u0020plus$u0020fifty started
Yanbin
  • 124
  • 7
3

It wouldn't be callable from Java otherwise. Interoperability with Java was an important consideration in the design of Scala.

OTOH, experience has shown that it's best to just stick to Java names in such cases.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • 1
    $ seem to be a valid java identifier: http://stackoverflow.com/questions/7484210/what-is-the-meaning-of-in-a-variable-name – Siphor May 17 '15 at 12:38
  • 1
    It's super easy to write Scala code that isn't callable from Java, though (name a method `const`, for example). I'm not sure I'm 100% convinced by this explanation. – Travis Brown May 17 '15 at 15:11