core-ng-test和spring test的比较

core-ng-test和spring-test的比较

core-ng-test用到的是junit的封装

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author keny
 */
class TestDBConfigTest {
    private TestDBConfig config;

    @BeforeEach
    void createTestDBConfig() {
        config = new TestDBConfig();
    }

    @Test
    void databaseURL() {
        assertThat(config.databaseURL(null))
            .isEqualTo("jdbc:hsqldb:mem:.;sql.syntax_mys=true");

        config.name = "db1";
        assertThat(config.databaseURL(null))
            .isEqualTo("jdbc:hsqldb:mem:db1;sql.syntax_mys=true");
    }
}

2.springboot test 是注解机制


        UTF-8
   

   
        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
   

   
       
            org.springframework.boot
            spring-boot-starter-web
       

       
            org.springframework.boot
            spring-boot-starter-test
       

   

   
       
           
                org.springframework.boot
                spring-boot-maven-plugin
           

       

   

可以直接用

@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartUpApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerTest {

    /**
     * @LocalServerPort 提供了 @Value("${local.server.port}") 的代替
     */
    @LocalServerPort
    private int port;

    private URL base;

    @Autowired
    private TestRestTemplate restTemplate;

    @Before
    public void setUp() throws Exception {
        String url = String.format("http://localhost:%d/", port);
        System.out.println(String.format("port is : [%d]", port));
        this.base = new URL(url);
    }

    /**
     * 向"/test"地址发送请求,并打印返回结果
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {

        ResponseEntity response = this.restTemplate.getForEntity(
                this.base.toString() + "/test", String.class, "");
        System.out.println(String.format("测试结果为:%s", response.getBody()));
    }

 

你可能感兴趣的:(spring实践大全)