How to use Matchers class of org.mockito.kotlin package

Best Mockito-kotlin code snippet using org.mockito.kotlin.Matchers

TestUserService.kt

Source:TestUserService.kt Github

copy

Full Screen

...7import org.junit.jupiter.api.Assertions.assertArrayEquals8import org.junit.jupiter.api.BeforeEach9import org.junit.jupiter.api.Test10import org.junit.jupiter.api.extension.ExtendWith11import org.mockito.ArgumentMatchers12import org.mockito.Mock13import org.mockito.Mockito14import org.mockito.junit.jupiter.MockitoExtension15import org.mockito.kotlin.any16import org.mockito.kotlin.eq17import org.mockito.kotlin.verify18import org.mockito.kotlin.whenever19import java.util.*20import java.util.stream.IntStream21import kotlin.test.assertEquals22import kotlin.test.assertNotNull23import kotlin.test.assertNull24@ExtendWith(MockitoExtension::class)25class TestUserService(26 @Mock val userRepository: UserRepository27) {28 private lateinit var userService: UserService29 @BeforeEach30 fun setUp() {31 userService = UserService(userRepository)32 }33 @Test34 fun `Test addUser method - username exists`() {35 val u = User(1, "", "", "test", "test", true, "test", "test")36 whenever(userRepository.findByUsername(ArgumentMatchers.anyString())).thenReturn(listOf(u))37 val addUser = userService.addUser(u)38 verify(userRepository, Mockito.times(1)).findByUsername(any())39 verify(userRepository, Mockito.times(0)).insert(eq(u))40 assertNull(addUser)41 }42 @Test43 fun `Test addUser method - userRepository returns null`() {44 val u = User(1, "", "", "test", "test", true, "test", "test")45 whenever(userRepository.findByUsername(ArgumentMatchers.anyString())).thenReturn(null)46 Assertions.assertThrows(NullPointerException::class.java) {userService.addUser(u)}47 verify(userRepository, Mockito.times(1)).findByUsername(any())48 verify(userRepository, Mockito.times(0)).insert(eq(u))49 }50 @Test51 fun `Test addUser method - insert successful`() {52 val u = User(1, "", "", "test", "test", true, "test", "test")53 whenever(userRepository.findByUsername(any())).thenReturn(listOf())54 whenever(userRepository.insert(eq(u))).thenReturn(u)55 val addUser = userService.addUser(u)56 verify(userRepository, Mockito.times(1)).findByUsername(any())57 verify(userRepository, Mockito.times(1)).insert(eq(u))58 assertEquals(u, addUser)59 }60 @Test61 fun `Test addUser method - insert unsuccessful`() {62 val u = User(1, "", "", "test", "test", true, "test", "test")63 whenever(userRepository.findByUsername(any())).thenReturn(listOf(u))64 whenever(userRepository.insert(eq(u))).thenReturn(null)65 val addUser = userService.addUser(u)66 verify(userRepository, Mockito.times(1)).findByUsername(any())67 verify(userRepository, Mockito.times(0)).insert(eq(u))68 assertNull(addUser)69 }70 @Test71 fun `Test getUser method - user doesn't exists`() {72 whenever(userRepository.findById(any())).thenReturn(Optional.empty())73 val getUser = userService.getUser(1)74 verify(userRepository, Mockito.times(1)).findById(any())75 assertNull(getUser)76 }77 @Test78 fun `Test getUser method - user exists`() {79 val u = User(1, "", "", "test", "test", true, "test", "test")80 whenever(userRepository.findById(any())).thenReturn(Optional.of(u))81 val getUser = userService.getUser(1)82 verify(userRepository, Mockito.times(1)).findById(any())83 assertNotNull(getUser)84 assertEquals(u, getUser)85 }86 @Test87 fun `Test getUsers method - user exists`() {88 val u = User(1, "", "", "test", "test", true, "test", "test")89 val users = IntStream.range(1, 5).mapToObj { u }.toList()90 whenever(userRepository.findAll()).thenReturn(users)91 val getUsers = userService.getUsers()92 verify(userRepository, Mockito.times(1)).findAll()93 assertNotNull(getUsers)94 assertArrayEquals(users.toTypedArray(), getUsers.toTypedArray())95 }96 @Test97 fun `Test login method - username doesn't exists`() {98 val u = User(1, "", "", "test", "test", true, "test", "test")99 whenever(userRepository.findByUsername(ArgumentMatchers.anyString())).thenReturn(listOf())100 val userModel = userService.login("test", "test")101 verify(userRepository, Mockito.times(1)).findByUsername(any())102 assertNull(userModel)103 }104 @Test105 fun `Test login method - wrong password`() {106 whenever(userRepository.findByUsername(ArgumentMatchers.anyString())).thenReturn(107 listOf(108 User(1, "", "", "test", "test2", true, "test", "test"),109 User(2, "", "", "test2", "test2", true, "test2", "test2")110 )111 )112 val userModel = userService.login("test", "test")113 verify(userRepository, Mockito.times(1)).findByUsername(any())114 assertNull(userModel)115 }116 @Test117 fun `Test login method - isOnline false`() {118 whenever(userRepository.findByUsername(ArgumentMatchers.anyString())).thenReturn(119 listOf(120 User(1, "", "", "test", "test2", true, "test", "test"),121 User(2, "", "", "test2", "test2", false, "test2", "test2")122 )123 )124 val userModel = userService.login("test", "test2")125 verify(userRepository, Mockito.times(1)).findByUsername(any())126 assertNotNull(userModel)127 assertEquals(userModel.username, "test")128 }129 @Test130 fun `Test login method - saving with error`() {131 whenever(userRepository.findByUsername(ArgumentMatchers.anyString())).thenReturn(132 listOf(133 User(1, "", "", "test", "test", true, "test", "test"),134 User(2, "", "", "test2", "test2", true, "test2", "test2")135 )136 )137 whenever(userRepository.save(ArgumentMatchers.any(User::class.java))).thenThrow(RuntimeException())138 Assertions.assertThrows(RuntimeException::class.java) { userService.login("test", "test") }139 verify(userRepository, Mockito.times(1)).findByUsername(any())140 verify(userRepository, Mockito.times(1)).save(any())141 }142 @Test143 fun `Test login method - throws exception`() {144 whenever(userRepository.findByUsername(ArgumentMatchers.anyString())).thenThrow(MongoClientException(""))145 whenever(userRepository.save(ArgumentMatchers.any(User::class.java))).thenThrow(RuntimeException())146 Assertions.assertThrows(MongoClientException::class.java) { userService.login("test", "test") }147 verify(userRepository, Mockito.times(1)).findByUsername(any())148 verify(userRepository, Mockito.times(0)).save(any())149 }150 @Test151 fun `Test login method - saving successful`() {152 whenever(userRepository.findByUsername(ArgumentMatchers.anyString())).thenReturn(153 listOf(154 User(1, "", "", "test", "test", true, "test", "test"),155 User(2, "", "", "test2", "test2", true, "test2", "test2")156 )157 )158 whenever(userRepository.save(ArgumentMatchers.any(User::class.java))).thenReturn(null)159 val user = userService.login("test", "test")160 verify(userRepository, Mockito.times(1)).findByUsername(any())161 verify(userRepository, Mockito.times(1)).save(any())162 assertNotNull(user)163 assertEquals(164 user, UserUserModel(165 User(1, "", "", "test", "test", true, user.lastOnline, "test")166 )167 )168 }169 @Test170 fun `Test getOnlineUsers method`() {171 whenever(userRepository.findAll()).thenReturn(172 listOf(...

Full Screen

Full Screen

StatusResultMatchersExtensionsTests.kt

Source:StatusResultMatchersExtensionsTests.kt Github

copy

Full Screen

...23import org.mockito.Answers24import org.mockito.Mock25import org.mockito.junit.MockitoJUnitRunner26@RunWith(MockitoJUnitRunner::class)27class StatusResultMatchersExtensionsTests {28 @Mock(answer = Answers.RETURNS_MOCKS)29 lateinit var matchers: StatusResultMatchers30 @Test31 fun `StatusResultMatchers#is with Matcher parameter is called as expected when using isEqualTo extension`() {32 val matcher = mock<Matcher<Int>>()33 matchers.isEqualTo(matcher)34 verify(matchers, times(1)).`is`(matcher)35 }36 @Test37 fun `StatusResultMatchers#is with int parameter is called as expected when using isEqualTo extension`() {38 matchers.isEqualTo(200)39 verify(matchers, times(1)).`is`(200)40 }41}...

Full Screen

Full Screen

Matchers

Using AI Code Generation

copy

Full Screen

1val mock = mock<SomeClass>()2whenever(mock.someFunction(any())).thenReturn(1)3verify(mock).someFunction(argThat { it > 0 })4val mock = mock<SomeClass>()5whenever(mock.someFunction(any())).thenReturn(1)6verify(mock).someFunction(argThat { it > 0 })7val mock = mock<SomeClass>()8whenever(mock.someFunction(any())).thenReturn(1)9verify(mock).someFunction(argThat { it > 0 })10val mock = mock<SomeClass>()11whenever(mock.someFunction(any())).thenReturn(1)12verify(mock).someFunction(argThat { it > 0 })13val mock = mock<SomeClass>()14whenever(mock.someFunction(any())).thenReturn(1)15verify(mock).someFunction(argThat { it > 0 })16val mock = mock<SomeClass>()17whenever(mock.someFunction(any())).thenReturn(1)18verify(mock).someFunction(argThat { it > 0 })19val mock = mock<SomeClass>()20whenever(mock.someFunction(any())).thenReturn(1)21verify(mock).someFunction(argThat { it > 0 })22val mock = mock<SomeClass>()23whenever(mock.someFunction(any())).thenReturn(1)24verify(mock).someFunction(argThat { it > 0 })25val mock = mock<SomeClass>()26whenever(mock.someFunction(any())).thenReturn(1)27verify(mock).someFunction(argThat { it > 0 })28val mock = mock<SomeClass>()29whenever(mock.someFunction(any())).thenReturn(1)30verify(mock).someFunction(argThat { it > 0 })31val mock = mock<SomeClass>()32whenever(mock.someFunction(any())).thenReturn(1)33verify(mock).someFunction(argThat { it > 0 })34val mock = mock<SomeClass>()35whenever(mock.someFunction(any())).thenReturn(1)36verify(mock).someFunction(arg

Full Screen

Full Screen

Matchers

Using AI Code Generation

copy

Full Screen

1val mock = mock<SomeClass>()2every { mock.someMethod(any()) } returns "Hello World"3val result = mock.someMethod("some arg")4verify { mock.someMethod("some arg") }5assertThat(result).isEqualTo("Hello World")6val mock = mock<SomeClass>()7`when`(mock.someMethod(any())).thenReturn("Hello World")8val result = mock.someMethod("some arg")9verify(mock).someMethod("some arg")10assertThat(result).isEqualTo("Hello World")11val mock = mock<SomeClass>()12every { mock.someMethod(any()) } returns "Hello World"13val result = mock.someMethod("some arg")14verify { mock.someMethod("some arg") }15assertThat(result).isEqualTo("Hello World")16public open class Any {17}18equals(other: Any?): Boolean19hashCode(): Int20toString(): String21public inline fun <reified T : Any> any(): T = Mockito.any(T::class.java)22The any()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito-kotlin automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful