How to mock out InetAddress.getLocalHost() using JMockit - jmockit

The InetAddress constructor is not visible because the factory pattern is used.
final InetAddress anyInstance = InetAddress.getLocalHost();
new NonStrictExpectations(InetAddress.class) {
{
anyInstance.getHostAddress();
result = "192.168.0.101";
}
};
When I try to use the factory method to get an instance for partial mocking I get the error:
java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter

You need to specify that InetAddress and any subclasses should be mocked:
#Test
public void mockAnyInetAddress(#Capturing final InetAddress anyInstance)
throws Exception
{
new Expectations() {{
anyInstance.getHostAddress(); result = "192.168.0.101";
}};
String localHostAddress = InetAddress.getLocalHost().getHostAddress();
assertEquals("192.168.0.101", localHostAddress);
}

Related

JUnit 5 Parameterized test #ArgumentsSource parameters not loading

I have created below JUnit5 parameterized test with ArgumentsSource for loading arguments for the test:
public class DemoModelValidationTest {
public ParamsProvider paramsProvider;
public DemoModelValidationTest () {
try {
paramsProvider = new ParamsProvider();
}
catch (Exception iaex) {
}
}
#ParameterizedTest
#ArgumentsSource(ParamsProvider.class)
void testAllConfigurations(int configIndex, String a) throws Exception {
paramsProvider.executeSimulation(configIndex);
}
}
and the ParamsProvider class looks like below:
public class ParamsProvider implements ArgumentsProvider {
public static final String modelPath = System.getProperty("user.dir") + File.separator + "demoModels";
YAMLDeserializer deserializedYAML;
MetaModelToValidationModel converter;
ValidationRunner runner;
List<Configuration> configurationList;
List<Arguments> listOfArguments;
public ParamsProvider() throws Exception {
configurationList = new ArrayList<>();
listOfArguments = new LinkedList<>();
deserializedYAML = new YAMLDeserializer(modelPath);
deserializedYAML.load();
converter = new MetaModelToValidationModel(deserializedYAML);
runner = converter.convert();
configurationList = runner.getConfigurations();
for (int i = 0; i < configurationList.size(); i++) {
listOfArguments.add(Arguments.of(i, configurationList.get(i).getName()));
}
}
public void executeSimulation(int configListIndex) throws Exception {
final Configuration config = runner.getConfigurations().get(configListIndex);
runner.run(config);
runner.getReporter().consolePrintReport();
}
#Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return listOfArguments.stream().map(Arguments::of);
// return Stream.of(Arguments.of(0, "Actuator Power"), Arguments.of(1, "Error Logging"));
}}
In the provideArguments() method, the commented out code is working fine, but the first line of code
listOfArguments.stream().map(Arguments::of)
is returning the following error:
org.junit.platform.commons.PreconditionViolationException: Configuration error: You must configure at least one set of arguments for this #ParameterizedTest
I am not sure whether I am having a casting problem for the stream in provideArguments() method, but I guess it somehow cannot map the elements of listOfArguments to the stream, which can finally take the form like below:
Stream.of(Arguments.of(0, "Actuator Power"), Arguments.of(1, "Error Logging"))
Am I missing a proper stream mapping of listOfArguments?
provideArguments(…) is called before your test is invoked.
Your ParamsProvider class is instantiated by JUnit. Whatever you’re doing in desiralizeAndCreateValidationRunnerInstance should be done in the ParamsProvider constructor.
Also you’re already wrapping the values fro deserialised configurations to Arguments and you’re double wrapping them in providesArguments.
Do this:
#Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return listOfArguments.stream();
}}

ByteBuddy intercepting constructor arguments

I am trying to dynamically create a class using ByteBuddy with my custom constructor.
I have read the Intercepting default constructor with Byte Buddy and I have written the following code base on that.
Class<?> dynamicType = new ByteBuddy().subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS)
.name("foo").defineConstructor(Modifier.PUBLIC).withParameters(int.class)
.intercept(
to(new Object() {
public void construct() throws Exception {
System.out.println("before constructor");
}
})
.andThen(MethodCall.invoke(Object.class.getConstructor()))
.andThen(to(new Object() {
public void construct() throws Exception {
System.out.println("after constructor");
}})
))
.make()
.load(Main.class.getClassLoader(), INJECTION)
.getLoaded();
dynamicType.getConstructor(int.class).newInstance(3);
My question is how can I access the integer argument of 'foo' constructor in the custom codes that I have added before and after calling the super constructor.
Sure, simply define a parameter with an annotation #Argument(0).
I'd recommend against using anonymous classes as their package-private visibility might render tricky outcomes.

ByteBuddy: AbstractMethodError when set interceptor

The following is my learning code, when I executing the code, an exception happened:
Exception in thread "main"
java.lang.AbstractMethodError: org.learning.UserRepository$ByteBuddy$etz0xUhc.$$_pharos_set_interceptor(Lorg/learning/Interceptor;)V**
ByteBuddy version: 1.10.14
final TypeCache.SimpleKey cacheKey = getCacheKey(learningClazz, interceptor.getClass());
Class proxyClass = load(learningClazz, proxyCache, cacheKey, byteBuddy ->
byteBuddy
.subclass(learningClazz)
.defineField(ProxyConfiguration.INTERCEPTOR_FIELD_NAME, Interceptor.class, Visibility.PRIVATE)
.method(not(isDeclaredBy(Object.class)))
.intercept(MethodDelegation.to(ProxyConfiguration.InterceptorDispatcher.class))
.implement(ProxyConfiguration.class)
.intercept(FieldAccessor.ofField(ProxyConfiguration.INTERCEPTOR_FIELD_NAME)
.withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC)));
final ProxyConfiguration proxy = (ProxyConfiguration) proxyClass.getDeclaredConstructor().newInstance();
proxy.$$_pharos_set_interceptor(interceptor);
return (T) proxy;
public interface ProxyConfiguration {
String INTERCEPTOR_FIELD_NAME = "$$_pharos_interceptor";
void $$_pharos_set_interceptor(Interceptor interceptor);
class InterceptorDispatcher {
#RuntimeType
public static Object intercept(
#This final Object instance,
#Origin final Method method,
#AllArguments final Object[] arguments,
#StubValue final Object stubValue,
#FieldValue(INTERCEPTOR_FIELD_NAME) Interceptor interceptor,
#SuperMethod Method superMethod
) throws Throwable
{
if (interceptor == null) {
return stubValue;
}
else {
return interceptor.intercept(instance, method, arguments, superMethod);
}
}
}
}
Package-private methods are overriden but the JVM will not dispatch them dynamically if the subclass is loaded on a different class loader. If you declare the method public, the problem should be solved. Alternatively, inject the class into the target class loader.

jmockit - Mocking chain of methods one of which returns a Collection using #Cascading

I am trying to mock a method call which goes something like this:
rapContext.getSysInfo().get(key)
The getSysInfo() method returns a ConcurrentHashMap.
Here is what I have done:
Class ABCTest {
#Cascading RapContext context;
#Test
doTest() {
new Expectations() {
{
rapContext.getSysInfo().get(anyString);
result = new UserPrefCtxObject();
}
}
}
With this I get a NullPointerException on rapContext.getSysInfo(). Call to getSysInfo() returns null. If I call any other method which does not return a collection, for instance rapContext.getDomain() everything working fine.
I am not sure what I am missing.
Thanks
The code example is not complete however you are likely running into some issue associated with accidentally mocking Map. If a Map (or any part of the Collection framework) is mocked then a lot of things will break. I could not reproduce your problem as any attempt to mock RapContext using #Cascading resulted in a stack over flow.
You could partially mock RapContext instead and then either return a real or mocked Map. When I run into similar issues I generally get around them using either #Injectable to only mock an instance of a class or using partial mocks.
Here is an approach that will let you mock getSysInfo:
public class RapContextTest {
#Injectable ConcurrentHashMap<String, Object> mockedMap;
#Test
public void testContext() {
RapContext context = new RapContext();
new MockUp<RapContext>(){
#Mock
public ConcurrentHashMap getSysInfo(){
return mockedMap;
}
};
new NonStrictExpectations() {
{
mockedMap.get(anyString);
result = "Success";
}
};
Object value = context.getSysInfo().get("test");
System.out.println(value);
}
}

Error while resolving type because of constructor?

I get an error:
Resolution of the dependency failed, type = "MyAppApp.ServiceAgents.IMyAppServiceAgent", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Int32 cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:
Resolving MyAppApp.ServiceAgents.MyAppServiceAgent,(none) (mapped from MyAppApp.ServiceAgents.IMyAppServiceAgent, (none))
Resolving parameter "AuthHandlerId" of constructor MyAppApp.ServiceAgents.MyAppServiceAgent(System.Int32 AuthHandlerId, System.String AuthSessionGuid, System.ServiceModel.EndpointAddress ServiceEndPointAddress)
Resolving System.Int32,(none)
in the method which is below:
internal ServiceLocator()
{
services = new Dictionary<object, object>();
// fill the map
this.services.Add(typeof(IMyAppServiceAgent), _container.Resolve<IMyAppServiceAgent>());
}
This is how I call this method
I have a standard method in the ViewModelLocator (from MVVM Light Toolkit) method
public static void CreateShowroomLog()
{
if (_showroomLog == null)
{
_showroomLog = new ShowroomLogViewModel(ServiceLocator.Instance(_container).GetService<IMyAppServiceAgent>());
}
}
and constructor is
public ViewModelLocator()
{
_container=new UnityContainer();
_container.RegisterType<IMyAppServiceAgent, MyAppServiceAgent>();
}
The class of which I need an instance is:
protected static EndpointAddress ServiceEndPointAddress
{
get { return (App.Current as App).ServiceEndpointAddr; }
}
protected static string AuthSessionGuid
{
get { return (App.Current as App).W2OGuid; }
}
protected static int AuthHandlerId
{
get { return (App.Current as App).OriginalHandlerId; }
}
public MyAppServiceAgent(int AuthHandlerId, string AuthSessionGuid, System.ServiceModel.EndpointAddress ServiceEndPointAddress)
{
_proxy = new MyAppService.Service1Client(new BasicHttpMessageInspectorBinding(new SilverlightAuthMessageInspector(AuthHandlerId.ToString(), AuthSessionGuid)), ServiceEndPointAddress);
}
public MyAppServiceAgent()
: this(AuthHandlerId, AuthSessionGuid, ServiceEndPointAddress)
{
}
How can I resolve this problem with cosntructor?
When you register your type you didn't specify which constructor to call on MyAppServiceAgent. By default Unity will choose the constructor with the most parameters but you didn't specify how those parameters should be resolved.
You could try this and see if it will cause the the default constructor (paramaterless) of MyAppServiceAgent to be called when this type is resolved..
_container=new UnityContainer();
_container.RegisterType<IMyAppServiceAgent, MyAppServiceAgent>(new InjectionConstructor());
What I think would be even better is to remove the ServiceEndPointAddress, AuthSessionGuid and AuthHandlerId static properties from your MyAppServiceAgent class. Then register the type like this
_container=new UnityContainer();
_container.RegisterType<IMyAppServiceAgent, MyAppServiceAgent>(
new InjectionConstructor(
(App.Current as App).OriginalHandlerId,
(App.Current as App).W2OGuid,
(App.Current as App).ServiceEndpointAddr
));
Which should cause this constructor to be called.
public MyAppServiceAgent(int AuthHandlerId, string AuthSessionGuid, System.ServiceModel.EndpointAddress ServiceEndPointAddress)
{
_proxy = new MyAppService.Service1Client(new BasicHttpMessageInspectorBinding(new SilverlightAuthMessageInspector(AuthHandlerId.ToString(), AuthSessionGuid)), ServiceEndPointAddress);
}
That way your MyAppServiceAgent class is not dependent on the App class.