Unit testing

更新时间:
复制 MD 格式

The SOFABoot engineering prototype provides two types of unit tests:

This article will use the test classes of a SOFABoot Web project as an example to explain the testing logic.

Note

For SOFABoot Core projects, the test classes follow a comparatively straightforward logic that can be implemented by referring to the Web project examples; no further details will be provided.

Test class path:

  • Test base class (AbstractTestBasesrc):

    projectName/app/web/src/main/test/java/.../base/AbstractTestBase.java

  • Test subclass (SofaRestServiceTest): projectName/app/web/src/main/test/java/.../usercases/SofaRestServiceTest.java

For more information about unit testing, see Spring Boot documentation.

Test base class

Sample code

The following is the sample code of the AbstractTestBase.java test base class generated by default:

/**
 * For more information, see http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#boot-features-testing.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SOFABootWebSpringApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public abstract class AbstractTestBase {

    public static final String SOFA_REST_PORT = "8341";

    @Autowired
    public EmbeddedWebApplicationContext server;

    /**
     * 8080
     */
    @LocalServerPort
    public int definedPort;


    @Autowired
    public TestRestTemplate testRestTemplate;

    public String urlHttpPrefix;

    public String sofaRestHttpPrefix;

    @Before
    public void setUp() {
        sofaRestHttpPrefix = "http://localhost:" + SOFA_REST_PORT;
        urlHttpPrefix = "http://localhost:" + definedPort;
        childSetUp();
    }

    /**
     * Test the initialization code that needs to be performed before each method of the subclass is executed.
     */
    public abstract void childSetUp();

}

Code interpretation

  • Parameters of @ SpringBootTest:

    • SOFABootWebSpringApplication class: It is the startup function of SOFABoot. When running unit tests, you can directly start the SOFABoot framework and complete the automatic configuration and startup of all dependent middleware.

    • webEnvironment: Used to configure the servlet environment. DEFINED_PORT said that the pattern will provide a real servlet environment, using embedded containers, but using well-defined ports.

  • Injected bean:

    • EmbeddedWebApplicationContext instance: used to test the user context.

    • TestRestTemplate instance: An instance of the template used to test the REST functionality.

Test subclasses

Sample code

The following is the sample code for the default generated SofaRestServiceTest.java test class:

public class SofaRestServiceTest extends AbstractTestBase {

    @Test
    public void testSofaRestGet() throws Exception {
        assertNotNull(testRestTemplate);
        String restUrl = sofaRestHttpPrefix + "/webapi/users/xiaoming";
// Note: The RestSampleFacadeResp must have a default constructor.
        ResponseEntity<RestSampleFacadeResp> response = testRestTemplate.getForEntity(restUrl, RestSampleFacadeResp.class);
        RestSampleFacadeResp restSampleFacadeResp = response.getBody();
        assertTrue(restSampleFacadeResp.isSuccess());
// Note: This is only for testing. jackson does not support deserialization of generic types, so it is deserialized as map. 
// For more information, see http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#boot-features-testing.
        Map<String, Integer> demoUserModel = (Map<String, Integer>) restSampleFacadeResp.getData();
        assertTrue(demoUserModel.get("userId") >= 0);
    }
}

Code interpretation

  • Through inheritance, subclasses can obtain the following information from the base class:

    • Injected TestRestTemplate instance of the base class.

    • REST port number 8341.

    • URL of SOFAREST to be tested: Used to test the function of SOFAREST and verify according to the returned data results.

  • Test logic

    • The TestRestTemplate initiates a restUrl request and returns a response object.

    • The response object obtains the restSampleFacadeResp object through the getBody() method, and determines the obtained data through the assertTrue() method.

    • The restSampleFacadeResp object obtains the default data in the SampleRestFacadeRestImpl through the getData() method, and determines the obtained data through the assertTrue() method.

  • Test result verification

    • Method 1: You can run a test class SofaRestServiceTest through IntelliJ IDEA. When the test is passed, the console prompts "Tests passed: 1 of 1 test".

    • Method 2: Run the SOFABootWebSpringApplication class, and then enter: http://localhost:8341/webapi/users/xiaoming in the address bar of the browser. The output is shown in the following figure.

      单元测试