GithubHelp home page GithubHelp logo

Comments (10)

BrianNichols avatar BrianNichols commented on August 19, 2024 1

We are planning to add IAerospikeClient and IAsyncClient interfaces that should be more mock friendly. This will be available in the next release.

from aerospike-client-java.

vkorenev avatar vkorenev commented on August 19, 2024

+1

from aerospike-client-java.

BrianNichols avatar BrianNichols commented on August 19, 2024

The AerospikeClient methods are declared final because it allows the compiler to perform optimizations where method overrides don't have to be checked. You can still inherit from AerospikeClient and add functionality, but the underlying methods do not need to change. It's a reasonable design pattern that is unfortunately hostile to many (but not all) mock frameworks.

from aerospike-client-java.

sandonjacobs avatar sandonjacobs commented on August 19, 2024

Thanks for the reply.

I assume you mean using something like PowerMock with the expectNew functionality to mock the behavior of the execute method on subclasses of Command (like DeleteCommand, WriteCommand, etc..). Not a huge fan of that method, but I digress.

I am curious as to if you have metrics supporting the "optimizations" you speak of.

Thanks again...

from aerospike-client-java.

traviskaufman avatar traviskaufman commented on August 19, 2024

+1. We're having an extremely difficult time working with the client in our tests for this exact same reason. Perhaps all of the methods could be declared in an interface and then actually implemented as final methods in an implementation class? That way tools like mockito and scalamock could use the interface but in the production code you'd get the benefit of the optimized final methods from the implementation class.

from aerospike-client-java.

kennethjor avatar kennethjor commented on August 19, 2024

FWIW in our infrastructure we use a wrapper class which handles talking to Aerospike and decoding everything to the correct objects. Having this class means for unit testing we can simply cut out the Aerospike library altogether. That might be a workaround until an interface is available.

from aerospike-client-java.

sandonjacobs avatar sandonjacobs commented on August 19, 2024

We did the same thing, putting a layer of abstraction between our code and the Aerospike client to better accommodate testing. Once the next release is available it seems will may be able to rip this out.

from aerospike-client-java.

traviskaufman avatar traviskaufman commented on August 19, 2024

same here, but we also wanted to test the wrapper itself, so we still needed to figure out how to mock Aerospike.

Since we're using scala, we actually wound up creating a type class containing the Aerospike client methods we needed, and had our Aerospike service require that

object ASClientLike {
  implicit def aerospikeClientIsASClientLike(as: AerospikeClient): ASClientLike = new ASClientLike {
    def get(policy: Policy, key: Key): Record = as.get(policy, key)
  }
}

trait ASClientLike {
  def get(policy: Policy, key: Key): Record
}

object AerospikeService {
  import ASClientLike._

  def apply(client: ASClientLike, namespace: String)(implicit ec: ExecutionContext): AerospikeService =
    new AerospikeServiceImpl(client, namespace)(ec)

  private class AerospikeServiceImpl(client: ASClientLike, namespace: String)(implicit val ec: ExecutionContext) extends AerospikeService
}

trait AerospikeService {
  def client: ASClientLike

  def namespace: String

  def get[T](setName: String, keyName: KeyName, binName: String): Future[Option[T]] = Future {
    val key = new Key(namespace, setName, keyName)
    Option(client.get(null, key)) flatMap {
      rec => Option(rec.getValue(binName)) map { _.asInstanceOf[T] }
    }
  }
}

So now we can use it in production

import ASClientLike._

val client = new AerospikeClient(policy, clusterNodes: _*)
val aerospike = AerospikeService(client, "namespace")

// ...

as well as in tests (we use scalamock for mocking)

class AerospikeServiceSpec extends WordSpec 
    with Matchers with OneInstancePerTest MockFactory {
  val fakeClient = mock[ASClientLike]
  val service = AerospikeService(fakeClient, "namespace")

  // ...
}

It's a lot of boilerplate, but that also allows us to keep our implementation super flexible without altering any public APIs

from aerospike-client-java.

BrianNichols avatar BrianNichols commented on August 19, 2024

Java client 3.0.34 has been released which includes IAerospikeClient and IAsyncClient.

from aerospike-client-java.

maverickgautam avatar maverickgautam commented on August 19, 2024

I have made a aerospike-unit module, which one can use while writing unit, Integration test cases . (Java Project)

This is a Java wrapper over actual Aerospike Server which can be used for unit + Integration Testing .

Open Source CodeBase: https://github.com/maverickgautam/Aerospike-unit

Readme available at https://github.com/maverickgautam/Aerospike-unit/blob/master/README
Hope Aerospike community finds this useful .

from aerospike-client-java.

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.