How to single test microservices (Dubbo, Spring Cloud)?

Preface

There are many test frameworks, Junit, EasyMock, PowerMock, TestNG, DBUnit, etc.

It is recommended to write a single test for important business processes, using Junit, EasyMock test framework

If the business class is A, the naming method of the single test class is ATest, the package structure of the ATest class and the A class are the same, and the test method name is the same

IDEA quickly generates Test class

Press the shortcut key
Win: Ctrl + Shift + T
Mac: ⇧ + command + T to
Insert picture description here
select the method to be tested and click OK
Insert picture description here

Junit

Don't simply print the result, and write the expected assertion at the end, such as

assertTrue(flag);
assertEquals(username, "test");

EasyMock

In a microservice application, sometimes it is necessary to call someone else's service. How to perform a single test when someone else's interface is not well developed? At this time, you have to mock the return value of other people's services

If there is a ManageService that depends on the UserService of another service

@Service
public class ManageService {
    
    

    @Autowired
    private UserService userService;

    public String getUsername() {
    
    
        return userService.getUsername();
    }
    
    public boolean saveUserInfo(UserInfo userInfo) {
    
    
        return userService.saveUserInfo(userInfo);
    }
}
public interface UserService {
    
    

    String getUsername();

    boolean saveUserInfo(UserInfo userInfo);
}
@Data
public class UserInfo {
    
    

    private String name;
    private Integer age;
}

You can write a single test as follows

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class ManageServiceTest {
    
    

    @Autowired
    private ManageService manageService;

    private UserService userService;

    @Test
    public void getUsername() throws Exception {
    
    

        userService = EasyMock.createMock(UserService.class);
        EasyMock.expect(userService.getUsername()).andReturn("test");
        Field userServiceField = manageService.getClass().getDeclaredField("userService");
        userServiceField.setAccessible(true);
        userServiceField.set(manageService, userService);
        EasyMock.replay(userService);
        String username = manageService.getUsername();
        System.out.println(username);
        assertEquals(username, "test");
    }

    @Test
    public void saveUserInfo() throws Exception {
    
    

        userService = EasyMock.createMock(UserService.class);
        EasyMock.expect(userService.saveUserInfo(EasyMock.anyObject())).andReturn(true);
        Field userServiceField = manageService.getClass().getDeclaredField("userService");
        userServiceField.setAccessible(true);
        userServiceField.set(manageService, userService);
        EasyMock.replay(userService);
        boolean flag = manageService.saveUserInfo(EasyMock.anyObject());
        System.out.println(flag);
        assertTrue(flag);
    }
	
}

You can construct different types of input parameters, as shown below, there are many ways not to introduce

 EasyMock.anyBoolean();
 EasyMock.anyString();
 EasyMock.anyObject();

Reference blog

[1]http://thoreauz.com/2017/07/20/devops/test/JAVA common test framework/
[2]https://blog.csdn.net/a672489861/article/details/10065133
test web
[3 ]https://juejin.im/post/5b694ff7e51d4519475f7fbc
test json
[4]https://www.baeldung.com/jsonassert
Junit assertion
[5]http://wiki.jikexueyuan.com/project/junit/using-assertion .html
[6]https://www.cnblogs.com/shugen/p/6863027.html
maven generates a test report + which classes will be tested
[7]https://blog.csdn.net/fireofjava/article/details /28630449
好文
[8]https://www.cnblogs.com/mistor/p/5225380.html

Guess you like

Origin blog.csdn.net/zzti_erlie/article/details/106720391