Skip to content

Mocking creates uncompilable code for Kotlin, due improper mockedConstruction type #1278

Description

@tyuldashev

Description

When user tries to use mocking for external classes UTBot creates var mockedConstruction = null which usage in the code cause type compatibility errors

To Reproduce

Steps to reproduce the behavior:

  1. In UTBot settings set Generated test language to Kotlin other leave default
  2. Enable Force mocking static methods
  3. Generate tests for following code
importjava.util.RandomclassRandomInt {
funmultByRandom(x:Int): Int {
val random:Random=Random();
return x * random.nextInt();
}
}

Expected behavior

Compilable code is generated

Actual behavior

Generated test cannot be compiled. See its beginning below.

 @Test
funtestMultByRandom1() {
var mockedConstruction =nulltry {
val mockClassCounter =AtomicInteger()
mockedConstruction = mockConstruction(Random::class.java) { RandomMock:Random,
context:MockedConstruction.Context->when (mockClassCounter.get()) {
0-> {
(`when`(RandomMock.nextInt())).thenReturn(0)
}
}
mockClassCounter.getAndIncrement()
}

Visual proofs (screenshots, logs, images)

image

Metadata

Metadata

Labels

ctg-bugIssue is a buglang-kotlinIssue is related to Kotlin language support

Type

No type

Projects

Status
Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions