我正在编写一个JUnit测试,并希望在对象具有多个不同变量的地方运行测试。我通过传递可运行对象来避免深层嵌套的循环。
private Runnable withAges( final Person person, final Runnable r ) {
return () -> {
person.setAge( 0 );
r.run();
person.setAge( 50 );
r.run();
person.setAge( Integer.MAX_VALUE );
r.run();
person.setAge( -1 );
r.run();
person.setAge( Integer.MIN_VALUE );
r.run();
};
}
private Runnable withHair( final Person person, final Runnable r ) {
return () -> {
person.setHair( "blonde" );
r.run();
person.setHair( "BLONDE" );
r.run();
person.setHair( null );
r.run();
};
}
private Runnable withAddresses( final Person person, final Runnable r ) {
...
}
private Runnable withChildren( final Person person, final Runnable r ) {
...
}
实际测试为:
@Test
void testPerson() {
final Person person = new Person();
final Runnable tests = () -> {
assertTrue( ... );
assertFalse( ... );
}
...
}
目前,我可以使用以下方式运行这些测试:>
withAges( person, withHair( person, withAddresses( person, withChildren( person, tests ) ) ) );
但是,如果我能通过写类似的东西来清理它,那将是非常好的
withAges( person ).withHair( person ).withAddresses( person ).withChildren( person ).tests.run();
我正在编写一个JUnit测试,并希望在对象具有多个不同变量的地方运行测试。我通过传递可运行对象来避免深层嵌套的循环。私人Runnable withAges(final ...
不要采取这种错误的方式:您当前和期望的方法都很难遵循。
通过将site
设置为实例变量,我能够将其从withX()
签名中删除。这使我可以通过