2929import com .google .api .gax .rpc .UnaryCallSettings ;
3030import com .google .api .generator .engine .ast .AnnotationNode ;
3131import com .google .api .generator .engine .ast .AssignmentExpr ;
32- import com .google .api .generator .engine .ast .CastExpr ;
3332import com .google .api .generator .engine .ast .ClassDefinition ;
3433import com .google .api .generator .engine .ast .CommentStatement ;
3534import com .google .api .generator .engine .ast .ConcreteReference ;
3635import com .google .api .generator .engine .ast .EmptyLineStatement ;
37- import com .google .api .generator .engine .ast .EnumRefExpr ;
3836import com .google .api .generator .engine .ast .Expr ;
3937import com .google .api .generator .engine .ast .ExprStatement ;
40- import com .google .api .generator .engine .ast .InstanceofExpr ;
4138import com .google .api .generator .engine .ast .LineComment ;
4239import com .google .api .generator .engine .ast .MethodDefinition ;
4340import com .google .api .generator .engine .ast .MethodInvocationExpr ;
44- import com .google .api .generator .engine .ast .NewObjectExpr ;
4541import com .google .api .generator .engine .ast .PrimitiveValue ;
4642import com .google .api .generator .engine .ast .Reference ;
4743import com .google .api .generator .engine .ast .ScopeNode ;
8076import java .util .Optional ;
8177import java .util .UUID ;
8278import java .util .concurrent .ExecutionException ;
79+ import java .util .function .Function ;
8380import java .util .stream .Collectors ;
8481import javax .annotation .Generated ;
8582import org .junit .After ;
@@ -98,7 +95,7 @@ public abstract class AbstractServiceClientTestClassComposer implements ClassCom
9895
9996 protected static final TypeStore FIXED_TYPESTORE = createStaticTypes ();
10097 protected static final AnnotationNode TEST_ANNOTATION =
101- AnnotationNode .withType (FIXED_TYPESTORE .get ("Test" ));
98+ AnnotationNode .withType (FIXED_TYPESTORE .get ("Test" ));
10299
103100 private final TransportContext transportContext ;
104101
@@ -149,16 +146,35 @@ protected abstract Map<String, VariableExpr> createClassMemberVarExprs(
149146
150147 protected List <Statement > createClassMemberFieldDecls (
151148 Map <String , VariableExpr > classMemberVarExprs ) {
152- return classMemberVarExprs .values ().stream ()
153- .map (
154- v ->
155- ExprStatement .withExpr (
156- v .toBuilder ()
157- .setIsDecl (true )
158- .setScope (ScopeNode .PRIVATE )
159- .setIsStatic (v .type ().reference ().name ().startsWith ("Mock" ))
160- .build ()))
161- .collect (Collectors .toList ());
149+ Function <VariableExpr , Boolean > isMockVarExprFn =
150+ v -> v .type ().reference ().name ().startsWith ("Mock" );
151+
152+ // Ordering matters for pretty-printing and ensuring that test output is deterministic.
153+ List <Statement > fieldDeclStatements = new ArrayList <>();
154+
155+ // Static fields go first.
156+ fieldDeclStatements .addAll (
157+ classMemberVarExprs .values ().stream ()
158+ .filter (v -> isMockVarExprFn .apply (v ))
159+ .map (
160+ v ->
161+ ExprStatement .withExpr (
162+ v .toBuilder ()
163+ .setIsDecl (true )
164+ .setScope (ScopeNode .PRIVATE )
165+ .setIsStatic (true )
166+ .build ()))
167+ .collect (Collectors .toList ()));
168+
169+ fieldDeclStatements .addAll (
170+ classMemberVarExprs .values ().stream ()
171+ .filter (v -> !isMockVarExprFn .apply (v ))
172+ .map (
173+ v ->
174+ ExprStatement .withExpr (
175+ v .toBuilder ().setIsDecl (true ).setScope (ScopeNode .PRIVATE ).build ()))
176+ .collect (Collectors .toList ()));
177+ return fieldDeclStatements ;
162178 }
163179
164180 private List <MethodDefinition > createClassMethods (
0 commit comments