Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. Cannot mock/spy class bye.persistence.jdbcTrial
  2. Mockito cannot mock/spy following:
  3. - final classes
  4. - anonymous classes
  5. - primitive types
  6.  
  7. package byeTest.persistenceTest
  8. import bye.domain.User
  9. import bye.persistence.jdbcTrial
  10. import bye.spring.GreetingController
  11. import byeTest.persistenceTest.RestAPITest.RootConfig
  12. import org.junit.Before
  13. import org.junit.Test
  14. import org.junit.runner.RunWith
  15. import org.mockito.BDDMockito.given
  16. import org.mockito.Mockito
  17. import org.springframework.beans.factory.annotation.Autowired
  18. import org.springframework.context.annotation.Bean
  19. import org.springframework.context.annotation.Configuration
  20. import org.springframework.http.MediaType
  21. import org.springframework.test.context.ContextConfiguration
  22. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
  23. import org.springframework.test.context.web.WebAppConfiguration
  24. import org.springframework.test.web.servlet.MockMvc
  25. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
  26. import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
  27. import org.springframework.test.web.servlet.setup.MockMvcBuilders
  28. import org.springframework.web.context.WebApplicationContext
  29.  
  30. @RunWith(SpringJUnit4ClassRunner::class)
  31. @ContextConfiguration(classes = arrayOf(RootConfig::class))
  32. @WebAppConfiguration
  33. open class RestAPITest {
  34. var mockMvc: MockMvc? = null;
  35.  
  36. @Autowired
  37. var wac : WebApplicationContext? = null;
  38.  
  39. @Autowired
  40. var jdbcTrial : jdbcTrial? = null
  41.  
  42. @Autowired
  43. var todoServiceMock : GreetingController? = null;
  44.  
  45. @Before
  46. open fun setup(){
  47. mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build()
  48. given(this.jdbcTrial?.getUserById(2)).willReturn(User(2,"uname","typ"));
  49.  
  50. }
  51.  
  52.  
  53.  
  54. @Test
  55. open public fun find_2(){
  56. mockMvc!!.perform(get("/user/2").accept(MediaType.APPLICATION_JSON))
  57.  
  58. .andExpect(content().string("{"id":2,"username":"uname","usertype":"typ"}"))
  59.  
  60.  
  61. }
  62.  
  63. @Configuration
  64. open class RootConfig{
  65. @Bean
  66. open fun jdbcTrial():jdbcTrial{
  67. return Mockito.mock(jdbcTrial::class.java)
  68. }
  69. }
  70. }
  71.  
  72. import bye.domain.Comment
  73. import bye.domain.Event
  74. import bye.domain.Participant
  75. import bye.domain.User
  76. import java.sql.Connection
  77. import java.sql.DriverManager
  78. import java.sql.ResultSet
  79. import java.sql.Statement
  80. import java.util.ArrayList
  81. import javax.sql.DataSource
  82.  
  83. open class jdbcTrial() {
  84. open var url: String = "jdbc:postgresql://rosdel.quintor.local:5432/quintorevents"
  85. //val props: Properties = Properties();
  86.  
  87. open var DB_DRIVER = "org.postgresql.Driver";
  88. open var dataSource:String? = "b";
  89.  
  90. constructor(s : String):this(){
  91. this.dataSource = s
  92. }
  93.  
  94. open fun getFoo():String{
  95. var query : String = "select val_col from foo_tbl where key_col = 'foo';"
  96. var rs:ResultSet = this.getConnection().createStatement().executeQuery(query)
  97. var result:String = "wrong";
  98. while(rs.next()){
  99. result= rs.getString("val_col")
  100. }
  101. return result;
  102. }
  103.  
  104. open fun getBaz():String{
  105. return "qux"
  106. }
  107.  
  108. // /events
  109. open fun getAll(): List<Event> {
  110. return getMultipleEvents("select * from quintor_event;")
  111. }
  112.  
  113. // /events/search
  114. open fun searchEvents(search: String): List<Event> {
  115. var query = "select * from quintor_event where title LIKE '%" + search + "%';"
  116. return getMultipleEvents(query)
  117. }
  118.  
  119. // used for getting the comments
  120. open fun getUserById(id: Int): User {
  121. var query = "select * from quintor_user where id = ${id};";
  122. return getSingleUser(query)
  123. }
  124. ........
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement