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!!!"
}
}
I've been working with Cassandra for a little while and now I'm trying to setup spark and spark-cassandra-connector. I'm using IntelliJ IDEA to do that (first time with IntelliJ IDEA and Scala too) in Windows 10.
build.gradle
apply plugin: 'scala'
apply plugin: 'idea'
apply plugin: 'eclipse'
repositories {
mavenCentral()
flatDir {
dirs 'runtime libs'
}
}
idea {
project {
jdkName = '1.8'
languageLevel = '1.8'
}
}
dependencies {
compile group: 'org.apache.spark', name: 'spark-core_2.11', version: '2.4.5'
compile group: 'org.apache.spark', name: 'spark-sql_2.11', version: '2.4.5'
compile group: 'org.scala-lang', name: 'scala-library', version: '2.11.12'
compile group: 'com.datastax.spark', name: 'spark-cassandra-connector_2.11', version: '2.5.0'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
}
configurations.all {
resolutionStrategy {
force 'com.google.guava:guava:12.0.1'
}
}
compileScala.targetCompatibility = "1.8"
compileScala.sourceCompatibility = "1.8"
jar {
zip64 true
archiveName = "ModuleName.jar"
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'org.module.ModuelName'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
ModuleName.scala
package org.module
import org.apache.spark.sql.SparkSession
import com.datastax.spark.connector._
import org.apache.spark.sql.types.TimestampType
object SentinelSparkModule {
case class Document(id: Int, time: TimestampType, data: String)
def main(args: Array[String]) {
val spark = SparkSession.builder
.master("spark://192.168.0.3:7077")
.appName("App")
.config("spark.cassandra.connection.host", "127.0.0.1")
.config("spark.cassandra.connection.port", "9042")
.getOrCreate()
//I'm trying it without [Document] since it throws 'Failed to map constructor parameter id in
//org.module.ModuleName.Document to a column of keyspace.table'
val documentRDD = spark.sparkContext
.cassandraTable/*[Document]*/("keyspace", "table")
.select()
documentRDD.take(10).foreach(println)
spark.stop()
}
}
I have a running spark master at spark://192.168.0.3:7077 and a worker of that master, but I haven't tried to submit the job as a compiled jar in the console, I'm just trying to get it to work in the IDE.
Thanks
Cassandra connector jar needs to be added to the classpath of workers. One way to do this is to build an uber jar with all required dependencies and submit to the cluster.
Refer to: Building a uberjar with Gradle
Also, make sure you change the scope of dependencies in you build file from compile to provided for all jars except the cassandra connector.
Reference: https://reflectoring.io/maven-scopes-gradle-configurations/
Say I have a scalatest class in main/scala, like
import org.scalatest.FunSuite
class q3 extends FunSuite {
test("6 5 4 3 2 1") {
val digits = Array(6,5,4,3,2,1)
assert(digits.sorted === Array(1,2,3,4,5,6))
}
}
How do I run it with sbt?
I've tried sbt test, sbt testOnly, sbt "testOnly *q3" and they all had output like
[info] Run completed in 44 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] No tests were executed.
[info] No tests to run for Test / testOnly
A similar question from a few years back said they successfully used testOnly but I can't get it to work.
The metals extension on VSCode shows a "test" link when the file is open which successfully runs the test, but doesn't show how it does that. I want to know how to do it through sbt.
Put ScalaTest on Compile classpath in build.sbt like so
libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.0"
and then call org.scalatest.run runner explicitly from within an App, for example,
object MainTests extends App {
org.scalatest.run(new ExampleSpec)
}
Putting it together we have in src/main/scala/example/MainTests.scala
package example
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends AnyFlatSpec 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)
}
}
object MainTests extends App {
org.scalatest.run(new ExampleSpec)
}
and run it with runMain example.MainTests. Furthermore, we could gather tests in Suites and execute all like so
class ExampleASpec extends FlatSpec with Matchers {
"ExampleA" should "run" in { succeed }
}
class ExampleBSpec extends FlatSpec with Matchers {
"ExampleB" should "run" in { succeed }
}
class ExampleSuite extends Suites(
new ExampleASpec,
new ExampleBSpec,
)
object MainTests extends App {
(new ExampleSuite).execute()
}
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
I have a NetBeans web project building Java, JS and HTML files. In order to get retrofit working to import a JSON API, I need to inject a dependency in a build.gradle file in the root of the project. I've written a headless build.gradle file, which I've tested and correctly handles the dependencies, how do I now change project to run from the build.gradle file instead of ANT everytime and searching through libraries?
You need a build.gradle and settings.gradle file, and you have to delete the build.xml file (and maybe the nbproject/ directory)
I recommend making a gradle project using the netbeans gradle plugin and copying over and modifying the build.gradle file.
Then you need to add source sets in your build.gradle for where your sources are - gradle expects them in different place than netbeans ant puts them
sourceSets {
main {
java {
srcDir 'src'
}
resources {
srcDir 'resources'
}
}
test {
java {
srcDir 'test'
}
resources {
srcDir 'testResources'
}
}
}
also, you need to make your dependencies. This is what mine looks like:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10+'
compile project(':logger')
compile project(':postalker')
compile group: 'org.yaml', name: 'snakeyaml', version: '1.15+'
compile group: 'commons-io', name: 'commons-io', version: '2.4+'
compile group: 'gov.nist.math', name: 'jama', version: '1.0.3+'
compile group: 'org.apache.commons', name: 'commons-imaging', version: '1.0-SNAPSHOT+'
compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.5+'
compile files( 'lib/jogl-runtime/jogl.jar')
compile files( 'lib/gluegen-runtime/gluegen-rt.jar')
compile files( 'lib/toxiclibscore.jar')
compile group: 'org.apache.commons', name: 'commons-math3', version: '3.5+'
compile group: 'commons-codec', name: 'commons-codec', version: '1.6'
compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.6'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.3.6'
}
finally,
I had to close the old project, shut netbeans down, then re-open netbeans and the project. Do a reload project and it was up and running!
for the person wondering how I got jogl working, I looked at what the jogl plugin did with ant and replicated it with gradle. I add a PlatformPrivate.gradle file that contains the developer's platform, then copy the appropriate libraries to the build directory
my entire build.gradle:
import com.protocase.files.FolderUtils
import java.nio.file.Files
import java.nio.file.Paths
import com.protocase.designer.extractors.BuildInfoExtractor
import com.protocase.designer.fileeditors.NSISInstallerModifier
import com.protocase.designer.fileeditors.SignBatModifier
import com.protocase.designer.ConfigureRun
import com.protocase.finders.FindFiles
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'groovy'
apply from: "PlatformPrivate.gradle"
apply from: "Platforms.gradle"
sourceCompatibility = '1.6'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
ext.mainClass = 'com.protocase.viewer.JDesigner'
}
if (! hasProperty('localPlatform')) {
throw new GradleException("Missing PlatformPrivate.gradle");
}
mainClassName = ext.mainClass
repositories {
mavenCentral()
maven {
url "https://repository.apache.org/content/repositories/snapshots/"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10+'
compile project(':logger')
compile project(':postalker')
compile group: 'org.yaml', name: 'snakeyaml', version: '1.15+'
compile group: 'commons-io', name: 'commons-io', version: '2.4+'
compile group: 'gov.nist.math', name: 'jama', version: '1.0.3+'
compile group: 'org.apache.commons', name: 'commons-imaging', version: '1.0-SNAPSHOT+'
compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.5+'
compile files( 'lib/jogl-runtime/jogl.jar')
compile files( 'lib/gluegen-runtime/gluegen-rt.jar')
compile files( 'lib/toxiclibscore.jar')
compile group: 'org.apache.commons', name: 'commons-math3', version: '3.5+'
compile group: 'commons-codec', name: 'commons-codec', version: '1.6'
compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.6'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.3.6'
}
task copyDistLibs(type: Copy) {
def outputDir = file("${buildDir}/libs")
from configurations.runtime
into outputDir
}
task updateBuildInfoBeta (type: Exec) {
workingDir "${projectDir}/perl"
commandLine './updateBuildInfo'
def extractor = new BuildInfoExtractor(projectFile: new File("${projectDir}"))
def version = extractor.getBetaVersion()
def installerModifier = new NSISInstallerModifier(NSISFile: new File("${projectDir}/winRunFiles/JDesigner.nsi"))
installerModifier.setOutputFileVersion(version)
def signBatModifier = new SignBatModifier(signBatFile: new File("${projectDir}/sign.bat"))
signBatModifier.setOutputFileVersion(version)
println ""
println "Making Beta Version: " + version
println "branch: " + extractor.getReleaseVersion() + " date: " + extractor.getBuildDate()
println ""
}
task makeBeta(dependsOn: updateBuildInfoBeta) {
def distFolderLinux64 = makeOneDistBeta("linux-amd64")
def ditsFolderLinux32 = makeOneDistBeta("linux-i586")
def distFolderWin32 = makeOneDistBeta("windows-i586")
FolderUtils.copyFolderContents(new File("${projectDir}/jre"), new File(distFolderWin32, "jre"))
FolderUtils.copyFolderContents(new File("${projectDir}/src/main/resources/library"), new File(distFolderWin32, "library"))
FolderUtils.copyFolderContents(new File("${projectDir}/winRunFiles"), distFolderWin32)
Files.copy(Paths.get("${projectDir}/license.txt"), Paths.get(new File(distFolderWin32, "license.txt").path))
Files.copy(Paths.get("${projectDir}/Protocase Designer.ico"), Paths.get(new File(distFolderWin32, "Protocase Designer.ico").path))
def distFolderMac = makeOneDistBeta("macosx-universal")
}
private File makeOneDistBeta(def nativePlatform) {
def plat = new com.protocase.designer.Platform(natives: nativePlatform, libFolder: 'lib', genericBuildDir: new File("${buildDir}"))
println plat.getNativeDistDir()
plat.makeAndPopulateBuildDir()
plat.copyLibraryFiles()
plat.getNativeDistDir()
}
makeBeta.dependsOn(jar)
makeBeta.dependsOn(copyDistLibs)
task makeWin32Beta(dependsOn: makeBeta, type: Exec) {
def wineDir = new File("${projectDir}/../../.wine/drive_c/")
println wineDir.exists()
def nsisFile = FindFiles.findFirstFileByNameWithoutDirectory(wineDir, "makensis.exe", "users")
println nsisFile.path
def MAKENSIS = nsisFile.getPath().replaceFirst("(.*)drive_c/", "c:\\\\")
println MAKENSIS
MAKENSIS = MAKENSIS.replaceAll("/","\\\\")
def extractor = new BuildInfoExtractor(projectFile: new File("${projectDir}"))
def version = extractor.getBetaVersion()
workingDir "${buildDir}/windows-i586"
def fullCommand = 'WINEDEBUG=fixme-win,fixme-sync,fixme-heap /usr/bin/wine "' + MAKENSIS + '" /V1 /DVER_STR=' + version + ' /X"SetCompressor /FINAL lzma" ./JDesigner.nsi'
println "makensis command:"
println MAKENSIS
println fullCommand
println ""
commandLine fullCommand
}
private void configureRun (def task) {
apply from: 'PlatformPrivate.gradle'
def confRun = new ConfigureRun(localPlatform: localPlatform)
confRun.configureRun(task, mainClass, sourceSets)
}
run {
configureRun(it)
}
task(debug, dependsOn: 'classes', type: JavaExec) {
configureRun(it)
debug = true
}
The PlatformPrivate.gradle
ext {
localPlatform='linux-amd64'
}
com.protocase.designer.Platform:
package com.protocase.designer
import com.protocase.files.FolderUtils
import org.gradle.api.GradleException
import static groovy.io.FileType.FILES
import java.nio.file.StandardCopyOption
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
public class Platform {
def natives
def libFolder
def genericBuildDir
String gluegenDir() {
def dirName = 'gluegen-rt.jar-natives-' + natives
dirName
}
String joglDir() {
def dirName = "jogl.jar-natives-" + natives
dirName
}
def getExtension() {
def result = ""
switch (natives)
{
case 'linux-amd64':
case 'linux-i586':
case 'solaris-i586':
case 'solaris-sparc':
case 'solaris-sparcv9':
result = 'so'
break;
case 'macosx-universal':
case 'macosx-ppc':
result = 'jnilib'
break;
case 'windows-i586':
case 'windows-amd64':
result = 'dll'
break;
default:
throw new GradleException ('programmer missed a case or bad platform: ' + natives)
}
result
}
def getNativeDistDir() {
def buildDir = new File(genericBuildDir, natives)
}
def makeAndPopulateBuildDir() {
def sourceDir = new File(genericBuildDir, 'libs')
def nativeDistDir = getNativeDistDir()
if (nativeDistDir.exists()) {
FolderUtils.deleteFolder(nativeDistDir)
}
FolderUtils.copyFolderContents(sourceDir, nativeDistDir)
}
def getLibraryDirs() {
def dirList = []
def dir = new File(new File(libFolder, "gluegen-runtime"), gluegenDir())
dirList << dir
dir = new File(new File(libFolder, "jogl-runtime"), joglDir())
dirList << dir
dirList
}
def getLibraryFiles() {
def fileList = []
def dirs = getLibraryDirs()
dirs.each {
it.eachFile(FILES) {
file -> fileList << file
}
}
fileList
}
def copyLibraryFiles() {
def copyOptions = [StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES]
def destinationFolder = new File(getNativeDistDir(), 'lib')
getLibraryDirs().each {
FolderUtils.copyFolderContents(it, destinationFolder)
}
}
}