Установите значение для атрибута, не устанавливающего атрибут, в имитируемом суперклассеJAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Установите значение для атрибута, не устанавливающего атрибут, в имитируемом суперклассе

Сообщение Гость »


Я работаю над смешанным проектом JVM: классы в основном написаны на Java, а тесты — на Kotlin.
Итак, у меня есть Java which extends a

Код: Выделить всё

ParentClass
. Both are Jakarta . They are separated since

Код: Выделить всё

ParentClass
can be used in other places:

Код: Выделить всё

@Entity
@Setter
@Getter
public class MyClass extends ParentClass {
// some attributes
}
And

Код: Выделить всё

ParentClass
has attributes only with getter and no setters (is mandatory to not modify these values since the object is created):

Код: Выделить всё

@MappedSuperclass
@Getter
public class ParentClass {
private String value = "";
}
Now I'm testing some parts of the code and trying to mock this object to update value to a non null value.
Since there are no , the idea is to create mock to be able to modify the

Код: Выделить всё

getValue()
and return other value avoiding reflection.
I've tried to use something like

Код: Выделить всё

mock.when(myMock.getValue()).thenReturn("my string")
but it doesn't works.
In kotlin tests I've tried multiple lines:

Код: Выделить всё

val myMock = mock(CALLS_REAL_METHODS)

`when`(myMock.value).thenReturn("")
// or using mockito kotlin
whenever(myMock.value).thenReturn("")
First line is trying to force to call the real getter and then mock the getter in some of the next two lines and get desired value in that way.
But it seems that not work's in that way.
Also I've tried this answer

Код: Выделить всё

val myMock = mock()
// And also
val myMock = org.mockito.kotlin.mock()

// And these two
`when`(myMock.value).thenReturn("")
whenever(myMock.value).thenReturn("")
None of them works, I still have the "empty" object created for mockito with all values null/objects default.
Also tried:

Код: Выделить всё

val mock1: MyClass = mock(MyClass::class.java)
`when`(mock1.value).thenReturn("")
whenever(mock1.value).thenReturn("")

val mock2: MyClass = mock();
`when`(mock2.value).thenReturn("")
whenever(mock2.value).thenReturn("")

val mock3: MyClass = mock(CALLS_REAL_METHODS)
`when`(mock3.value).thenReturn("")
whenever(mock3.value).thenReturn("")
None of them works, my mock class has default values. But not other is that problem but also I want to modify the parent class which is not mocked.
So, what I'm missing? Is any inter-op problem using Java and Kotlin? Is some or

Код: Выделить всё

@MappedSuperclass
weird thing?
Do I will need to use reflection?
Thanks in advance,


Источник: https://stackoverflow.com/questions/781 ... uper-class
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»