Skip to content

Commit 8fee85f

Browse files
committed
Migrate junit4 to junit5
1 parent 499702f commit 8fee85f

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

SpringBootAOP/src/main/java/com/example/java/component/SimpleAspect.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010
import org.slf4j.LoggerFactory;
1111
import org.springframework.stereotype.Component;
1212

13+
import java.util.concurrent.atomic.AtomicInteger;
14+
1315
@Aspect
1416
@Component
1517
public class SimpleAspect {
1618

1719
private static final Logger logger = LoggerFactory.getLogger(SimpleAspect.class);
1820

21+
private static final AtomicInteger counter = new AtomicInteger();
22+
1923
@Before("execution(* com.example.java.service.*.*Aop(..))")
2024
public void onBeforeHandler(JoinPoint joinPoint) {
2125
logger.info("=============== onBeforeThing");
26+
counter.incrementAndGet();
2227
}
2328

2429
@After("execution(* com.example.java.service.*.*Aop(..))")
@@ -36,4 +41,8 @@ public void onAfterReturningHandler(JoinPoint joinPoint, Object str) {
3641
public void onPointcut(JoinPoint joinPoint) {
3742
logger.info("=============== onPointcut");
3843
}
44+
45+
public static int count() {
46+
return counter.get();
47+
}
3948
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package com.example.java;
22

3+
import com.example.java.component.SimpleAspect;
34
import com.example.java.service.TestService;
4-
import org.junit.Test;
5-
import org.junit.runner.RunWith;
5+
import org.junit.jupiter.api.Test;
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.boot.test.context.SpringBootTest;
8-
import org.springframework.test.context.junit4.SpringRunner;
98

10-
@RunWith(SpringRunner.class)
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
1111
@SpringBootTest
12-
public class SpringBootAopApplicationTests {
12+
class SpringBootAopApplicationTests {
1313

1414
@Autowired
1515
private TestService service;
1616

1717
@Test
18-
public void test_aop() {
18+
void test_aopAndNoAop() {
19+
assertThat(SimpleAspect.count()).isEqualTo(0);
1920
service.testAop();
20-
}
21-
22-
@Test
23-
public void test_noAop() {
21+
assertThat(SimpleAspect.count()).isEqualTo(1);
2422
service.test();
23+
assertThat(SimpleAspect.count()).isEqualTo(1);
2524
}
26-
2725
}
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
package com.example.java;
22

33
import com.example.java.service.BasicService;
4-
import org.junit.Test;
5-
import org.junit.runner.RunWith;
4+
import org.junit.jupiter.api.Test;
65
import org.springframework.beans.factory.annotation.Autowired;
76
import org.springframework.boot.test.context.SpringBootTest;
8-
import org.springframework.test.context.junit4.SpringRunner;
97
import org.springframework.util.StopWatch;
108

11-
@RunWith(SpringRunner.class)
129
@SpringBootTest
13-
public class SpringBootAsyncApplicationTests {
10+
class SpringBootAsyncApplicationTests {
1411

15-
@Autowired
16-
private BasicService service;
12+
@Autowired
13+
private BasicService service;
1714

18-
@Test
19-
public void test_async() {
20-
StopWatch stopWatch = new StopWatch();
21-
stopWatch.start();
22-
service.onAsync();
23-
stopWatch.stop();
24-
System.out.println(stopWatch.toString());
25-
}
15+
@Test
16+
void test_async() {
17+
StopWatch stopWatch = new StopWatch();
18+
stopWatch.start();
19+
service.onAsync();
20+
stopWatch.stop();
21+
System.out.println(stopWatch);
22+
}
2623

27-
@Test
28-
public void test_sync() {
29-
StopWatch stopWatch = new StopWatch();
30-
stopWatch.start();
31-
service.onSync();
32-
stopWatch.stop();
33-
System.out.println(stopWatch.toString());
34-
}
24+
@Test
25+
void test_sync() {
26+
StopWatch stopWatch = new StopWatch();
27+
stopWatch.start();
28+
service.onSync();
29+
stopWatch.stop();
30+
System.out.println(stopWatch);
31+
}
3532
}

0 commit comments

Comments
 (0)