diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java index a3f4a4530c..7f4abb0a42 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java @@ -30,6 +30,7 @@ import com.google.api.generator.engine.ast.NewObjectExpr; import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; +import com.google.api.generator.engine.ast.StringObjectValue; import com.google.api.generator.engine.ast.ThisObjectValue; import com.google.api.generator.engine.ast.TypeNode; import com.google.api.generator.engine.ast.ValueExpr; @@ -186,7 +187,6 @@ private static MethodDefinition createAddResponseMethod() { } private static MethodDefinition createSetResponsesMethod(Service service) { - // TODO(miraleung): Re-instantiate the fields here. VariableExpr responsesArgVarExpr = VariableExpr.withVariable( Variable.builder() @@ -490,6 +490,14 @@ private static Statement createHandleObjectStatement( } TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(Exception.class)); + Expr newExceptionExpr = + NewObjectExpr.builder() + .setType( + TypeNode.withReference(ConcreteReference.withClazz(IllegalArgumentException.class))) + .setArguments( + Arrays.asList( + ValueExpr.withValue(StringObjectValue.withValue("Unrecognized response type")))) + .build(); return IfStatement.builder() .setConditionExpr( @@ -524,6 +532,7 @@ private static Statement createHandleObjectStatement( MethodInvocationExpr.builder() .setMethodName("onError") .setExprReferenceExpr(responseObserverVarExpr) + .setArguments(Arrays.asList(newExceptionExpr)) .build()))) .build(); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java index e2f56d13a6..8fe7478590 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java @@ -123,7 +123,8 @@ public void generateServiceClasses() { + " } else if (response instanceof Exception) {\n" + " responseObserver.onError(((Exception) response));\n" + " } else {\n" - + " responseObserver.onError();\n" + + " responseObserver.onError(new " + + "IllegalArgumentException(\"Unrecognized response type\"));\n" + " }\n" + " }\n" + "\n" @@ -138,7 +139,8 @@ public void generateServiceClasses() { + " } else if (response instanceof Exception) {\n" + " responseObserver.onError(((Exception) response));\n" + " } else {\n" - + " responseObserver.onError();\n" + + " responseObserver.onError(new " + + "IllegalArgumentException(\"Unrecognized response type\"));\n" + " }\n" + " }\n" + "\n" @@ -156,7 +158,8 @@ public void generateServiceClasses() { + " } else if (response instanceof Exception) {\n" + " responseObserver.onError(((Exception) response));\n" + " } else {\n" - + " responseObserver.onError();\n" + + " responseObserver.onError(new " + + "IllegalArgumentException(\"Unrecognized response type\"));\n" + " }\n" + " }\n" + "\n" @@ -187,7 +190,8 @@ public void generateServiceClasses() { + " } else if (response instanceof Exception) {\n" + " responseObserver.onError(((Exception) response));\n" + " } else {\n" - + " responseObserver.onError();\n" + + " responseObserver.onError(new " + + "IllegalArgumentException(\"Unrecognized response type\"));\n" + " }\n" + " }\n" + "\n" @@ -218,7 +222,8 @@ public void generateServiceClasses() { + " } else if (response instanceof Exception) {\n" + " responseObserver.onError(((Exception) response));\n" + " } else {\n" - + " responseObserver.onError();\n" + + " responseObserver.onError(new " + + "IllegalArgumentException(\"Unrecognized response type\"));\n" + " }\n" + " }\n" + "\n" @@ -247,7 +252,8 @@ public void generateServiceClasses() { + " } else if (response instanceof Exception) {\n" + " responseObserver.onError(((Exception) response));\n" + " } else {\n" - + " responseObserver.onError();\n" + + " responseObserver.onError(new " + + "IllegalArgumentException(\"Unrecognized response type\"));\n" + " }\n" + " }\n" + "\n" @@ -262,7 +268,8 @@ public void generateServiceClasses() { + " } else if (response instanceof Exception) {\n" + " responseObserver.onError(((Exception) response));\n" + " } else {\n" - + " responseObserver.onError();\n" + + " responseObserver.onError(new " + + "IllegalArgumentException(\"Unrecognized response type\"));\n" + " }\n" + " }\n" + "\n" @@ -277,7 +284,8 @@ public void generateServiceClasses() { + " } else if (response instanceof Exception) {\n" + " responseObserver.onError(((Exception) response));\n" + " } else {\n" - + " responseObserver.onError();\n" + + " responseObserver.onError(new " + + "IllegalArgumentException(\"Unrecognized response type\"));\n" + " }\n" + " }\n" + "}\n";