我已经使用h2数据库进行单元测试,遵循this example。
在我的application.properties中:
class PicMee extends StatefulWidget{
@override
_PicMeeState createState() => new _PicMeeState();
}
class _PicMeeState extends State<PicMee>{
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
body: new ListView.builder(
itemCount: 20,
itemBuilder: (BuildContext context, int index){
return new CustomWidget(date: null, content: null, trailingIconOne: null, trailingIconTwo: null)
}
// https://stackoverflow.com/questions/47233209/flutter-group-listviews-with-separator?rq=1
),
);
}
}
class CustomWidget extends StatelessWidget {
String date;
String content;
Icon trailingIconOne;
Icon trailingIconTwo;
CustomWidget(
{@required this.date, @required this.content, @required this.trailingIconOne, @required this.trailingIconTwo});
@override
Widget build(BuildContext context) {
return new Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.grey[500])
),
child: new Column(
children: <Widget>[
new Container (child: new Text(date), color: Colors.yellow[200],),
new Container(height: 15.0,),
new Text(content),
new Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new IconButton(icon: trailingIconOne, onPressed: () {}),
new Container(width: 10.0,),
new IconButton(icon: trailingIconTwo, onPressed: () {})
],
)
],
),
);
}
}
在单元测试中:
server.port=8888
spring.h2.console.enabled=true
我有一个问题,当我运行单元测试时,在内存中创建h2数据库,但是在完成测试后,数据库被删除,我无法检查数据是否已插入h2数据库。
更新问题:我使用mybatis,它不是ORM(休眠,JPA)
答案 0 :(得分:0)
也许这个例子会更完整
https://www.baeldung.com/spring-boot-testing
您应该使用select和assert来测试在单元测试期间插入的数据,而不是在测试后手动对其进行检查。