Mockito set private property - properties

I have a small problem with Mockito. Let's say I have this code :
MyObject object = mock(MyObject.class);
System.out.println("PROPERTY 1 BRUT VALUE : " + property1Value);
object.setProperty1(property1Value);
System.out.println("PROPERTY 1 VALUE BEFORE STUB : " + object.getProperty1());
when(object.getProperty1()).thenReturn(property1Value);
System.out.println("PROPERTY 1 VALUE AFTER STUB : " + object.getProperty1());
PROPERTY 1 BRUT VALUE : 150.0
PROPERTY 1 VALUE BEFORE STUB : 0.0
PROPERTY 1 VALUE AFTER STUB : 150.0
The problem is that my object property is not set directly. I have a method in MyObject that uses it and it always return 0. It is not possible to set directly an object property if it is mocked ?
Found nothing on the documentation on this point.
Thanks.
EDIT 1 : Here is the method in MyObject that uses the property set by Mockito
public Double getProperty1X2() {
return 2 * property1;
}
This method returns 0 in my tests, even if I set the property value. Maybe because it is using the field directly and not the getter ?

You can check my answer on this discussion:-
Is it possible to invoke mocked object's method?
The key point for you is:-
when(object.getProperty1()).thenCallRealMethod();

Related

Class field with different argument types (setter overloading)

I have a class with a field of type long and I would like to pass either an Int or a Long value.
So I thought I can make a second setter with the same name, but different argument.
Kotlin does not complain and I can even call both setters from Java (same name, one automatically created with long from Kotlin). In Java I just call setMyNumber(long or int) value and the compiler will assign the correct method.
But why can't I do myNumber = 4 in Kotlin, why does it not call the other setter?
Is there a different way I can achieve this functionality, but still keep the property notation (yes I know I can write to setter methods, but then I have to call them with a method call rather just assigning a value)?
class MyClass {
var myNumber: Long = 0L // internal setMyNumber(value: Long)
fun setMyNumber(newNumber: Int) {
myNumber = newNumber.toLong()
}
}
As of writing, what you're trying to do is not supported. (See: Allow setters overloading for properties)
A workaround would be using the Superclass for all platform classes representing numeric values:
class MyClass {
var myNumber: Number = 0L
set (value) { field = value.toLong() }
}
val myClass = MyClass()
val anInt: Int = 1
val aLong: Long = 1L
myClass.myNumber = anInt
myClass.myNumber = aLong
Try it online!

'never' properties cannot be set inside the counstructor

So, the question is in the title. I declared some properties in my classes using 'never' keyword so I may set the values of these properties only once, in the constructor. However, I get the following error:
Cannot access field or identifier %name% for writing
Example of the problematic code:
class TreeAbility
{
public var id(default, never):String;
public var maxLvl(default, never):Int;
public function new(id:String, maxLvl:Int)
{
Assert.assert(maxLvl > 0);
this.id = id; (*)
this.maxLvl = maxLvl; (*)
this.currentLvl = 0;
}
}
The lines marked with (*) throw the access error
I believe the never write property means that writing/setting the variable is never allowed, not even within the constructor. See: https://haxe.org/manual/class-field-property.html
Perhaps you are looking for the final keyword, which is coming in Haxe 4. For instance fields, it allows assignment to the variable only from the class constructor. Confirmed here: https://haxe.org/download/version/4.0.0-preview.2/ and https://github.com/HaxeFoundation/haxe/issues/6584

Setter overloading in Kotlin

When trying to define a setter that accepts a parameter type that can be used to construct a property, thusly:
class Buffer(buf: String) {}
class Foo {
var buffer: Buffer? = null
set(value: String) {
field = Buffer(value)
}
}
I get the error message:
Setter parameter type must be equal to the type of the property
So what's meant to be the Kotlin way of doing this?
As of Kotlin 1.1 it is not possible to overload property setters. The feature request is tracked here:
https://youtrack.jetbrains.com/issue/KT-4075
Currently, you would have to define a buffer extension function on String:
val String.buffer : Buffer
get() = Buffer(this)
and set the value with
Foo().buffer = "123".buffer

Accessing Unknown 'ID' class method-Objective C

RS232MsgGetEventDescriptions.h:
#define DECLARE_RS232_NEWMSG(ClassID)\
enum \
{ \
ID = ClassID \
}; \
#interface RS232MsgGetEventDescriptions : RS232Msg
{
}
#end
RS232MsgGetEventDescriptions.m
#implementation RS232MsgGetEventDescriptions
DECLARE_RS232_NEWMSG(RM_GET_EVENT_DESCRIPTIONS);
#end
EventLogs.m
-(void)event
{
service = [CServiceAppDlg alloc];
if ([service:(REMOTE_MESSAGE_ID)RS232MsgGetEventDescriptions.ID withEvent:pEvent])
{
NSLog(#"Get Event descriptions!!");
}
}
I'm getting an error like "Accessing Unknown 'ID' class method"
I should not modify the definition here.How to pass the ID.I am going to call different descriptions ID in the same way so is this declaration of ID.
The reason why you are getting a Accessing Unknown 'ID' class method error message is because you have not declared a method called ID in your class RS232MsgGetEventDescriptions.
When you say RS232MsgGetEventDescriptions.ID in your code, you are calling the property ID of object RS232MsgGetEventDescriptions, which is equivalent to [RS232MsgGetEventDescriptions ID]. However, RS232MsgGetEventDescriptions is not an object, but a class and you don't have a class method called + (REMOTE_MESSAGE_ID)ID in your class specification (you don't have it declared on the interface or implemented on the class implementation).
I would also like to point out that it is bad practice to use the dot notation for something other than a property. Since classes cannot have #properties (these are for objects) you should call this method using standard Objective-C messaging notation [RS232MsgGetEventDescriptions ID].
Xcode will still allow you to write object.methodName to call methods with no parameters, and object.methodName = value for methods that take 1 parameter. Because they are interpreted as follows:
object.methodName; // Becomes [object methodName]
object.methodName = value; // Becomes [object setMethodName:value]

How can I keep -Xcheckinit from interfering with the deserialization of Scala objects?

When using the -Xcheckinit compiler option and implementing my own readObject method in a serializable class, I can't call any accessor functions on fields declared in the body of my class from the readObject method. Fields declared as constructor arguments are ok. When I do try to access a field declared in the class body, I get a scala.UninitializedFieldError.
That is, the following code fails on println(y) in the readObject method, even after y has been set in the previous line!
#serializable case class XYPointWithRWAndPrint(var x: Int) {
var y = 0
#throws(classOf[java.io.IOException])
private def writeObject(out: java.io.ObjectOutputStream) {
out.writeInt(x)
out.writeInt(y)
}
#throws(classOf[java.io.IOException])
#throws(classOf[ClassNotFoundException])
private def readObject(in: java.io.ObjectInputStream) {
x = in.readInt()
println(x)
y = in.readInt()
println(y)
}
}
Why?
When using the -Xcheckinit compiler option, the compiler creates a bitmap field that it uses to check initialization.
public volatile int bitmap$0;
In the accessors, the compiler checks the bitmap:
public int y(){
if ((this.bitmap$0 & 0x1) != 0){ return this.y; }
throw new UninitializedFieldError("Uninitialized field: Test.scala: 2".toString());
}
In the constructor, the compiler updates the bitmap:
public XYPointWithRW(int x) {
Product.class.$init$(this);
this.y = 0;
this.bitmap$0 |= 1;
}
Note that it doesn't update the bitmap for constructor arguments, only for fields declared in the class body. It does this because it assumes that you will be calling the constructor and that those fields will be initialized immediately.
During deserialization however, the no-arg constructor of the first non-serializable super class is called (Object in this case), instead of the one-arg constructor shown above. Then readObject is called. The constructor above is never called. Therefore, the bitmap is never updated. Calling the accessor would fail anywhere its called, not just in the readObject method.
To work around this, you must update the bitmap manually. I've chosen to do so from the readObject method. I set all of the bits in the bitmap to 1, like so:
getClass.getField("bitmap$0").set(this, -1)
By setting all the bits to 1, it will work for all the fields (up to 32 fields anyway...what happens beyond that is anyones guess).