Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Clojure mock Java class

farenda 2016-04-26 0

Clojure mock Java class

In this tutorial we’re going to show how to use Clojure proxy to mock Java classes and provide own functionality for methods.

Proxy class with no-arg constructor

Calls superclass default constructor:

(def my-list
  (proxy [ArrayList] []
    (get [i] 42)))

Let’s use it:

user> (.get my-list 10)
42

No matching ctor found for class

Superclass has no no-arg constructor:

(proxy [FileInputStream] []
        (read []
          42))

Produces an exception, because there’s no no-arg constructor in FileInputStream class:

CompilerException java.lang.IllegalArgumentException: No matching ctor found for class user.proxy$java.io.FileInputStream$ff19274a

Proxy java class with constructor

We have to provide obligatory constructor parameter (here “/proc/version”), that will be passed to superclass:

;; To count calls and return predictive values:
(def counter (atom 0))

(def my-mock
  (proxy [FileInputStream] ["/proc/version"]
    (read []
      (swap! counter inc))))

Now our version of read method may be called as expected:

user> (.read my-mock)
1
user> (.read my-mock)
2
user> (.read my-mock)
3

Overload mocked method

(def counter (atom 0))

(def my-mock
  (proxy [FileInputStream] ["/proc/version"]
    (read
      ([] (swap! counter inc)) ; read()
      ([^bytes arr]            ; read(byte[])
       (aset-byte arr 0 42)
       (swap! counter inc)))))

read(byte[]) can be called like this:

user> (.read my-mock (byte-array 3))
1
user> (.read my-mock (byte-array 3))
2

References:

  • proxy macro
  • ClojureDocs – proxy
Share with the World!
Categories Clojure Tags clojure
Previous: Java Path comparison
Next: Java find relative path

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok