Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Kotlin Spring引导测试端点和服务层,模拟存储库层_Java_Spring_Kotlin_Mocking - Fatal编程技术网

Java Kotlin Spring引导测试端点和服务层,模拟存储库层

Java Kotlin Spring引导测试端点和服务层,模拟存储库层,java,spring,kotlin,mocking,Java,Spring,Kotlin,Mocking,为了测试端点,我将JUnit与SpringRunner和@WebMvcTest @RunWith(SpringRunner::class) @WebMvcTest(UserEndpoints::class) class UserEndpointsTest { } UserEndpoints取决于UserSevice UserService取决于UserRepository 我想模拟UserRepository,以便测试UserEndpoints @RunWith(SpringRunner::c

为了测试端点,我将JUnit与
SpringRunner
@WebMvcTest

@RunWith(SpringRunner::class)
@WebMvcTest(UserEndpoints::class)
class UserEndpointsTest {
}
UserEndpoints
取决于
UserSevice

UserService
取决于
UserRepository

我想模拟
UserRepository
,以便测试
UserEndpoints

@RunWith(SpringRunner::class)
@WebMvcTest(UserEndpoints::class)
class UserEndpointsTest2 {

    @Autowired
    private val mockMvc:MockMvc?=null

    @MockBean
    private val userRepository:UserRepository?=null

    @InjectMocks
    private var userService:UserService?=null

    @Before
    fun setup() {
        initMocks(this)

        Mockito.`when`(userRepository !!.findById(eq("1")))
                .thenReturn(Optional.of(users().get(0)))

        Mockito.`when`(userRepository.findById(eq("2"))).thenReturn(Optional.of(users().get(1)))
    }

    @Test
    fun testGetUser() {
        this.mockMvc!!.perform(get("/user").param("id", "2"))
                .andExpect(MockMvcResultMatchers.status().isOk)
                .andExpect(MockMvcResultMatchers.jsonPath("$.username").value(Matchers.equalTo("username1")))
                .andExpect(MockMvcResultMatchers.jsonPath("$.remaining_requests").value(Matchers.equalTo(101)))
                .andExpect(MockMvcResultMatchers.jsonPath("$.type").value(Matchers.equalTo("USER")))
    }

    fun users(): List<User> {
        val user1 = User("1", "username", "password", "123", 100, UserType.BETA)
        val user2 = User("2", "username1", "password1", "1234", 101, UserType.USER)
        return arrayListOf<User>(user1, user2)
    }

    private fun <T> any(type: Class<T>): T {
        Mockito.any(type)
        return null as T
    }
}

模拟存储库层的正确方法是什么?

因此,首先,如果这是一个web层/控制器测试,您应该模拟
UserSevice
而不是用户存储库。如果这是对
UserSevice
的测试,则不需要web环境。其次,我认为您的代码非常冗长,因为它将类型声明为可为null,我将使用专门为这些情况创建的kotlin

说我通常做的是创建一个嵌套的
@Configuration
类,它显式地包含我的bean声明

@RunWith(SpringRunner::class)
@WebMvcTest(UserEndpoints::class)
@Import(TestConfig::class)
class UserEndpointsTest {
}

然后,您可以使用简单的
@Autowired
(无需混合spring和mockito注释)

来注入bean,因此首先,如果这是一个web层/控制器测试,那么您应该模拟
用户服务
而不是用户存储库。如果这是对
UserSevice
的测试,则不需要web环境。其次,我认为您的代码非常冗长,因为它将类型声明为可为null,我将使用专门为这些情况创建的kotlin

说我通常做的是创建一个嵌套的
@Configuration
类,它显式地包含我的bean声明

@RunWith(SpringRunner::class)
@WebMvcTest(UserEndpoints::class)
@Import(TestConfig::class)
class UserEndpointsTest {
}
然后,您可以使用简单的
@Autowired
(无需混合spring和mockito注释)注入bean

@Configuration
class TestConfig {

    @Bean
    fun userRepository(): UserRepository = mock(UserRepository.class)

    @Bean
    fun userService(UserRepository userRepo): UserService = UserService(userRepo)
}