Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 822b1b6

Browse files
writing tests
1 parent dfc76fd commit 822b1b6

File tree

2 files changed

+91
-18
lines changed

2 files changed

+91
-18
lines changed

‎src/test/java/com/example/demo/RandomBookApplicationTests.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,99 @@
11
package com.example.randombook;
22

3-
import org.junit.jupiter.api.Test;
3+
import com.example.randombook.user.User;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
48
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.web.client.TestRestTemplate;
10+
import org.springframework.boot.web.server.LocalServerPort;
11+
import org.springframework.http.*;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.springframework.web.client.HttpClientErrorException;
514

6-
@SpringBootTest
7-
class RandombookApplicationTests {
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest(classes = RandomBookApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
17+
public class RandombookApplicationTests {
18+
19+
@Autowired
20+
private TestRestTemplate restTemplate;
21+
22+
@LocalServerPort
23+
private int port;
24+
25+
private String getRootUrl() {
26+
return "http://localhost:" + port;
27+
}
28+
29+
@Test
30+
public void contextLoads() {
31+
}
32+
33+
@Test
34+
public void testGetAllUsers() {
35+
HttpHeaders headers = new HttpHeaders();
36+
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
37+
38+
ResponseEntity<String> response = restTemplate.exchange(getRootUrl() + "/user",
39+
HttpMethod.GET, entity, String.class);
40+
41+
Assert.assertNotNull(response.getBody());
42+
}
43+
44+
@Test
45+
public void testGetUserById() {
46+
User user = restTemplate.getForObject(getRootUrl() + "/user/1", User.class);
47+
System.out.println(user.getEmail());
48+
Assert.assertNotNull(user);
49+
}
850

951
@Test
10-
void contextLoads() {
52+
public void testCreateUser() {
53+
User user = new User(
54+
0,
55+
"lollo",
56+
"lollo@lollo.com",
57+
"qwerty123",
58+
""
59+
);
60+
61+
ResponseEntity<User> postResponse = restTemplate.postForEntity(getRootUrl() + "/user", user, User.class);
62+
Assert.assertNotNull(postResponse);
63+
Assert.assertNotNull(postResponse.getBody());
64+
}
65+
66+
@Test
67+
public void testUpdatePost() {
68+
int id = 1;
69+
User user = restTemplate.getForObject(getRootUrl() + "/user/" + id, User.class);
70+
User u = new User(
71+
user.getId(),
72+
user.getUsername(),
73+
user.getEmail(),
74+
"new-super-password",
75+
user.getPicture()
76+
);
77+
78+
restTemplate.put(getRootUrl() + "/user/" + id, user);
79+
80+
User updatedUser = restTemplate.getForObject(getRootUrl() + "/user/" + id, User.class);
81+
Assert.assertNotNull(updatedUser);
82+
}
83+
84+
@Test
85+
public void testDeletePost() {
86+
int id = 3;
87+
User user = restTemplate.getForObject(getRootUrl() + "/user/" + id, User.class);
88+
Assert.assertNotNull(user);
89+
90+
restTemplate.delete(getRootUrl() + "/user/" + id);
91+
92+
try {
93+
user = restTemplate.getForObject(getRootUrl() + "/users/" + id, User.class);
94+
} catch (final HttpClientErrorException e) {
95+
Assert.assertEquals(e.getStatusCode(), HttpStatus.NOT_FOUND);
96+
}
1197
}
1298

13-
}
99+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /