@Test public void should_create_user() { given().contentType(ContentType.JSON).body(prepareUser()). when().post("/users"). then().statusCode(201). body("id", notNullValue()). body("name", is("Juntao Qiu")). body("email", is("juntao.qiu@gmail.com")); } private User prepareUser() { User user = new User(); user.setName("Juntao Qiu"); user.setEmail("juntao.qiu@gmail.com"); user.setPassword("password"); return user; }
如果使用Spring,还可以通过 profile
来切换不同的数据库。比如下面这个例子中,默认的profile会连接数据库 jigsaw
,而 integration
的profile会连接 jigsaw_test
数据库:
spring: datasource: url: jdbc:mysql://localhost:3306/jigsaw driver-class-title: com.mysql.jdbc.Driver username: root password: password --- spring: profiles: integration datasource: url: jdbc:mysql://localhost:3306/jigsaw_test driver-class-title: com.mysql.jdbc.Driver username: root password: password
组件测试会涉及到的组件包括:
原文转自:http://icodeit.org/2016/10/testing-in-microservice-context/