Mockito error "The system cannot find the path specified" - mockito

Could some one help me how to mock this call?
Actual code
final ServicesLocal servicesBean = new WSDelegate().getServiceLocal();
here is my mock code
mockDelegate = mock(PortalDelegate.class);
PowerMockito.whenNew(PortalDelegate.class).withAnyArguments().thenReturn(mockPortalDelegate);

You have to add the when for the method invocation as well.
ServicesLocal mockServicesLocal = mock(ServicesLocal.class);
when(mockDelegate.getServiceLocal()).thenReturn(mockServicesLocal);

Related

Groovy's Path.traverse() extension, IntelliJ idea syntactic error

I am editing a Groovy (3.0.10) script and I am getting a syntactic error that I do not understand:
When I try this in GroovyConsole, it seems to work just fine.
Why is IntelliJ IDEA complaining? This is with IntelliJ IDEA 2022.1.1.
The snippet in text:
final java.nio.file.Path p;
p.traverse(type: FileType.FILES, nameFilter: ~/^\.deployment$/, maxDepth: 1) { final Path dotDeploymentPath ->
println dotDeploymentPath
}
UPDATE 1
I actually got the same error from Groovy when running the script in our product:
Script4.groovy: 59: [Static type checking] - Cannot call java.nio.file.Path#traverse(java.util.Map <java.lang.String, java.lang.Object>, groovy.lang.Closure) with arguments [java.util.LinkedHashMap <java.lang.String, java.io.Serializable>, groovy.lang.Closure]
# line 59, column 9.
extensionsPath.traverse(type: FileType.FILES, nameFilter: ~/^\.deployment$/, maxDepth: 1) { final Path dotDeploymentPath ->
^
UPDATE 2
I ended up with this which seems to work just fine. I still don't understand why Groovy does not like the options as arguments of the call.
def final traverseOptions = [type: FileType.FILES, nameFilter: ~/^\.deployment$/, maxDepth: 1] as Map<String, Object>;
extensionsPath.traverse(traverseOptions) { final Path dotDeploymentPath ->
So I figured this out. The issue was that I used #CompileStatic at the top of my class. I used #CompileStatic because I misunderstood what it does. I thought it were similar to saying static class in Java. But that is not what it does. What it does instead is this, from the docs: This will let the Groovy compiler use compile time checks in the style of Java then perform static compilation, thus bypassing the Groovy meta object protocol.
(This shows how important it is to provide self contained test case. I should have known better.)

Replace node text with Mule & groovy

I am trying to replace the text value of a node using a groovy scripting transformer in Mule.
I first use a XML to DOM transformer then I am using the following
<scripting:transformer name="replacePassword">
<scripting:script engine="Groovy"><![CDATA[import org.dom4j.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
Node passwordElement = message.payload.getRootElement().selectSingleNode("//*[local-name()=\'Password\']")
passwordElement.setTextContent("xxxxxxxxxxxxx")
return message.payload
]]></scripting:script>
</scripting:transformer>
But this seems to be throwing the following exception
Root Exception stack trace: groovy.lang.MissingMethodException: No
signature of method: org.dom4j.tree.DefaultElement.setTextContent() is
applicable for argument types: (java.lan g.String) values:
[xxxxxxxxxxxxx]
I am not too familiar with groovy is my script correct?
Thanks
just look at the documentation for org.dom4j.tree.DefaultElement
there is no setTextContent() method... but there is a setText() method
Went with this in the end, this will get a node and replace the text value
<scripting:transformer name="replacePassword">
<scripting:script engine="Groovy"><![CDATA[
node = message.payload.getRootElement().selectSingleNode('//*[local-name()=\'Password\'][1]');
node.text = 'xxxxxxx';
return message.payload;]]></scripting:script>
</scripting:transformer>

What does it mean by _._ in Groovy/Spock

I have seen _._ in the source code of a few Spock Specs. Does anyone know what that means?
For example,
def "test something"() {
given:
someClass = Mock(SomeClass)
and:
1 * someClass.someMethod() >> returnSomething
0 * _._
when:
String str = someClass.someMethod().toString()
then:
str == 'returnedValue'
}
_ is like a wildcard.
_.someMethod()
Means the method someMethod called on any mocked object, likewise
myDomain._
Means anything called on myDomain.
And following that logic _._ means anything called on any mock object. This can be useful if you don't want any more methods called:
0 * _._
Extra Reading
You should read the docs for some more information on the kinds of things _ can be used for.

Set function return type in Haxe

For some reason, I get this error message whenever I try to compile this simple function: Test.hx:1: lines 1-7 : Invalid -main : Test has invalid main function
public static function main(a:Int, b:Int){
trace("Calling main function");
return a+b;
}
I'm not sure why this is happening. What's wrong with this function definition, and how can I get it to compile correctly? I tried reading the documentation, and found it to be unclear in its explanation of how to properly set function return types.
The special main entry function must be a Void->Void function. i.e. No param and no return value is allowed. Remember there is no command line argument concept in JS/Flash, which Haxe also compiles to. So we have to use system targets' API for that:
Sys.args() : Array<String> to get the command line params.
Sys.exit( code : Int ) : Void to exit with exit code.
FYI, the doc of Sys is at http://haxe.org/api/sys

Python 3 C-API IO and File Execution

I am having some serious trouble getting a Python 2 based C++ engine to work in Python3. I know the whole IO stack has changed, but everything I seem to try just ends up in failure. Below is the pre-code (Python2) and post code (Python3). I am hoping someone can help me figure out what I'm doing wrong.I am also using boost::python to control the references.
The program is supposed to load a Python Object into memory via a map and then upon using the run function it then finds the file loaded in memory and runs it. I based my code off an example from the delta3d python manager, where they load in a file and run it immediately. I have not seen anything equivalent in Python3.
Python2 Code Begins here:
// what this does is first calls the Python C-API to load the file, then pass the returned
// PyObject* into handle, which takes reference and sets it as a boost::python::object.
// this takes care of all future referencing and dereferencing.
try{
bp::object file_object(bp::handle<>(PyFile_FromString(fullPath(filename), "r" )));
loaded_files_.insert(std::make_pair(std::string(fullPath(filename)), file_object));
}
catch(...)
{
getExceptionFromPy();
}
Next I load the file from the std::map and attempt to execute it:
bp::object loaded_file = getLoadedFile(filename);
try
{
PyRun_SimpleFile( PyFile_AsFile( loaded_file.ptr()), fullPath(filename) );
}
catch(...)
{
getExceptionFromPy();
}
Python3 Code Begins here: This is what I have so far based off some suggestions here... SO Question
Load:
PyObject *ioMod, *opened_file, *fd_obj;
ioMod = PyImport_ImportModule("io");
opened_file = PyObject_CallMethod(ioMod, "open", "ss", fullPath(filename), "r");
bp::handle<> h_open(opened_file);
bp::object file_obj(h_open);
loaded_files_.insert(std::make_pair(std::string(fullPath(filename)), file_obj));
Run:
bp::object loaded_file = getLoadedFile(filename);
int fd = PyObject_AsFileDescriptor(loaded_file.ptr());
PyObject* fileObj = PyFile_FromFd(fd,fullPath(filename),"r",-1,"", "\n","", 0);
FILE* f_open = _fdopen(fd,"r");
PyRun_SimpleFile( f_open, fullPath(filename) );
Lastly, the general state of the program at this point is the file gets loaded in as TextIOWrapper and in the Run: section the fd that is returned is always 3 and for some reason _fdopen can never open the FILE which means I can't do something like PyRun_SimpleFile. The error itself is a debug ASSERTION on _fdopen. Is there a better way to do all this I really appreciate any help.
If you want to see the full program of the Python2 version it's on Github
So this question was pretty hard to understand and I'm sorry, but I found out my old code wasn't quite working as I expected. Here's what I wanted the code to do. Load the python file into memory, store it into a map and then at a later date execute that code in memory. I accomplished this a bit differently than I expected, but it makes a lot of sense now.
Open the file using ifstream, see the code below
Convert the char into a boost::python::str
Execute the boost::python::str with boost::python::exec
Profit ???
Step 1)
vector<char> input;
ifstream file(fullPath(filename), ios::in);
if (!file.is_open())
{
// set our error message here
setCantFindFileError();
input.push_back('\0');
return input;
}
file >> std::noskipws;
copy(istream_iterator<char>(file), istream_iterator<char>(), back_inserter(input));
input.push_back('\n');
input.push_back('\0');
Step 2)
bp::str file_str(string(&input[0]));
loaded_files_.insert(std::make_pair(std::string(fullPath(filename)), file_str));
Step 3)
bp::str loaded_file = getLoadedFile(filename);
// Retrieve the main module
bp::object main = bp::import("__main__");
// Retrieve the main module's namespace
bp::object global(main.attr("__dict__"));
bp::exec(loaded_file, global, global);
Full Code is located on github:

Resources