Why do we need unit tests

Unit testing has the advantages of ensuring code quality, finding software bugs early, simplifying the debugging process, facilitating change and simplifying integration, and making processes more flexible. Unit testing is the independent testing of code units. Its core is “independence”, and its advantage is derived from this independence. Its disadvantage is precisely because of its independence: since it is “independence”, it is difficult to test the interrelationship with other code and dependent environment. Unit testing and system testing are complementary rather than substitutes. The advantage of unit testing is the deficiency of system testing, and the deficiency of unit testing is the advantage of system testing. Unit testing can not be regarded as a panacea to solve all problems, but need to understand its advantages and disadvantages, develop strengths and circumvent weaknesses, and system testing complement each other, to achieve the maximum benefit of testing.

OAuth2 system unit testing is difficult

  • Interface testing relies on UPMS (user rights management) and cannot be decoupled and independent

  • The spring-security-test module does not provide a standard implementation

  • The complex scenario requires both stateless token invocation and the guarantee of message delivery services

The solution

Reference @ WithMockUser, automatically perform related enhancement in Mock blocker (token), and by extension WithSecurityContextFactory context token passing. Pig-common-test [1]

Introduction of depend on

<dependency>
  <groupId>com.pig4cloud</groupId>
  <artifactId>pig-common-test</artifactId>
  <version>${last.version}</version>
  <scope>test</scope>
</dependency> Copy the code

Unit test Controller interface

  • Specify the authentication authority interface
The configuration intest/resources/application.yml
security:
  oauth2:
    client:
      access-token-uri: http://pig-gateway:3000/oauth/token
Copy the code
  • Simulate testing the Controller interface
@RunWith(SpringRunner.class)
@SpringBootTest
public class SysLogControllerTest {

 private MockMvc mvc;
  @Autowired private WebApplicationContext applicationContext; / / injection WebApplicationContext  @Before  public void setUp() {  this.mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();  }   @Test  @SneakyThrows  @WithMockOAuth2User  public void testMvcToken() {  mvc.perform(delete("/log/1").with(token())).andExpect(status().isOk());  } } Copy the code

Simulation test FeignClient to transfer token

Simply inject the FeignClient implementation and test the class with the @WithMockoAuth2User annotation

WithMockOAuth2User Property description

  • The current use case obtains the user name used by the token
String username() default "admin";
Copy the code
  • The current use case obtains the password used by the token
String password() default "123456";
Copy the code

The pig-common-test[1] module is currently only implemented in Pig 2.10

The resources

[1]

pig-common-test: https://gitee.com/log4j/pig/tree/master/pig-common/pig-common-test