Skip to content

Commit 07afe44

Browse files
author
thuytm
committed
test
1 parent 1ddf419 commit 07afe44

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

hello-spring-retry/src/main/java/com/example/hellospringretry/MyService.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,37 @@
77

88
@Service
99
public class MyService {
10+
// @Retryable(
11+
// value = ArithmeticException.class,
12+
// maxAttempts = 2,
13+
// backoff = @Backoff(delay = 5001))
14+
int doSomething1(int a) {
15+
try {
16+
System.out.println("---------------- Doing");
17+
int i = a/0;
18+
return i;
19+
} catch (ArithmeticException ex) {
20+
throw ex;
21+
}
22+
}
1023

24+
// @Retryable(
25+
// value = Exception.class,
26+
// maxAttempts = 2,
27+
// backoff = @Backoff(delay = 1001))
28+
int doSomething(int a) {
29+
try {
30+
System.out.println("---------------- Doing");
31+
int i = a/0;
32+
return i;
33+
} catch (Exception ex) {
34+
throw ex;
35+
}
36+
}
37+
38+
@Recover
39+
public int recover(Exception ex, int a) {
40+
System.out.println("---------------- Recovering for ex " + ex.getMessage());
41+
return a;
42+
}
1143
}

user-management-test/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<artifactId>lombok</artifactId>
4444
<optional>true</optional>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.codehaus.groovy</groupId>
48+
<artifactId>groovy</artifactId>
49+
<version>3.0.8</version>
50+
</dependency>
4651
<dependency>
4752
<groupId>org.mindrot</groupId>
4853
<artifactId>jbcrypt</artifactId>
@@ -63,6 +68,28 @@
6368
<artifactId>junit-jupiter-engine</artifactId>
6469
<scope>test</scope>
6570
</dependency>
71+
<dependency>
72+
<groupId>junit</groupId>
73+
<artifactId>junit</artifactId>
74+
<version>4.12</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>net.serenity-bdd</groupId>
79+
<artifactId>serenity-junit</artifactId>
80+
<version>1.0.23</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>com.adaptavist</groupId>
84+
<artifactId>tm4j-junit-integration</artifactId>
85+
<version>1.0.0</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>io.rest-assured</groupId>
89+
<artifactId>rest-assured</artifactId>
90+
<version>4.4.0</version>
91+
<scope>test</scope>
92+
</dependency>
6693
</dependencies>
6794

6895
<build>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.demo;
2+
3+
import com.example.demo.model.dto.UserDto;
4+
import net.thucydides.core.annotations.Title;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.boot.web.server.LocalServerPort;
9+
import org.springframework.http.HttpEntity;
10+
import org.springframework.http.HttpMethod;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.boot.test.web.client.TestRestTemplate;
13+
import org.springframework.http.HttpHeaders;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
import static org.hamcrest.Matchers.is;
16+
import static org.junit.Assert.assertEquals;
17+
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest(classes = UserManagementTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
20+
public class IntegrationTest {
21+
@LocalServerPort
22+
private int port;
23+
24+
TestRestTemplate restTemplate = new TestRestTemplate();
25+
26+
HttpHeaders headers = new HttpHeaders();
27+
28+
@Test
29+
@Title("Verify api create user")
30+
public void createUserTest() {
31+
headers.add("Content-Type", "application/json");
32+
HttpEntity<String> entity = new HttpEntity<String>("{ \"full_name\": \"Nguyen Van A\", \"email\": \"\", \"password\": \"abc123\", \"phone\": \"0942362001\" }", headers);
33+
ResponseEntity<UserDto> response = restTemplate.exchange(createURLWithPort("/users"), HttpMethod.POST, entity, UserDto.class);
34+
35+
assertEquals(response.getStatusCode(), is(400));
36+
}
37+
38+
private String createURLWithPort(String uri) {
39+
return "http://localhost:" + port + uri;
40+
}
41+
42+
}
43+
44+

0 commit comments

Comments
 (0)