第一章-测试概述
1junit4.12
jar
1
2
3
4
5
6
|
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
<scope>test</scope>
</dependency>
|
使用
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class ZzDepartmentMapperTest {
@Autowired
ZzDepartmentMapper mapper;
@Test
public void t1(){
mapper.doXX();//这样就不用进行application获取了
}
}
|
2pressTest
pressureTest压力测试
1创建线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package com.lsl.cache;
import com.lsl.entity.User;
import com.lsl.mapper.UserMapper;
import com.lsl.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
public class PressureThread implements Runnable {
private Integer oid;
private UserMapper userMapper;
private RedisTemplate template;
CyclicBarrier cyclicBarrier;
public PressureThread(Integer oid, UserMapper userMapper, RedisTemplate template,CyclicBarrier cyclicBarrier) {
this.oid = oid;
this.userMapper = userMapper;
this.template = template;
this.cyclicBarrier=cyclicBarrier;
}
@Override
public void run() {
//System.out.println("开启");
try {
//线程等待
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
String newoid=UUID.randomUUID().toString();
User user = (User) template.opsForValue().get("user🆔"+newoid.toString());
if(user!=null){
//redis存在 直接返回
}else {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
user = (User) template.opsForValue().get("user🆔"+newoid.toString());
if(user==null) {
user = (User) template.opsForValue().get("user🆔" + oid.toString());
user = userMapper.selectById(newoid);
template.opsForValue().set("user🆔" + oid, user, 100 + new Random().nextInt(30), TimeUnit.SECONDS);
System.out.println("存活时间:" + template.getExpire("user🆔" + oid, TimeUnit.SECONDS));
}
}
//System.out.println("结束");
}
}
|
2使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Test
public void findNull(){
//循环栅栏,到1000时 放开栅栏
CyclicBarrier cyclicBarrier = new CyclicBarrier(1000);
ExecutorService executorService = Executors.newFixedThreadPool(1000);
for (int i = 0; i <1000 ; i++) {
executorService.execute(new PressureThread(1,userMapper,redisTemplate,
cyclicBarrier));
}
executorService.shutdown();
while(!executorService.isTerminated()){
}
System.out.println("程序结束");
}
|
3Springboot测试
注意:
测试时,@Test的方法,若主线程执行关闭,则会关闭整个测试方法线程。
若此时还有其它线程,则也会被关闭。System.in.read();//加入该代码,让主线程不挂掉
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
|
1
2
3
4
5
|
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@jupiter.api.Test //注意使用这个注解
|
第二章-