1

I think that a piece of code under test should not know that it is under test. Therefore, this question represents an anti-pattern, but I have a need to configure a legacy application in a particular way 'if called from a JUnit integration test'. If you think that this smells like an application that does not use CDI you are correct.

I can always walk the stack to see if I was called from JUnit. I am hoping for something more elegant.

cmdematos
  • 883
  • 4
  • 12
  • 24
  • What would you need to configure? I'm thinking perhaps using a mock framework (4eg mockito + powermock) will help, or maybe some reflection from withing the JUnit test case to avoid modifying the production code for the sake of the tests. – Morfic Jul 02 '14 at 14:30
  • Thank you for your comment. The integration test needs to read in thousands of rows (about 4000) and update some of them, then repeat the process about 250 times with different data (now we are at 500000 mock objects). The test is multi-threaded and we specifically want the database in between. Remember I mentioned this is an integration test, it is not a test that is run during Continuos Integration and yes it does rely on the systems that integrates with. – cmdematos Jul 02 '14 at 14:40
  • Sorry, missed the fact that it's an integration testing, no mocks then. However you still have to let us know what you need to configure different in the tests. – Morfic Jul 02 '14 at 14:45
  • Amongst the many things we need to configure are JNDI data sources, system properties and the like. We are not running in a test harness (JUnit is that harness I guess) but the legacy code is a web app. – cmdematos Jul 02 '14 at 15:22

1 Answers1

2

How about using a system property that is set by the JUnit test, evaluated by the class under test and resetted by JUnit afterwards?

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72