How to mock an array containing objects using mockito? - arrays

In my Mockito unit test I am trying to mock an array containing instances of the object Message. To do this I try to mock it like normal objects so like:
private var messagesMock = mock(Array<Message>::class.java)
This gives the following error/exception:
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class [Lrobot.fsrt.robotguest.common.data.Message;
Mockito cannot mock/spy because :
- VM does not not support modification of given type
How to mock an array the right way using Mockito?

A distinct non-answer: you (almost) never mock objects that represent containers!
An array is just that: a container.
You create the container with the size you need, and then you put your mocked objects into that ordinary container, and make sure that container with your prepared content gets used by your production code.
It is as simple as that: you don't mock arrays, or lists, or maps: you create them as is, and manipulate their content!

Related

Jmeter Property with array of values

Requirement: Need to store 50+ values to a Jmeter property and use with idx
In the case of normal variable we can use Country_1 or Country_2.
Do we have any function to set an array of values to jmeter Property and how to get value using index?
Note: In this case,value has to be used in different thread group.
Your ArrayList initialization is not correct, you should be doing something like:
List myList = Arrays.asList('India', 'USA', 'UK')
There is no putObject method in props shorthand (which is basically an instance of java.util.Properties class so you will need to amend your code like:
props.put('Middle', myList)
Once done you will be able to access individual list members using __groovy() function like:
${__groovy(props.get('Middle').get(0),)} - for first member
${__groovy(props.get('Middle').get(1),)} - for second member
${__groovy(props.get('Middle').get(2),)} - for third member
etc.
Demo:
See Apache Groovy - Why and How You Should Use It article for more details on using Groovy scripting in JMeter tests.

I am getting this error when executing Testng in selenium

Data Provider public java.lang.Object[] as.get() must return either Object[][] or Iterator<Object>[], not class java.lang.Object;
As the documentation says:
The Data Provider method can return one of the following two types:
An array of array of objects (Object[][]) where the first dimension's
size is the number of times the test method will be invoked and the
second dimension size contains an array of objects that must be
compatible with the parameter types of the test method. This is the
cast illustrated by the example above.
An Iterator<Object[]>. The only
difference with Object[][] is that an Iterator lets you create your
test data lazily. TestNG will invoke the iterator and then the test
method with the parameters returned by this iterator one by one. This
is particularly useful if you have a lot of parameter sets to pass to
the method and you don't want to create all of them upfront.
So, I suppose you current data provider method is returning Object instead of one of the 2 supported types.

How can I populate an instance of an object with random values?

Is there any way I can give AutoFixture an instance of an object and have it go through all the setters and set random data? The wiki examples only show how to obtain an instance from AutoFixture, e.g.
var autoGeneratedClass = fixture.Create<ComplexParent>();
My example use case is a factory method which generate instances of objects with dynamic properties based on a configuration. I want to test that my methods correctly, detect and interact (e.g. copy) these dynamic properties.
dynamic dynamicPropertyObject1 = factoryMethod(configuration);
dynamic dynamicPropertyObject2 = factoryMethod(configuration);
dynamicPropertyObject1.propA = random.Next();
dynamicPropertyObject1.CopyTo(dynamicPropertyObject2);
Assert.That(dynamicPropertyObject2.propA, Is.EqualTo(dynamicPropertyObject1.propA);
Thanks
AutoFixture has a lot of built-in heuristics for creating objects, including some for factory methods.
If AutoFixture finds no public constructors on a type, it starts to look for factory methods; i.e. static methods that return objects of the type of the class that defines that static method, e.g.
public class Foo
{
public static Foo CreateFoo();
// ... other members
}
If, on the other hand, a factory method exists on another class, you'll need to help AutoFixture a bit. The easiest way would be to use the Customize method:
fixture.Customize<Foo>(c => c
.FromFactory(() => FooFactory.CreateFoo())
.WithAutoProperties());
When you subsequently ask a Fixture object for a Foo object, FooFactory.CreateFoo() will be invoked, and because of WithAutoProperties that object will be populated with data created by AutoFixture.

How to access a main document class array from a movieclip?

I have an array in my main
public var graphArray:Array = [1,2,3,4,5,6];
And I'm trying to access it from within a MovieClip that I've put on my timeline using:
var graph1scale:Number = MovieClip(root).graphArray[0]
It looks like it would make sense to me but when I try to run it I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Am I wrong to be using MovieClip(root) to try and access it? I've only just started using external classes (this is my first project doing so) and usually I just do everything on the timeline. So MovieClip(root) is familiar to me but I guess it's not the right thing to do here.
Is there a way I can access vars from Main.as?
-----SOLVED-----
I realised MovieClip(root) did work all along but I was just calling on the array before the array was being defined in Main.as. I put a delay on calling graphArray and it worked.
Not sure how that makes sense though because the graphArray is the first thing I've defined in the whole main.as class
Try using this instead
MovieClip(this.root)
This works for me on a test that you can see here:
http://marksost.com/test/as3arrayaccess/
And the source files here:
http://marksost.com/test/as3arrayaccess/test.zip

Resolving array types in a Unity (Prism) container

Is it possible to register and resolve array types in a Unity container? I'd like to do something like this:
this.mContainer
.RegisterType<ISomeType, SomeType>()
.RegisterType<ISomeType[], SomeType[]>();
ISomeType[] lSomeTypes = this.mContainer.Resolve<ISomeType[6]>();
It would be even better if I didn't have to register the array type, and have Unity figure out the array based on RegisterType<ISomeType, SomeType>() and Resolve<ISomeType[]>() alone.
If you register multiple types for a particular type (using named registrations), then when the container sees a dependency on an array of that type, it'll automatically inject all the named registrations.
So this will work:
this.mContainer
.RegisterType<ISomeType, SomeImpl1>("one")
.RegisterType<ISomeType, SomeOtherImpl>("other")
.RegisterType,ISomeType, AnotherImpl>("another");
ISomeType[] someTypes = mContainer.Resolve<ISomeType[]>();
This logic will kick in whenever there's a dependency of ISomeType[] - constructor parameter, injected property, etc.
Note that the array injection will only inject named registrations. The default, unnamed registration is not included in the array.

Resources