2

   
public void Method1() {
restTemplate = new RestTemplate();
}

public void Method2() {
restTemplate = new RestTemplate();
}

public void Method50() {
restTemplate = new RestTemplate();
}

....  

Cant we create a mock of restTemplate and reuse it in all methods ?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
user754657
  • 388
  • 3
  • 14
  • What exactly is the question? If you instantiate restTemplate inside your method, then no, you cannot put a mock there. If you don't, you can. In most cases, JUnit will run echt method on it's own, anyway, so you will end up creating more restTemplates than you probably think you do. – Florian Schaetz Feb 14 '16 at 08:47

1 Answers1

1

Yes. You can just create a single instance and re-use it in all the cases. If you use a real RestTemplate in an integration test (say hitting an in-memory service or similar) this will be safe too as RestTemplate is thread-safe.

Community
  • 1
  • 1
leeor
  • 17,041
  • 6
  • 34
  • 60