title: ClassCastException when using Mockito.thenAnswer
body: <p>I am new to Kotlin. I get exception when trying to use Mockito's <code>thenAnswer</code> method</p>
  <p>Controller:</p>
  <pre><code>@RestController
  class SampleRestController(
    val sampleService: SampleService
  ) {
  
    @PostMapping(value = [&quot;/sample-endpoint&quot;], consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
    fun sampleEndpoint(@RequestBody values: List&lt;String&gt;): ResponseEntity&lt;String&gt; {
      val response = sampleService.serviceCall(values)
      return ResponseEntity.status(HttpStatus.OK).body(response)
    }
  }
  </code></pre>
  <p>Service:</p>
  <pre><code>@Service
  @Transactional
  class SampleService {
  
    fun serviceCall(values: List&lt;String&gt;): String {
      return values.joinToString(&quot;&quot;)
    }
  
  }
  </code></pre>
  <p>Test:</p>
  <pre><code>@ExtendWith(MockitoExtension::class)
  internal class SampleRestControllerTest {
  
    @Mock
    private lateinit var sampleService: SampleService
  
    private lateinit var mockMvc: MockMvc
  
    private lateinit var objectMapper: ObjectMapper
  
    private lateinit var sampleRestController: SampleRestController
  
    @BeforeEach
    fun before() {
      MockitoAnnotations.initMocks(this)
      objectMapper = ObjectMapper()
        .registerModule(JavaTimeModule())
        .registerKotlinModule()
      sampleRestController = SampleRestController(sampleService)
      mockMvc = MockMvcBuilders.standaloneSetup(sampleRestController).build()
    }
  
    @Test
    fun doTest() {
      val testData = listOf(&quot;123&quot;, &quot;456&quot;)
      //Mockito.`when`(sampleService.serviceCall(testData)).thenReturn(&quot;123456&quot;)
      //Mockito.`when`(sampleService.serviceCall(testData)).thenAnswer { invocation -&gt; &quot;123456&quot; }
      Mockito.`when`(sampleService.serviceCall(testData)).thenAnswer { invocation -&gt; {
        val numbers = invocation.getArgument&lt;List&lt;String&gt;&gt;(0)
        if (&quot;123&quot; == numbers[0] &amp;&amp; &quot;456&quot; == numbers[1]) {
          &quot;123456&quot;
        } else {
          &quot;654321&quot;
        }
      } }
  
      val result: MvcResult = mockMvc.perform(
        MockMvcRequestBuilders.post(&quot;/sample-endpoint&quot;)
          .content(objectMapper.writeValueAsString(testData))
          .contentType(MediaType.APPLICATION_JSON_VALUE)
      )
        .andExpect(status().isOk)
        .andReturn()
  
      assertEquals(&quot;123456&quot;, result.response.contentAsString)
  
    }
  }
  </code></pre>
  <p>The unit test is working fine when using the <code>thenReturn()</code> and also when using <code>thenAnswer()</code> without any <code>if</code> condition.</p>
  <p>When I try to use <code>thenAnswer</code> with <code>if</code> condition then I get classCastException.</p>
  <p>Probably because Kotlin only accepts non-null value? How do I resolve this issue.</p>