I am running test for this scala code using junit
//Scala Code
package com.sd.proj.executable
object HelloWorld {
def my_message(msg:String):String ={
msg
}
}
//Scala Test
package com.sd.proj.junit_test
import org.junit.runner.RunWith
import org.scalatestplus.junit.JUnitRunner
import com.sd.proj.executable.HelloWorld
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers
#RunWith(classOf[JUnitRunner])
class HelloWorldTest extends AnyFlatSpec with Matchers {
val msg = HelloWorld.my_message("Hello World!!!")
println("testing - " + msg)
msg mustBe "Hello World!!!"
}
The test is executing correctly, however it does not show the green tick for the test, just shows this
this is my build.gradle file
plugins {
id 'scala'
id 'idea'
}
repositories {
mavenCentral()
}
sourceSets{
main{
scala.srcDirs = ['src/main/scala']
resources.srcDirs = ['src/main/resources']
}
test{
scala.srcDirs = ['src/test/scala']
resources.srcDirs = ['src/test/resources']
}
}
dependencies {
//scala
implementation 'org.scala-lang:scala-library:2.12.15'
implementation 'org.scala-lang:scala-reflect:2.12.15'
implementation 'org.scala-lang:scala-compiler:2.12.15'
//junit
testImplementation 'junit:junit:4.12'
testImplementation 'org.scalatestplus:scalatestplus-junit_2.12:1.0.0-M2'
}
Intellij version - IntelliJ IDEA 2021.3.1 (Community Edition)
OS : macOS ventura 13.0
This is the first time I am setting up IntelliJ to run code on my mac.
Can someone please suggest, is it something I am doing incorrectly or is this an issue with Intellij?
I was expecting a green tick for the passed test on intellij
Nevermind, found the solution.
I was writing the test incorrectly, it should be
#RunWith(classOf[JUnitRunner])
class HelloWorldTest extends AnyFlatSpec with Matchers {
"hello world" should "return" in {
val msg = HelloWorld.my_message("Hello World!!!")
println("testing - " + msg)
msg mustBe "Hello World!!!"
}
}
Related
How to get the package of methods or objects in Scala?
For example
import scala.math._
round().getClass // errors
Is there a way to get the desired output: "scala.math.round"? So that I can be sure of round() method doesn't come from any other package I've imported.
PS. Without using Intellij IDE. The IDE shows you this info, hence there has to be a function that IDE calls to get this info.
EDIT:
For example, running these commands in scala shell
With Scala reflection (libraryDependencies += scalaOrganization.value % "scala-reflect" % scalaVersion.value in build.sbt) you can do
import scala.math._
import scala.reflect.runtime.universe._
object App {
def main(args: Array[String]): Unit = {
println(
showRaw(reify {
round(???)
})
)
}
}
Output at runtime will be
Expr(Apply(Select(Ident(scala.math.package), TermName("round")), List(Select(Ident(scala.Predef), TermName("$qmark$qmark$qmark")))))
Or you can add scalacOptions in Compile ++= Seq("-Xprint-types", "-Xprint:typer") to build.sbt.
Then compilation of
package pckg
import scala.math._
object App {
round(???)
}
will produce (at compile time)
Warning:scalac: package pckg{pckg.type} {
import scala.math._;
object App extends scala.AnyRef {
def <init>(): pckg.App.type = {
App.super{pckg.App.type}.<init>{()Object}(){Object};
(){Unit}
}{Unit};
scala.math.`package`.round{(x: Long)Long}(scala.Predef.???{Nothing}){Long}
}
}
I have a scala project, but the imports don't work as designed. I tried everything here, but nothing seems to fix the issue. My project is as follows:
- src
- main
- scala
- importtest
ImportTest.scala
Main.scala
build.sbt
Imported class:
#/src/main/scala/importtest/ImportTest.scala
package importtest
class ImportTest {
def run(): Unit = {
System.out.println("boo!")
}
}
My main class is:
#/src/main/scala/Main.scala
import importtest.ImportTest
object Main {
def main(): Unit = {
val i = ImportTest()
}
}
My SBT build is:
name := "ImportTest"
version := "0.1"
scalaVersion := "2.12.6"
When I try to build, I get:
Error:(5, 13) not found: value ImportTest
val i = ImportTest()
What is going wrong here? Why can't I import the ImportTest class?
Also, not sure if this helps, but IntelliJ will autocomplete the package name, but it cant autocomplete the class within the package - it marks it as unresolved.
You are initializing ImportTest() as if it was a case class.
Because its a regular class, you need to use "new".
Change the initialization to:
val i = new ImportTest()
I changed a test from WordMatchers to FunSpec and now can not rid the tests of the following compilation error:
class SangriaDnbIACDataPipelineTest extends FunSpec {
test("SangriaDnbIACDataPipeline") {
val args =
error: package test is not a value
[ERROR] test("SangriaDnbIACDataPipeline") {
This is on scala 2.11 with scalatest 3.0.1.
I think you're looking for the FunSuite extension:
import org.scalatest.FunSuite
class SangriaDnbIACDataPipelineTest extends FunSuite {
test("SangriaDnbIACDataPipeline") {
val args =
See the testing styles
i am trying to run a simple ScalaTest app to understand the scala testing
my code is:
package org.scalatest.examples.flatspec
import org.scalatest.FlatSpec
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}
i have also downloaded scalatest_2.11-2.2.4.jar. i am compiling the file using command
scalac -cp scalatest_2.11-2.2.4.jar ExampleSpec.scala
and facing the following error
error while loading package, class file needed by package is missing.
reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
I followed the example from "XPath + HOFs" part of https://github.com/json4s/json4s, below is my source code:
import org.json4s._
import org.json4s.native.JsonMethods._
object HiveToCSVEngine {
def main(args: Array[String]): Unit = {
val json = parse( """
{ "name": "joe",
"children": [
{
"name": "Mary",
"age": 5
},
{
"name": "Mazy",
"age": 3
}
]
}
""")
print((json \ "children")(0))
}
}
no compile errors, but the following error occurred at runtime:
Exception in thread "main" java.lang.NoSuchMethodError:
scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object;
at org.json4s.MonadicJValue.$bslash(MonadicJValue.scala:18)
Could anybody help me on this problem?
===How is the program invoked===
I use gradle to run above code, below is the build.gradle file(the dependencies are what is needed by other programs):
apply plugin: 'scala'
apply plugin: 'java'
apply plugin:'application'
//mainClassName = 'SimpleMail'
mainClassName = System.getProperty("mainClassName")
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
repositories {
mavenCentral()
}
dependencies {
//can't comment scalaTools on linux because of gradle version problem
scalaTools "org.scala-lang:scala-compiler:2.11.1"
compile 'org.scala-lang:scala-library:2.11.1'
compile 'com.github.tototoshi:scala-csv_2.10:1.0.0'
compile 'com.darwinsys:hirondelle-date4j:1.5.1'
compile 'com.sun.mail:javax.mail:1.5.2'
compile 'org.apache.hive:hive-jdbc:0.13.1'
compile 'org.apache.hadoop:hadoop-core:1.2.1'
compile 'mysql:mysql-connector-java:5.1.31'
compile 'net.sourceforge.expectj:expectj:2.0.7'
compile 'org.apache.commons:commons-lang3:3.3'
compile 'org.json4s:json4s-native_2.10:3.2.10'
compile 'org.json4s:json4s-jackson_2.10:3.2.10'
}
run {
// set heap size for the test JVM(s)
minHeapSize = "128m"
maxHeapSize = "512m"
// set JVM arguments for the test JVM(s)
jvmArgs '-XX:MaxPermSize=256m'
if ( project.hasProperty('args') ) {
def paras=project.args.trim()
println "args:"+paras
def leading_paras=[]
while(paras.contains('\'')){
def prefix=paras.substring(0,paras.indexOf('\''))
def right_part=paras.substring(prefix.size()+1)
def middle=right_part.substring(0,right_part.indexOf('\''))
def tail=right_part.substring(middle.size()+1)
if(prefix.size()>0) {
leading_paras = (leading_paras << prefix.split('\\s+')) << [middle]
}else{
leading_paras =leading_paras<<[middle]
}
paras=tail.trim()
}
leading_paras=(leading_paras<<paras.split('\\s+')).flatten()
println "[param list]"
for(String para:leading_paras){
println "param:"+para+"."
}
args (leading_paras as String[])
}
}
and I run the following command to invoke the program:
gradle run -DmainClassName=HiveToCSVEngine
Before:
compile 'org.json4s:json4s-native_2.10:3.2.10'
compile 'org.json4s:json4s-jackson_2.10:3.2.10'
Update:
compile 'org.json4s:json4s-native_2.11:3.2.10'
compile 'org.json4s:json4s-jackson_2.11:3.2.10'
I think it's your scala is 2.11, but your json4s is 2.10