Kotlin – Mockito不能模拟/间谍(Spring REST API)

由于Kotlin中的所有课程都是默认的最终课程,所以Mockito不能窥探最后的课程:

 Cannot mock/spy class bye.persistence.jdbcTrial Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types 

而这个指南 (7月6日,丹尼·布鲁斯勒(Danny Preussler))说,一个框架是解决这个问题的必要条件。

现在我想知道,是否有可能测试一个REST API(使用Spring MockMvc)。 以下是我的测试代码:

 package byeTest.persistenceTest import bye.domain.User import bye.persistence.jdbcTrial import bye.spring.GreetingController import byeTest.persistenceTest.RestAPITest.RootConfig import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.BDDMockito.given import org.mockito.Mockito import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.http.MediaType import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.junit4.SpringJUnit4ClassRunner import org.springframework.test.context.web.WebAppConfiguration import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content import org.springframework.test.web.servlet.setup.MockMvcBuilders import org.springframework.web.context.WebApplicationContext @RunWith(SpringJUnit4ClassRunner::class) @ContextConfiguration(classes = arrayOf(RootConfig::class)) @WebAppConfiguration open class RestAPITest { var mockMvc: MockMvc? = null; @Autowired var wac : WebApplicationContext? = null; @Autowired var jdbcTrial : jdbcTrial? = null @Autowired var todoServiceMock : GreetingController? = null; @Before open fun setup(){ mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build() given(this.jdbcTrial?.getUserById(2)).willReturn(User(2,"uname","typ")); } @Test open public fun find_2(){ mockMvc!!.perform(get("/user/2").accept(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"id\":2,\"username\":\"uname\",\"usertype\":\"typ\"}")) } @Configuration open class RootConfig{ @Bean open fun jdbcTrial():jdbcTrial{ return Mockito.mock(jdbcTrial::class.java) } } } 

我设置了所有用于open的函数和类,因为根据kotlin文档,这与final是完全相反的。 但是使用这个(到处都是)仍然会抛出上面提到的异常。

 import bye.domain.Comment import bye.domain.Event import bye.domain.Participant import bye.domain.User import java.sql.Connection import java.sql.DriverManager import java.sql.ResultSet import java.sql.Statement import java.util.ArrayList import javax.sql.DataSource open class jdbcTrial() { open var url: String = "jdbc:postgresql://rosdel.quintor.local:5432/quintorevents" //val props: Properties = Properties(); open var DB_DRIVER = "org.postgresql.Driver"; open var dataSource:String? = "b"; constructor(s : String):this(){ this.dataSource = s } open fun getFoo():String{ var query : String = "select val_col from foo_tbl where key_col = 'foo';" var rs:ResultSet = this.getConnection().createStatement().executeQuery(query) var result:String = "wrong"; while(rs.next()){ result= rs.getString("val_col") } return result; } open fun getBaz():String{ return "qux" } // /events open fun getAll(): List<Event> { return getMultipleEvents("select * from quintor_event;") } // /events/search open fun searchEvents(search: String): List<Event> { var query = "select * from quintor_event where title LIKE '%" + search + "%';" return getMultipleEvents(query) } // used for getting the comments open fun getUserById(id: Int): User { var query = "select * from quintor_user where id = ${id};"; return getSingleUser(query) } ........