Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

kotlin测试mockito报错ArgumentMatchers.any must not be null

项目使用springboot+kotlin进行开发,项目代码如下:
IAccountService:

interface IAccountService : IService<AccountInfo> {
    fun simpleInfo(id: Long): AccountInfo?
    fun register(accountInfo: AccountInfo): Boolean
}

AccountServiceImpl

@Service
class AccountServiceImpl : IAccountService, ServiceImpl<AccountMapper, AccountInfo>() {
    override fun simpleInfo(id: Long): AccountInfo? {
        val simpleInfo = baseMapper.simpleInfo(id);
        return simpleInfo
    }
    override fun register(accountInfo: AccountInfo): Boolean {
        return baseMapper.insert(accountInfo) > 0
    }
}

AccountInfo

data class AccountInfo (
     var uid: Long? = null,
     // 用户名
     var username: String? = null,
     // 用户昵称
     var nickname: String? = null,
) {
}

测试代码如下:

@RunWith(SpringRunner::class)
@SpringBootTest
@AutoConfigureMockMvc
class AccountControllerTest {
     @Autowired
     lateinit var mockMvc: MockMvc
     @MockBean
     private lateinit var accountService: IAccountService
       
     @Test
     fun register() {
       `when`(accountService.simpleInfo(anyLong())).thenReturn(AccountInfo(100))
       val simpleInfo = accountService.simpleInfo(1L)
       `when`(accountService.register(any(AccountInfo::class.java))).thenReturn(true)
        accountService.register(AccountInfo(100))
     }
}

运行上边的测试,simpleInfo方法可以正常执行,但是当执行 register测试方法的第三行的when时,报以下错误:

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

因为any(AccountInfo::class.java)可能返回空,但是我的方法是非空的参数,所以无法匹配,找了好久终于找到了解决办法:

  1. 自定义MockitoHelper

     object MockitoHelper {
         fun <T> anyObject(): T {
             Mockito.any<T>()
             return uninitialized()
         }
         @Suppress("UNCHECKED_CAST")
        fun <T> uninitialized(): T =  null as T
     }

    使用MockitoHelper.anyObject() 来代替 Mockito.any()方法

  2. 引入mockito-kotlin类库
    导入import com.nhaarman.mockitokotlin2.any,使用这里的any()方法

我这里采用第二种方法,引入新的类库,最后的测试方法如下:

@RunWith(SpringRunner::class)
@SpringBootTest
@AutoConfigureMockMvc
class AccountControllerTest {
     @Autowired
     lateinit var mockMvc: MockMvc
     @MockBean
     private lateinit var accountService: IAccountService
       
     @Test
     fun register() {
       `when`(accountService.simpleInfo(anyLong())).thenReturn(AccountInfo(100))
       val simpleInfo = accountService.simpleInfo(1L)
       // 在这里改动any     
       //`when`(accountService.register(any(AccountInfo::class.java))).thenReturn(true)
       `when`(accountService.register(any())).thenReturn(true)
        accountService.register(AccountInfo(100))
     }
}

问题解决。

参考:

ArgumentMatchers.any must not be null


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...