/testability-explorer/src/main/java/com/google/test/metric/example/NonMockableCollaborator/FinalMethodCantBeOverridden.java

http://testability-explorer.googlecode.com/ · Java · 49 lines · 18 code · 4 blank · 27 comment · 0 complexity · 42738e9a8138cedb3bd2ffab6ef99e75 MD5 · raw file

  1. /*
  2. * Copyright 2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.example.NonMockableCollaborator;
  17. /**
  18. * This class demonstrates that if a method is final, it cannot be
  19. * overridden to make a seam.
  20. */
  21. public class FinalMethodCantBeOverridden {
  22. /**
  23. * This class has a final method with a cyclomatic complexity of 2.
  24. */
  25. public static class HasFinalMethod {
  26. final String computeString() {
  27. boolean x = true;
  28. boolean a = x ? false : true;
  29. String b = a ? "hello" : "goodbye";
  30. return b;
  31. }
  32. }
  33. private HasFinalMethod collaborator;
  34. // Even though the collaborator is injected into the constructor...
  35. public FinalMethodCantBeOverridden(HasFinalMethod collaborator) {
  36. this.collaborator = collaborator;
  37. }
  38. /**
  39. * there is no seam between us and the computeString() method, so we have a
  40. * non-mockable cost of 2.
  41. */
  42. public void NeedsAnInstance() {
  43. collaborator.computeString();
  44. }
  45. }