io.github.cweijan.mock.jupiter.MockInstanceHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http-test Show documentation
Show all versions of http-test Show documentation
test springmvc controller by api
package io.github.cweijan.mock.jupiter;
import io.github.cweijan.mock.Mocker;
import io.github.cweijan.mock.context.HttpMockContext;
import java.util.HashMap;
import java.util.Map;
/**
* @author cweijan
* @since 2020/05/25 18:17
*/
public class MockInstanceHolder {
private final HttpMockContext context;
private final Map instanceMap;
public MockInstanceHolder(String scheme, String host, Integer port) {
this.context =new HttpMockContext(scheme, host, port);
this.instanceMap=new HashMap<>();
}
@SuppressWarnings("unchecked")
public T getInstance(Class instanceClass){
return (T) instanceMap.computeIfAbsent(
instanceClass.getSimpleName()+"_"+ context.getScheme()+"_"+context.getHost()+"_"+context.getPort(),
key -> Mocker.api(instanceClass, context)
);
}
}