Commit 2423350c by yuwencan

Initial commit

parents
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.16</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.catkissfish.springboot.mocikito</groupId>
<artifactId>catkissfish-springboot-mockito-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>catkissfish-springboot-mockito-demo</name>
<description>catkissfish-springboot-mockito-demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.catkissfish.springboot.mocikito.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CatkissfishSpringbootMockitoDemoApplication {
public static void main(String[] args) {
SpringApplication.run(CatkissfishSpringbootMockitoDemoApplication.class, args);
}
}
package com.catkissfish.springboot.mocikito.demo.dto;
import lombok.Data;
@Data
public class UserUpdateReqDto {
private Long id;
private String username;
private String password;
}
package com.catkissfish.springboot.mocikito.demo.entity;
import lombok.Data;
@Data
public class User {
private String name;
private String password;
}
package com.catkissfish.springboot.mocikito.demo.service;
public interface OrderService {
public int orderCount(Long userId);
}
package com.catkissfish.springboot.mocikito.demo.service;
import com.catkissfish.springboot.mocikito.demo.dto.UserUpdateReqDto;
import com.catkissfish.springboot.mocikito.demo.entity.User;
public interface UserService {
public User selectOne(String username);
public int modifyById(UserUpdateReqDto reqDto);
public void add(User user);
public int orderCount(long userId);
}
package com.catkissfish.springboot.mocikito.demo.service.impl;
import com.catkissfish.springboot.mocikito.demo.service.OrderService;
import org.springframework.stereotype.Service;
@Service
public class OrderServiceImpl implements OrderService {
@Override
public int orderCount(Long userId) {
return 0;
}
}
package com.catkissfish.springboot.mocikito.demo.service.impl;
import com.catkissfish.springboot.mocikito.demo.dto.UserUpdateReqDto;
import com.catkissfish.springboot.mocikito.demo.entity.User;
import com.catkissfish.springboot.mocikito.demo.service.OrderService;
import com.catkissfish.springboot.mocikito.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private OrderService orderService;
@Override
public User selectOne(String username) {
return null;
}
@Override
public int modifyById(UserUpdateReqDto reqDto) {
return 0;
}
@Override
public void add(User user) {
}
@Override
public int orderCount(long userId) {
return orderService.orderCount(userId);
}
}
package com.catkissfish.springboot.mocikito.demo;
import com.catkissfish.springboot.mocikito.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
/**
* 测试Mock和Spy对象
*/
@ExtendWith(MockitoExtension.class)
class Demo01 {
@Mock
UserService mockUserService;
@Spy
UserService spyUserService;
@Test
public void test(){
System.out.println(Mockito.mockingDetails(mockUserService).isMock());
System.out.println(Mockito.mockingDetails(spyUserService).isSpy());
}
}
package com.catkissfish.springboot.mocikito.demo;
import com.catkissfish.springboot.mocikito.demo.dto.UserUpdateReqDto;
import com.catkissfish.springboot.mocikito.demo.entity.User;
import com.catkissfish.springboot.mocikito.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
/**
* 参数匹配测试:通过方法签名(参数)来指定哪些方法调用需要被处理(插桩、verify验证)
*/
@ExtendWith(MockitoExtension.class)
public class Demo02 {
@Mock
private UserService mockUserService;
/**
* mock对象没有插桩,不会调用真实方法,直接返回mock对象的默认值
* 如(int)默认值就是0,对象的默认值就是null,空集合的默认值就是长度为0的集合。
*/
@Test
public void test1(){
User user = mockUserService.selectOne("zs");
System.out.println("user = " + user);
UserUpdateReqDto reqDto = new UserUpdateReqDto();
int i = mockUserService.modifyById(reqDto);
System.out.println("i = " + i);
}
/**
* 测试插桩时的参数匹配,只拦截userUpdateReqDto
*/
@Test
public void test2(){
UserUpdateReqDto userUpdateReqDto = new UserUpdateReqDto();
userUpdateReqDto.setId(1L);
userUpdateReqDto.setUsername("zs");
//指定参数为userUpdateReqDto时调用mockUserService.modifyById返回99
Mockito.doReturn(99).when(mockUserService).modifyById(userUpdateReqDto);
int result1 = mockUserService.modifyById(userUpdateReqDto);
System.out.println("result1 = " + result1);
UserUpdateReqDto userUpdateReqDto2 = new UserUpdateReqDto();
userUpdateReqDto2.setId(2L);
userUpdateReqDto2.setUsername("ls");
int result2 = mockUserService.modifyById(userUpdateReqDto2);
System.out.println("result2 = " + result2);
}
/**
* ArgumentMatchers.any拦截UserUpdateReqDto类型的任意对象
*/
@Test
public void test3(){
Mockito.doReturn(99).when(mockUserService).modifyById(ArgumentMatchers.any(UserUpdateReqDto.class));
UserUpdateReqDto userUpdateReqDto = new UserUpdateReqDto();
userUpdateReqDto.setId(1L);
userUpdateReqDto.setUsername("zs");
int result1 = mockUserService.modifyById(userUpdateReqDto);
System.out.println("result1 = " + result1);
UserUpdateReqDto userUpdateReqDto2 = new UserUpdateReqDto();
userUpdateReqDto2.setId(2L);
userUpdateReqDto2.setUsername("ls");
int result2 = mockUserService.modifyById(userUpdateReqDto2);
System.out.println("result2 = " + result2);
}
/**
*
*/
@Test
public void test4(){
User user = new User();
user.setName("zs");
user.setPassword("123456");
mockUserService.add(user);
// mockUserService.add(user);
//校验调用了add方法一次
Mockito.verify(mockUserService, Mockito.times(1)).add(user);
}
}
package com.catkissfish.springboot.mocikito.demo;
import com.catkissfish.springboot.mocikito.demo.dto.UserUpdateReqDto;
import com.catkissfish.springboot.mocikito.demo.service.UserService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import java.util.ArrayList;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class Demo03 {
@Mock
private UserService userService;
@Mock
private ArrayList<String> mockList;
@Spy
private ArrayList<String> spyList;
/**
* 指定返回值
*/
@Test
public void test1(){
Mockito.doReturn(1).when(userService).modifyById(ArgumentMatchers.any(UserUpdateReqDto.class));
// Assertions.assertEquals(0, userService.modifyById(new UserUpdateReqDto()));
Assertions.assertEquals(1, userService.modifyById(new UserUpdateReqDto()));
}
/**
* void返回值方法插桩
*/
@Test
public void test2(){
doNothing().when(mockList).clear();
mockList.clear();
//验证调用一次
verify(mockList, times(1)).clear();
}
/**
* 抛出异常
*/
@Test
public void test3(){
doThrow(RuntimeException.class).when(mockList).get(anyInt());
try {
mockList.get(5);
}catch (Exception e) {
Assertions.assertTrue(e instanceof RuntimeException);
}
}
/**
* 多次插桩
*/
@Test
public void test4(){
doReturn(1).doReturn(2).doReturn(3).when(mockList).size();
Assertions.assertEquals(1, mockList.size());
Assertions.assertEquals(2, mockList.size());
Assertions.assertEquals(3, mockList.size());
Assertions.assertEquals(4, mockList.size());
}
/**
* 实现指定的逻辑插桩
*/
@Test
public void test5(){
doAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock) throws Throwable {
Integer argument = invocationOnMock.getArgument(0, Integer.class);
return String.valueOf(argument * 100);
}
}).when(mockList).get(anyInt());
String result = mockList.get(3);
Assertions.assertEquals("300", result);
}
/**
* 执行真正的原始方法
*/
@Test
public void test6(){
// //对mock对象插桩执行原始方法
// when(mockList.get(0)).thenCallRealMethod();
// System.out.println(mockList.get(0));
//未插桩spy对象默认就会调用真实方法。
doReturn("666").when(spyList).get(0);
System.out.println(spyList.get(0));
}
/**
* verify的使用
*/
@Test
public void test7(){
mockList.clear();
//从未调用过clear方法
// verify(mockList, never()).clear();
//至少调用过多少次
verify(mockList, atLeast(1)).clear();
}
}
package com.catkissfish.springboot.mocikito.demo;
import com.catkissfish.springboot.mocikito.demo.service.OrderService;
import com.catkissfish.springboot.mocikito.demo.service.impl.UserServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.Mockito.doReturn;
/**
* @InjectMocks注解的使用
*/
@ExtendWith(MockitoExtension.class)
public class Demo04 {
/**
* userService依赖的对象,mock后注入到他本身
*/
@InjectMocks
@Spy
private UserServiceImpl userService;
@Mock
private OrderService orderService;
@Test
public void test1(){
doReturn(100).when(userService).orderCount(1);
int orderCount = userService.orderCount(1);
Assertions.assertEquals(100, orderCount);
}
}
package com.catkissfish.springboot.mocikito.demo;
import com.catkissfish.springboot.mocikito.demo.service.OrderService;
import com.catkissfish.springboot.mocikito.demo.service.impl.UserServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import static org.mockito.Mockito.doReturn;
/**
* 集成springboot
*/
@SpringBootTest(classes = CatkissfishSpringbootMockitoDemoApplication.class)
public class Demo05 {
@Autowired
@SpyBean
private UserServiceImpl userService;
@MockBean
private OrderService orderService;
@Test
public void test1(){
// doReturn(100).when(userService).orderCount(1);
doReturn(50).when(orderService).orderCount(1L);
int orderCount = userService.orderCount(1);
Assertions.assertEquals(50, orderCount);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment