|
1 | 1 | package br.com.jonyfs.springbootkotlinapi
|
2 | 2 |
|
| 3 | +import br.com.jonyfs.springbootkotlinapi.privilege.PrivilegeRepository |
| 4 | +import br.com.jonyfs.springbootkotlinapi.role.RoleRepository |
| 5 | +import br.com.jonyfs.springbootkotlinapi.user.User |
| 6 | +import br.com.jonyfs.springbootkotlinapi.user.UserRepository |
| 7 | +import org.assertj.core.api.Assertions.assertThat |
3 | 8 | import org.junit.Test
|
4 | 9 | import org.junit.runner.RunWith
|
| 10 | +import org.springframework.beans.factory.annotation.Autowired |
5 | 11 | import org.springframework.boot.test.context.SpringBootTest
|
| 12 | +import org.springframework.boot.test.json.JacksonTester |
| 13 | +import org.springframework.boot.test.web.client.TestRestTemplate |
| 14 | +import org.springframework.context.ApplicationContext |
| 15 | +import org.springframework.http.ResponseEntity |
6 | 16 | import org.springframework.test.context.junit4.SpringRunner
|
| 17 | +import javax.annotation.Resource |
| 18 | + |
7 | 19 |
|
8 | 20 | @RunWith(SpringRunner::class)
|
9 |
| -@SpringBootTest |
| 21 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
10 | 22 | class SpringBootKotlinApiApplicationTests {
|
11 | 23 |
|
| 24 | + @Autowired |
| 25 | + lateinit var restTemplate: TestRestTemplate |
| 26 | + |
| 27 | + @Resource |
| 28 | + private val applicationContext: ApplicationContext? = null |
| 29 | + |
| 30 | + @Resource |
| 31 | + lateinit var userRepository: UserRepository |
| 32 | + |
| 33 | + @Resource |
| 34 | + lateinit var roleRepository: RoleRepository |
| 35 | + |
| 36 | + @Resource |
| 37 | + lateinit var privilegeRepository: PrivilegeRepository |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private val json: JacksonTester<User>? = null |
| 41 | + |
| 42 | + |
| 43 | + @Test |
| 44 | + fun testIfContextLoads() { |
| 45 | + assertThat(applicationContext).isNotNull() |
| 46 | + } |
| 47 | + |
12 | 48 | @Test
|
13 |
| - fun contextLoads() { |
| 49 | + fun testIfApiLoads() { |
| 50 | + |
| 51 | + var user = User() |
| 52 | + user.firstName = "John" |
| 53 | + user.lastName = "Anderson" |
| 54 | + user.email = "john.anderson@test.com" |
| 55 | + user.password = "XPTO" |
| 56 | + |
| 57 | + |
| 58 | + user = userRepository.save(user) |
| 59 | + |
| 60 | + assertThat(user).isNotNull() |
| 61 | + assertThat(user.id).isNotNull() |
| 62 | + assertThat(user.firstName).isNotNull() |
| 63 | + |
| 64 | + var response: ResponseEntity<User> = restTemplate.getForEntity("/users/" + user.id, User::class.java) |
| 65 | + |
| 66 | + assertThat(response).isNotNull() |
| 67 | + |
| 68 | + var savedUser = response.body |
| 69 | + |
| 70 | + assertThat(savedUser).isNotNull() |
| 71 | + //assertThat(savedUser.id).isNotNull() |
| 72 | + assertThat(savedUser.firstName).isNotNull() |
| 73 | + |
| 74 | + |
14 | 75 | }
|
15 | 76 |
|
16 | 77 | }
|
0 commit comments