0

Using groovy, I need to test a Java method. For example Foo.needsTesting(); Near the top of the needsTesting method is a call that sets an Object using a static call to a Factory method. For example ...

class Foo {
    public static String needsTesting() {
       FooStructure data = FooFactory.getData(); // Static Java method, does not work in groovy test bed environment ...
       ... more code ...
       return answer;
    }
}

Is there a way in groovy, without touching the source for either Foo.java or FooFactory.java or FooStructure.java to interception the call FooFactory.getData() so as to test with desired data?

  • independent of groovy or similars... you can not override static methods in java – ΦXocę 웃 Пepeúpa ツ Oct 26 '17 at 18:24
  • Not possible, except via reflection, which actually changes the class definition. This is the main reason cited to avoid all-static classes: they don't play well with testing frameworks. Also, see [this thread](https://stackoverflow.com/questions/576918/how-do-i-intercept-a-method-invocation-with-standard-java-features-no-aspectj-e). – Ted Hopp Oct 26 '17 at 18:26
  • 1
    I appreciate the comments. Saves time looking for a solution that probably does not exist. – John Diller Oct 26 '17 at 18:46
  • Possible duplicate re: Spock - https://stackoverflow.com/questions/37513161 – Michael Easter Oct 26 '17 at 19:04

0 Answers0