I'm writing unit tests for a web app and using the Java Play! (2.1.3) framework's FakeApplication class.
public class TagTest {
public static FakeApplication app;
...
// BeforeClass only runs once before any tests are run
#BeforeClass
public static void setUpBeforeClass() {
// Set up new FakeApplication before running any tests
app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
Helpers.start(app);
}
...
}
I have the same setUpBeforeClass() method in all four of my test classes that test each of my four different models but when I run the play test command, all four test classes return a similar error:
[error] Test models.TagTest failed: java.lang.NullPointerException: null
[error] at Global.onStart(Global.java:59)
[error] at play.core.j.JavaGlobalSettingsAdapter.onStart(JavaGlobalSettingsAdapter.scala:17)
[error] at play.api.GlobalPlugin.onStart(GlobalSettings.scala:175)
[error] at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:68)
[error] at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:68)
[error] at scala.collection.immutable.List.foreach(List.scala:309)
[error] at play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:68)
[error] at play.api.Play$$anonfun$start$1.apply(Play.scala:68)
[error] at play.api.Play$$anonfun$start$1.apply(Play.scala:68)
[error] at play.utils.Threads$.withContextClassLoader(Threads.scala:18)
[error] at play.api.Play$.start(Play.scala:67)
[error] at play.api.Play.start(Play.scala)
[error] at play.test.Helpers.start(Helpers.java:354)
[error] at models.TagTest.setUpBeforeClass(TagTest.java:35)
Where line 35 is the line:
Helpers.start(app);
Is there something I'm doing wrong here?
Looks from the stack like the NPE is thrown on line 59 of your own Global class - inside your override of GlobalSettings.onStart()?
Look/debug there to see what the issue is. Hard to suggest any more without seeing your code - maybe check this out for a basic intro to Global and how it can be used.
By the way - just a couple of additional points
Why is app a static variable? It should probably be an instance variable of TagTest.
You didn't have to point out what code was on line 35 of TagTest - this can be seen on the second-last line of the stacktrace. The first line of the trace shows you the method in which the Exception was actually thrown - the rest just show the stack of calls which led up to that method call.
Related
I am currently migrating a project to JDK 11 and compiling it with Maven. However, Maven throws a wobbly over a single method reference (it doesn't have issues elsewhere). The method in question looks like this:
public class MyThing {
boolean something = true;
// ...
public boolean isSomething() {
return something;
}
// ...
}
And the call to the above method looks more or less like this:
return methodThatGetsOptionalDefinition(aString) // Optional<Definition>
.map(definition -> defenition.getMyThing(anotherString)) // Optional<MyThing>
.map(MyThing::isSomething) // Optional<Boolean>
.orElse(true);
Compiling this, Maven throws the following message:
> mvn clean install -Pdist-snapshot -DskipTests
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project XXXXX: Compilation failure
[ERROR] /D:/XXXXX.java:[63,38] incompatible types: invalid method reference
[ERROR] method isSomething in class XXXXX.MyThing cannot be applied to given types
[ERROR] required: no arguments
[ERROR] found: java.lang.Object
[ERROR] reason: actual and formal argument lists differ in length
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
If I expand the method reference like so, it compiles without a problem:
return methodThatGetsOptionalDefinition(aString)
.map(definition -> defenition.getMyThing(anotherString))
.map(myThing -> myThing.isSomething())
.orElse(true);
Typing the Optional also works:
return methodThatGetsOptionalDefinition(aString)
.<MyThing>map(definition -> defenition.getMyThing(anotherString))
.map(myThing -> myThing.isSomething())
.orElse(true);
The same error occurs when compiling the project using IntelliJ with both Open JDK versions 11.0.1 and 11.0.2. Ironically, IntelliJ complains that the "Lambda can be replaced with method reference". The problem also occurs when using a different "language mode".
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
Does anyone have any idea why this might be happening?
Some of the specific implementation details have been obfuscated.
It has to do with the Type definition of the map method.
In order to provide a method reference, the method must have a matching Type definition. Stream map has a function method, but yours is a producer function.
Function = (T) -> R
Producer = () -> R
So, Method::isSomething() is not compatible with Stream.map, but your inline closure myThing -> myThing.isSomething() is..
This question already has answers here:
Why does a Try/Catch block create new variable scope?
(5 answers)
Closed 6 years ago.
I wasn't able to get apache commons cli to work.
apache commons-cli
I have the most simple start:
This is the only class.
Resources are added with maven(commandline).
import org.apache.commons.cli.*;
public class App {
public static void main(String[] args) {
// create Options object
Options options = new Options();
CommandLineParser parser = new DefaultParser();
// add t option
options.addOption("t", false, "display current time");
try{
CommandLine cmd = parser.parse( options, args);
}catch(ParseExeption ex){
}
if(cmd.hasOption("t")) {
// print the date and time
}else {
// print the date
}
}
}
No matter what I tried. I get the "cannot find symbol".
This is the last part of the error:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
/java/com/mkyong/core/utils/App.java:[27,8] cannot find symbol
symbol: class ParseExeption
location: class com.mkyong.core.utils.App
[ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
/java/com/mkyong/core/utils/App.java:[31,4] cannot find symbol
symbol: variable cmd
location: class com.mkyong.core.utils.App
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO]
-------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO]
-------------------------------------------------------------------
[INFO] Total time: 0.842 s
[INFO] Finished at: 2016-10-31T12:17:29+01:00
[INFO] Final Memory: 15M/309M
[INFO]
---------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-
compiler-plugin:3.1:compile (default-compile) on project
dateUtils2: Compilation failure: Compilation failure:
[ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
/java/com/mkyong/core/utils/App.java:[27,8] cannot find symbol
[ERROR] symbol: class ParseExeption
[ERROR] location: class com.mkyong.core.utils.App
[ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
/java/com/mkyong/core/utils/App.java:[31,4] cannot find symbol
[ERROR] symbol: variable cmd
[ERROR] location: class com.mkyong.core.utils.App
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven
with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug
logging.
[ERROR]
[ERROR] For more information about the errors and possible
solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN
/MojoFailureException
Please help me to start with the commons-cli.
This is compiled with the maven compiler.
thank you.
Simply replace ParseExeption with ParseExceptionand move the if/else block in the same code block as where you defined your variable cmd otherwise it won't be visible, for example as next:
public static void main(String[] args) throws ParseException{
// create Options object
Options options = new Options();
CommandLineParser parser = new DefaultParser();
// add t option
options.addOption("t", false, "display current time");
CommandLine cmd = parser.parse( options, args);
if(cmd.hasOption("t")) {
// print the date and time
}else {
// print the date
}
}
In my current project, i have this code:
for(Annotation annotation : field.getAnnotations()) {
String package = annotation.annotationType().getPackage().getName();
if(package.equals("com.loja.annotations.input_type"))
input.setAttribute("type", annotation.annotationType().getSimpleName());
}
when i try build the project, this code causes a compilation failure due to this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project store: Compilation failure: Compilation failure:
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[37,11] not a statement
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[37,17] ';' expected
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[38,14] illegal start of expression
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[38,21] illegal start of expression
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[38,28] ';' expected
[ERROR] /c:/Users/Kleber Mota/Documents/GitHub/app/src/main/java/com/loja/thymeleaf/processor/form/InputProcessor.java:[38,22] variable declaration not allowed here
anyone can see what's wrong here?
line 37 is: String package = annotation.annotationType().getPackage().getName(); and line 38 is if(package.equals("com.loja.annotations.input_type"))
you can try this..
annotation.annotationType().getName();
You can print out your "package" variable after line 37 (before if).
I wrote and ran the similar code and it worked for me. I created a custom annotation in my api package and line 37 returned "api".
The simple code snippet speaks for itself.
[error] Form<User> userForm = Form.form(User.class).bindFromRequest();
[error] ^
[error] symbol: method form()
[error] location: class Form
I've checked documentation : http://www.playframework.com/documentation/2.1.0/JavaForms
Doesn't understand what's going on...
The documentation is clear about that :
http://www.playframework.com/documentation/2.1.0/Migration
and the Java API is clear too :
http://www.playframework.com/documentation/api/2.1.0/java/play/data/Form.html#form(java.lang.Class)
Form<User> userForm = form(User.class);
OR
DynamicForm requestData = form().bindFromRequest();
Shifting from play 2.0.x to 2.1.x gives error for above code.
solution:
play.data.Form.* is a static import.
use import static play.data.Form.*; in your file & error will get resolved.
The form method has been moved in version 2.1.
Check that you are actually using play.data.Form.form() (it was previsously in play.mvc.Controller.Form if I remember well).
If you did change that, try a play clean clean-all and re-run a compilation. It should work then...
I have added the simple code write to a file into an open source project (Saiku) that before my changes builds and compiles cleanly.
The program compiles cleanly in Centos 5.3 Eclipse (no red X's).
However, when rerunning the maven build script, the compiler errors are generated (Exhibit 1):
Does Maven compile java projects differently from Eclipse?
Exhibit 1:
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[67,8] illegal start of type
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[67,11] ';' expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[70,14] <identifier> expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[70,15] illegal start of type
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[71,14] <identifier> expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[73,14] <identifier> expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[75,11] illegal start of type
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[79,2] invalid method declaration; return type required
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[130,15] class, interface, or enum expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[130,43] class, interface, or enum expected
[ERROR]
[ERROR] /usr/local/.m2/repository/saiku/saiku-core/saiku-service/src/main/java/org/saiku/olap/util/formatter/HierarchicalCellSetFormatter.java:[131,8] class, interface, or enum expected
[ERROR]
Exhibit 2:
import java.io.*; to the import section
try {
FileWriter fstream = new FileWriter("/usr/local/dailycandy/biserver.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.flush();
//Close the output stream
out.close();
}
catch (Exception e)
{
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
The code that you have added is not within a method/constructor/block etc. This is not valid Java, and thus does not compile.
You need to surround this with something like follows:
public void doSomething
{
// Insert code here
}
Alternatively, you can place it in an existing method, or constructor, depending on when you need this code to execute.
Why Eclipse is not highlighting this error is beyond me. It would normally report this. Try refreshing your project or cleaning/rebuilding and you should see that it will fail to compile.