Question

I am implementing a custom authentication provider which has to authenticate and authorize user (through one time token/password). There is a server already in place for this purpose, and we have been provided a client API to use in our application for making calls to this authentication/authorization server.

We are allowed to replicate a test setup of that authentication server in our test environments for commercial reasons, hence we have to somehow mock this client API when we deploy to our environments whereas use an actual impl when we deliver to customer. I am stuck creating a clean way of injecting this client API as a bean to my spring application

the client API provided by them is essentially a single package JAR with a public class (no interfaces) and some public static methods to use, there is no interface I can use to inject this client API or a dummy implementation.

this is what I was planning to do

create an interface

public interface ClientAPIWrapper {

}

and then inject diff implementations of it

i.e.

public class DummyClientApiBean implements ClientAPIWrapper

and

public class ClientApiBean extends ClientAPI implements ClientAPIWrapper

but then I realized injecting as type of this interface will not allow accessing the methods of original client API class

@Autowired
private ClientAPIWrapper clientApiBean

what I want to achieve is to either use the original Client API class as a bean or a dummy bean returning dummy data transparently to the classes using either of the implementations.

something like

public MyAuthenticationProvider

@Autowired
private ClientAPI clientApi

how to achieve this?

Was it helpful?

Solution

The problem here is that the API consists of nothing but static methods (if I've understood your question correctly). The cleanest solution (but still ugly) is to write an interface for the API AND a facade class with non-static methods that delegates to the real API. For mocking, create a second implementation of the interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top