sbt and play test with #RunWith - intellij-idea

I'm dealing with a problem working on a Play project and sbt.
The problem is:
I have tests, some of them are using PowerMockito, some of them aren't
For the tests using PowerMockito, I've added #RunWith(PowerMockRunner.class) at the beginning of the class.
For the tests not using, I've added nothing.
Here is an example for a test that uses PowerMockito
#RunWith(PowerMockRunner.class)
#PrepareForTest({SomeMockedClass.class})
public class SomeClassTest {
#Test
public void aTest() {
PowerMockito
.stub(PowerMockito.method(SomeMockedClass.class, "aMethod"))
.toReturn(something);
assertThat(someCall).isEqualTo(something);
}
}
Here is an example for a test that doesn't use PowerMockito
public class AnotherClassTest {
#Test
public void anotherTest(){
assertThat(something).isEqualTo(something);
}
}
And here is my build.sbt dep:
libraryDependencies ++= Seq(
cache,
...
// Testing
"org.easytesting" % "fest-assert" % "1.4" % "test",
"junit" % "junit" % "4.12" % "test",
"org.powermock" % "powermock-mockito-release-full" % "1.6.2" % "test",
"org.powermock" % "powermock-module-junit4-rule-agent" % "1.6.2" % "test",
"org.easymock" % "easymock" % "3.3.1" % "test",
"com.novocode" % "junit-interface" % "0.11" % "test"
)
Here is the problem:
When I run my tests from IntelliJ, it founds all tests and everything is ok
When I run my tests from activator (activator test), it does NOT found the tests with the #RunWith.
I've read some post about it, like the sbt version or something
(even https://github.com/sbt/jacoco4sbt/issues/15).
But the sbt version is : 0.13.8-M5 so it seems ok.
Play version is : 2.3.8
If you guys have any clue that'd be great.
Thanks

Related

Xbase Interpreter: Could not access field on instance: null

I am testing the idea of making my dsl Jvm compatible and I wanted to test the possibility of extending Xbase and using the interpreter. I have tried to make a minimal test project to use with the interpreter but I am getting a runtime error. I think I understand the general concepts of adapting Xbase, but am unsure about how the setup/entrypoints for the interpreter and could not find any information regarding the error I am getting or how to resolve. Here are the relevant files for my situation:
Text.xtext:
import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types
Program returns Program:
{Program}
'program' name=ID '{'
variables=Var_Section?
run=XExpression?
'}'
;
Var_Section returns VarSection:
{VarSection}
'variables' '{'
decls+=XVariableDeclaration+
'}'
;
#Override // Change syntax
XVariableDeclaration returns xbase::XVariableDeclaration:
type=JvmTypeReference name=ID '=' right=XLiteral ';'
;
#Override // Do not allow declarations outside of variable region
XExpressionOrVarDeclaration returns xbase::XExpression:
XExpression;
TestJvmModelInferrer:
def dispatch void infer(Program element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass(element.fullyQualifiedName)) [
documentation = element.documentation
if (element.variables !== null) {
for (decl : element.variables.decls) {
members += decl.toField(decl.name, decl.type) [
static = true
initializer = decl.right
visibility = JvmVisibility.PUBLIC
]
}
}
if (element.run !== null) {
members += element.run.toMethod('main', typeRef(Void::TYPE)) [
parameters += element.run.toParameter("args", typeRef(String).addArrayTypeDimension)
visibility = JvmVisibility.PUBLIC
static = true
body = element.run
]
}
]
}
Test case:
#Inject ParseHelper<Program> parseHelper
#Inject extension ValidationTestHelper
#Inject XbaseInterpreter interpreter
#Test
def void basicInterpret() {
val result = parseHelper.parse('''
program program1 {
variables {
int var1 = 0;
double var2 = 3.4;
}
var1 = 13
}
''')
result.assertNoErrors
var interpretResult = interpreter.evaluate(result.run)
println(interpretResult.result)
Partial stack trace:
java.lang.IllegalStateException: Could not access field: program1.var1 on instance: null
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._assignValueTo(XbaseInterpreter.java:1262)
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.assignValueTo(XbaseInterpreter.java:1221)
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:1213)
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:216)
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:204)
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:190)
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:180)
The interpreter does only support expressions, but does not work with types that are created by a JvmModelInferrer. Your code tries to work with fields of such an inferred type.
Rather than using the interpreter, I'd recommend to use an InMemoryCompiler in your test. The domainmodel example may serve as an inspiration: https://github.com/eclipse/xtext-eclipse/blob/c2b15c3ec118c4c200e2b28ea72d8c9116fb6800/org.eclipse.xtext.xtext.ui.examples/projects/domainmodel/org.eclipse.xtext.example.domainmodel.tests/xtend-gen/org/eclipse/xtext/example/domainmodel/tests/XbaseIntegrationTest.java
You may find this project interesting, which (among other stuff) implements an interpreter for Xtend based on the Xbase interpreter. It might be a bit outdated, though, and also will not fully support all Xtend concepts. But it could be a starting point, and your contrbutions are welcome :-)
https://github.com/kbirken/xtendency

Why does compiling Holden Karau's spark-testing-base generate an error?

I am trying to use Holden Karau's spark-testing-base using sbt and get 4 errors. It looks like sbt is generating invalid references to 4 jars.
The errors are:
[error] 4 not found
[error] https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-common/2.6.0/hadoop-common-2.6.0.test-jar
[error] https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-hdfs/2.6.0/hadoop-hdfs-2.6.0.test-jar
[error] https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-mapreduce-client-jobclient/2.6.0/hadoop-mapreduce-client-jobclient-2.6.0.test-jar
[error] https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-yarn-server-tests/2.6.0/hadoop-yarn-server-tests-2.6.0.test-jar
My build.sbt contains:
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.2.1",
"org.scalacheck" %% "scalacheck" % "1.12.4",
"com.holdenkarau" %% "spark-testing-base" % "1.6.1_0.3.3"
)
parallelExecution in Test := false
lazy val root = (project in file(".")).
settings(
name := "core",
version := "1.0",
scalaVersion := "2.11.8"
)
And my test class is from Holden's wiki example:
import org.scalatest._
import com.holdenkarou.SharedSparkContext
class SampleTest extends FunSuite with SharedSparkContext {
test("test initializing spark context") {
val list = List(1, 2, 3, 4)
val rdd = sc.parallelize(list)
assert(rdd.count === list.length)
}
}
When I execute sbt test I get a bunch of maven updates followed by the errors listed above.
It appears that sbt is generating invalid jar names - for example https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-common/2.6.0/hadoop-common-2.6.0.test-jar should be hadoop-common-2.6.0-test.jar (i.e. the separator after the 2.6.0 should be -, not .).
Can someone guide me as to why this is occurring, and how to fix it?

How to enable play-querydsl plugin in Play 2.2?

I have problem setting up querydsl framework in play 2.2.6 with scala 2.10.3 and java 1.7
I have done installation exactly like it was in documentation. But it doesn't work.
I am gettign an error:
dany#dany1L:~/git/app$ playFramework-2.2.6
[info] Loading project definition from /home/dany/git/app/project
/home/dany/git/app/build.sbt:11: error: not found: value QueryDSLPlugin
val current = project.in(file(".")).configs(QueryDSLPlugin.QueryDSL)
^
[error] sbt.compiler.EvalException: Type error in expression
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
Here is my project/plugins.sbt:
// Comment to get more information during initialization
logLevel := Level.Warn
// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Use the Play sbt plugin for Play projects
// changed to support play 2.2.4 addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.1")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.6")
addSbtPlugin("com.code-troopers.play" % "play-querydsl" % "0.1.2")
And my build.sbt:
import com.typesafe.config._
import play.Project._
import sbt._
import Keys._
//javacOptions ++= Seq("-Xlint:unchecked")
playJavaSettings
playJavaSettings ++ QueryDSLPlugin.queryDSLSettings
val current = project.in(file(".")).configs(QueryDSLPlugin.QueryDSL)
val conf = ConfigFactory.parseFile(new File("conf/application.conf")).resolve()
name := conf.getString("app.name")
version := conf.getString("app.version")+"_("+conf.getString("app.releaseDate")+")"
libraryDependencies ++= Seq(
javaJdbc,
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "3.6.9.Final",
"mysql" % "mysql-connector-java" % "5.1.27",
"org.mindrot" % "jbcrypt" % "0.3m",
"org.jasypt" % "jasypt" % "1.9.2",
"org.apache.poi" % "poi" % "3.10.1",
"com.googlecode.genericdao" % "dao" % "1.2.0",
"com.googlecode.genericdao" % "search-jpa-hibernate" % "1.2.0",
"com.google.code.gson" % "gson" % "2.3.1",
"com.googlecode.json-simple" % "json-simple" % "1.1.1",
"javax.mail" % "javax.mail-api" % "1.5.3",
"javax.activation" % "activation" % "1.1.1",
"com.sun.mail" % "javax.mail" % "1.5.3",
"com.querydsl" % "querydsl-jpa" % "4.0.2",
"com.querydsl" % "querydsl-apt" % "4.0.2",
cache
)
Please give me some help.
After adding:
import codetroopers._
on top of build.sbt I am getting an error:
[info] Loading project definition from /home/dany/git/app/project
error: bad symbolic reference. A signature in QueryDSLPlugin.class refers to type AutoPlugin
in package sbt which is not available.
It may be completely missing from the current classpath, or the version on
the classpath might be incompatible with the version used when compiling QueryDSLPlugin.class.
[error] sbt.compiler.EvalException: Type error in expression
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
Thanks to #Nathan
and his answer here
Finally after few days of struggle I've make it working.
Here are my configuration files:
plugins.sbt
// Comment to get more information during initialization
logLevel := Level.Warn
// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Use the Play sbt plugin for Play projects
// changed to support play 2.2.4 addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.1")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.6")
addSbtPlugin("com.code-troopers.play" % "play-querydsl" % "0.1.1")
and build.sbt
import com.typesafe.config._
import play.Project._
import sbt._
import Keys._
//javacOptions ++= Seq("-Xlint:unchecked")
playJavaSettings
val conf = ConfigFactory.parseFile(new File("conf/application.conf")).resolve()
name := conf.getString("app.name")
version := conf.getString("app.version")+"_("+conf.getString("app.releaseDate")+")"
libraryDependencies ++= Seq(
javaJdbc,
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "3.6.9.Final",
"mysql" % "mysql-connector-java" % "5.1.27",
"org.mindrot" % "jbcrypt" % "0.3m",
"org.jasypt" % "jasypt" % "1.9.2",
"org.apache.poi" % "poi" % "3.10.1",
"com.googlecode.genericdao" % "dao" % "1.2.0",
"com.googlecode.genericdao" % "search-jpa-hibernate" % "1.2.0",
"com.google.code.gson" % "gson" % "2.3.1",
"com.googlecode.json-simple" % "json-simple" % "1.1.1",
"javax.mail" % "javax.mail-api" % "1.5.3",
"javax.activation" % "activation" % "1.1.1",
"com.sun.mail" % "javax.mail" % "1.5.3",
"com.querydsl" % "querydsl-jpa" % "4.0.2",
"com.querydsl" % "querydsl-apt" % "4.0.2",
cache
)
playJavaSettings ++ QueryDSLPlugin.queryDSLSettings
val current = project.in(file(".")).configs(QueryDSLPlugin.QueryDSL)
QueryDSLPlugin.queryDSLPackage := "models"
and build.properties
sbt.version=0.13.0

sbt not recognizing test

I am having difficulties getting sbt (version 0.12.1) to recognize any tests in src/test/scala.
I have tried both JUnit style tests and scalatest style tests but to no avial
To make things simple
I have moved my tests to the root package (src/test/scala)
I have included both org.scalatest and junit-interface in my build.sbt
libraryDependencies ++= List(
"org.scalatest" %% "scalatest" % "1.8" % "test",
"com.novocode" % "junit-interface" % "0.8" % "test->default"
)
I have made the tests as simple as possible:
scalatest example
import org.scalatest.FunSuite
import scala.collection.mutable.Stack
class ExampleSuite extends FunSuite {
test("math still works") {
assert(1+1 == 2)
}
}
junit test example:
import org.junit.Assert._
import org.junit.Test
class SimpleTest {
#Test
def testPass() {
assertEquals(1+1, 2)
}
}
my test structure is:
src/test/scala
├── FunSuiteExample.scala
└── SimpleTest.scala
What am I missing?
based on instructions at:
https://github.com/szeiger/junit-interface
modfied build.sbt
removed "junit" % "junit" % "4.10" % "test" from build.sbt
added "com.novocode" % "junit-interface" % "0.11" % "test"
put test in src/test/scala
import org.junit._
import org.junit.Assert._
class SimpleTeset {
#Test
def testTrue() {
assertEquals(1+1, 2)
}
}

runge kutta 4th order to solve system of differential equation

dT/dt=(1.344-1.025T)/h (1)
dh/dt=0.025-(3.5*10^-4)*sqrt(h) (2)
h(0)=1
T(0)=1
I have to solve this system of equations in fortran. I solved the problem in matlab but I dont know fortran programming so guys if somebody can help me or somebody have the fortran code for this help me please please please
thanks a lot
Try it with Euler integration. Do something simple first. You have one advantage: you've solved this once, so you know what the answer looks like when you get it.
Since the moderators are insisting this is a low quality answer because of the short length, I'll provide a working one in Java that should spark some thoughts for you. I used the Apache Commons math library; it has several different ODE integration schemes, including Euler and Runge Kutta.
I ran this on a Windows 7 machine using JDK 8. You can switch between Euler and Runge-Kutta using the command line:
package math.ode;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.MaxCountExceededException;
import org.apache.commons.math3.ode.FirstOrderDifferentialEquations;
import org.apache.commons.math3.ode.FirstOrderIntegrator;
import org.apache.commons.math3.ode.nonstiff.ClassicalRungeKuttaIntegrator;
import org.apache.commons.math3.ode.nonstiff.EulerIntegrator;
/**
* IntegrationExample solves coupled ODEs using Euler and Runge Kutta
* Created by Michael
* Creation date 12/20/2015.
* #link https://stackoverflow.com/questions/20065521/dependencies-for-jama-in-maven
*/
public class IntegrationExample {
public static final double DEFAULT_STEP_SIZE = 0.001;
private static final double DEFAULT_MAX_TIME = 2.0;
public static void main(String[] args) {
// Problem set up
double step = (args.length > 0) ? Double.valueOf(args[0]) : DEFAULT_STEP_SIZE;
double maxTime = (args.length > 1) ? Double.valueOf(args[1]) : DEFAULT_MAX_TIME;
String integratorName = (args.length > 2) ? args[2] : "euler";
// Choose different integration schemes here.
FirstOrderIntegrator firstOrderIntegrator = getFirstOrderIntegrator(step, integratorName);
// Equations to solve here; see class below
FirstOrderDifferentialEquations odes = new CoupledOdes();
double [] y = ((CoupledOdes) odes).getInitialConditions();
double t = 0.0;
int i = 0;
while (t <= maxTime) {
System.out.println(String.format("%5d %10.6f %10.6f %10.6f", i, t, y[0], y[1]));
firstOrderIntegrator.integrate(odes, t, y, t+step, y);
t += step;
++i;
}
}
private static FirstOrderIntegrator getFirstOrderIntegrator(double step, String integratorName) {
FirstOrderIntegrator firstOrderIntegrator;
if ("runge-kutta".equalsIgnoreCase(integratorName)) {
firstOrderIntegrator = new ClassicalRungeKuttaIntegrator(step);
} else {
firstOrderIntegrator = new EulerIntegrator(step);
}
return firstOrderIntegrator;
}
}
class CoupledOdes implements FirstOrderDifferentialEquations {
public double [] getInitialConditions() {
return new double [] { 1.0, 1.0 };
}
#Override
public int getDimension() {
return 2;
}
#Override
public void computeDerivatives(double t, double[] y, double[] yDot) throws MaxCountExceededException, DimensionMismatchException {
yDot[0] = (1.344-1.025*y[0])/y[1];
yDot[1] = 0.025-3.5e-4*Math.sqrt(y[1]);
}
}
You didn't say how far out you needed to integrate in time, so I assumed 2.0 as the max time. You can change this on the command line, too.
Here's the plot of results versus time from Excel. As you can see, the responses are smooth and well behaved. Euler has no problem with systems of equations like this.