Coder Social home page Coder Social logo

Comments (5)

jkim323 avatar jkim323 commented on June 15, 2024

Although I am unfamiliar to the the Thales JCPROV library, I am a bit curious on this issue. I am wondering if you have tried checking if your class is missing in your class path by simply trying to use the static method before mocking it because this has happened to me once before. But based on your issue since you are work involves with native code maybe mocking the native methods is not the best route because I have heard of challenges on doing this. Have you tried exploring doing something like encapsulating it in a wrapper class or in an interface?

from mockito.

scordio avatar scordio commented on June 15, 2024

Yes, the library is in the classpath: if I call one of the methods without mocking, it fails in the static initialization block with a missing native library error, as expected.

Also yes, wrapping works fine as a workaround. In addition, there is another class (still part of the library) that offers similar static methods, wrapping each native call with exception throwing in case of errors, and mocking this class works as expected.

from mockito.

jkim323 avatar jkim323 commented on June 15, 2024

Interesting. Does the other successful class also share the same characteristics as the class you are having problems with or are there differences? I am wondering if two classes share the last bullet you mentioned in the post. Also, have you explored using PowerMock perhaps?

from mockito.

scordio avatar scordio commented on June 15, 2024

No, they are not the same. The successful one:

  • Is also public, top level and non-final
  • Has also default constructor that is private and does nothing
  • Has public methods that are all static but not native
  • Has no static initialization block (i.e., no load of a native library)

Imagine something like:

public class NativeClass {

  private NativeClass() {}

  public static native int method();

  static {
    System.loadLibrary("native");
  }

}

public class WrapperClass {

  private WrapperClass() {}

  public static int method() {
    int result = NativeClass.method();
    if (result != 0) throw new RuntimeException(result);
  }

}

(written without checking the original code – beware of typos 🙂 )

from mockito.

jkim323 avatar jkim323 commented on June 15, 2024

After reading your difference , it makes sense to why you are seeing it fail because it seems like a common known issue that Mockito is unable to mock native methods; it is currently a limitation on Mockito as of now. Perhaps you can try PowerMock as an alternative solution but I am not aware of a definitive solution.. unless we develop on this maybe creating a discussion on this topic might help.

from mockito.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.