I am running a method that returns a boolean. The method does not change any data. The first line returns false but then when I run the assertion test it says that it is returning null. Any thoughts?
System.out.println(fb.existsInNetwork(x)); // returns false
assertFalse(fb.existsInNetwork(x)); // junit.framework.AssertionFailedError: null
we know that if a method returns a boolean, it cannot return null. I suspect that instead you should interpret this as "Assertion failed, and the message is: null". Try instead calling the version of assertFalse which takes a message:
assertFalse("didn't expect x to be in network", fb.existsInNetwork(x))
Related
I got XSL file which i'm trying to generate over my app.
The code goes like this:
TransformerFactory factory = TransformerFactory.newInstance();
Source styleSheetLoc = new ResourceSource(styleSheetLocation);
Templates t = factory.newTemplates(styleSheetLoc); // <<< throwing the Exception
return t.newTransformer();
The exception is:
ERROR: 'Cannot find external method 'com.am.caretalks.util.XsltUtils.getResourceString' (must be public).'
FATAL ERROR: 'Cannot convert argument/return type in call to method 'com.am.caretalks.util.XsltUtils.getResourceString(node-set, node-set, int)''
javax.xml.transform.TransformerConfigurationException: Cannot convert argument/return type in call to method 'com.am.caretalks.util.XsltUtils.getResourceString(node-set, node-set, int)'
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:990)
at com.am.caretalks.sendreport.CreatePDF.buildTransformer(CreatePDF.java:1147)
at com.am.caretalks.sendreport.CreatePDF.createProviderEngagementPDF(CreatePDF.java:608)
at com.am.caretalks.sendreport.CreatePDF.createProviderEngagementPDF(CreatePDF.java:586)
at com.am.caretalks.admin.EngagementReportExportServiceImpl.exportAndUpload(EngagementReportExportServiceImpl.java:131)
at com.am.caretalks.admin.EngagementReportExportServiceImpl$$FastClassBySpringCGLIB$$38327504.invoke(<generated>)
EDIT:
This is the called function from the XSL file
public static String getResourceString(final String messageKey, final String locale, final String customerIndex) {
.
.
}
I searched the web and I found a solution that tells me to use only objects (not primitives) while calling to a function from my XSL file to my java code and due to that i'm getting these error, but nothing did helped me here.
The big issue here that I don't understand what is the error and how to solve it, what do I need to do according the thrown Exception?
Any other suggestions to fix this issue are welcome
The error message suggests that you are passing inappropriate arguments to the function. It appears the method expects (string, string, string) and you are passing (node-set, node-set, int). I don't know Xalan well, but try doing an explicit conversion of the supplied arguments to the required type by calling number() or string().
I'm having a very frustrating issue at the moment and perhaps the answer lies here?
I'm currently having an issue with if statements.
I want my core.java class to contain an if statement which closes the entire program if my variable counter reaches 2.
private int counter = 0;
//located in the class Ending
I implemented that using a seperate method addCounter()
which goes as
public void addCounter(){
this.counter ++;
}
//this will be called in core.java
I also have a getter which is supposed to return the value of counter
public int getCounter(){
return counter;
}
//this will be called in core.java
Decleration of changeState in core.java
Ending changeState = new Ending();
//(As per request)
The real issue is described here:
I can't seem to come up with a fitting if statement which checks if the method getCounter has reached '2' after addCounter();has been invoked several times
My first idea was to use something such as
if(changeState.getCounter().equals(2)){
System.exit(0);
}
//I also tried using:
if(changeState.getCounter() == 2)
//however, that didn't work either
both lines give me numerous errors which I can't wrap my head around:
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: <identifier> expected: if(changeState.getCounter().equals(2)){
.java:476: error: ';' expected: if(changeState.getCounter().equals(2)){
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: ';' expected: if(changeState.getCounter().equals(2)){
Could anyone elaborate on what is going wrong and what should be done to overcome this issue?
Thank you in advance!
C.C.
.equals(2) is incorrect , the 2 in the equals method is a primitive type int literal not an Object or String type.
.equals() method uses either a type "String"
counter.equals("2")
or it uses a type "object" to compare
.equals(((Object)new String("2")))
If you must use .equals() method then it would be
if(counter.getCounter().equals(new Integer(2).toString())){
System.exit(0);
}
Although that really should be simpler such as
if(counter.getCounter() == 2){
System.exit(0);
}
My answer lied here all along.
If anyone else is having similar kind of issue, it seems that you simply can't invoke an object inside a class unless it's in a method.
I admit that this does solve my issue entirely, but it did show me a valuable lesson.
Good luck!
When I call eval (in strict mode) on a nashorn engine with the following script I get an exception:
var yfunc = function () {
(null).apply(null, arguments);
};
yfunc();
I've truncated my personal situation heavily. The "(null)" on line 2 can be replaced with anything between parenthesis or a local variable, either way just something that shouldn't throw a compile error, and it will yield the same result.
The issue seems to be explicitly that "arguments" is passed directly as the second argument of calling a method called "apply". Any of the following changes will undo the thrown exception:
Putting "arguments" in a variable first (but simply wrapping it in parenthesis doesn't work!)
Calling something other than apply
Passing "arguments" in a different argument slot when calling apply
Calling print() (with or without passing any arguments) as a preceding line of code inside yfunc() (weird huh?)
Defining more than 0 parameters for yfunc()
Binding yfunc first and then calling the bound method
Calling yfunc via Function.apply (not so much with Function.call!)
The Exception thrown is this:
Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.runtime.Undefined to jdk.nashorn.internal.runtime.ScriptFunction
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:4)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
When I call this method with an owner, the exception thrown changes. Example code:
var yfunc = {
method: function () {
(null).apply(null, arguments);
}
};
var x = yfunc.method();
Then the thrown exception looks like this:
Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.scripts.JO4 to jdk.nashorn.internal.runtime.ScriptFunction
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:5)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
I've reproduced the issue so far on specifically these environments:
windows 7 64bit -> jdk 1.8.0_60 64bit
windows 8 64bit -> jdk 1.8.0_131 64bit
I can't seem to find anything on the internet about similar issues. Do I need to report this to Oracle/OpenJDK?
Minor update
Added items 6 and 7 to list of "following changes will undo the thrown exception".
Final update
Bug filed: JDK-8184720
Yes, it appears to be a bug. Please file a bug.
My code is like below
#Test
public void testMyMethod(){
MyClass mc = new MyClass();
String exeVal="sometext some text";
String x=mc.exampleMethod();
// Assertion type 1
Assert.assertEquals(exeVal,x);
//Assertion Type 2
Assert.assertTrue(exeVal.equals(x));
}
I want to know which is the best approach.
Type 1 is preferred because of the assertion message you will receive when they do not match.
org.junit.ComparisonFailure: expected: <[foo]> but was: <[bar]>
vs
java.lang.AssertionError
Type 1 is preferred because of the following reasons.
It gives you a better error message.
ex:- "expected [3] but found [4]" . The second approach will evaluate the condition and will give you a message like "expected [true] but found [false]" and the user will not get the values evaluated.
AssertEquals is null safe and assertTrue isn't
We have a legacy project and we are using jgroups-all 2.2.9.1 jar.
We are facing a issue from the past few days where our server crashes giving following
exception :
java.lang.IllegalArgumentException: timeout value is negative
at java.lang.Object.wait(Native Method)
at org.jgroups.protocols.ring.UdpRingNode.receiveToken(UdpRingNode.java:59)
at org.jgroups.protocols.TOTAL_TOKEN$TokenTransmitter.run(TOTAL_TOKEN.java:1116)
we think this is occuring due to the old jgroups jar we are using.but then if we upgrade the jar file there is another problem.The new jar has removed the sub package ring(org.jgroups.protocol.ring) from package protocol.
So my question is how should we proceed ?. if i will have to change the implementation of udpring then what should i use instead?.
The exception tells the entire story :
java.lang.IllegalArgumentException: timeout value is negative
There must be a call to method that expects a positive value for its argument and you may be giving a negative value to it, so it throws an exception.
Imagine I am having a java.util.Date object, and then I call a method to set the year as
java.util.Date d = new java.util.Date();
d.setYear(-123);
Then it may throw this kind of exception as I cannot specify negative value for year.
So just have check where your code is accessing the jar's code that throws this exception and make a check whether the parameter value passed are of correct value.