0

I have a Bean with a property "DBServer"; It has a default of "location1";

During application startup, Depending on the value of "DBServer", different classes are instantiated.

I have 200 tests where this default setting is fine.

However, As of now,I would like to test the alternative.

Question:

Is there a way to from within a junit test case to reset the default before the application context starts ?

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
user215422
  • 49
  • 1
  • 8
  • 1
    You should consider using Spring Profiles. http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html – shazin May 15 '15 at 08:46
  • This is exactly the use case for profiles – jrao77 May 15 '15 at 11:30
  • I tried using @profiles("useProfile1"); – user215422 May 15 '15 at 11:37
  • @Resource public AnnotationConfigApplicationContext m_annotationConfigApplicationContext; .................... m_annotationConfigApplicationContext.getEnvironment().setActiveProfiles("useProfile1"); m_annotationConfigApplicationContext.getEnvironment().setActiveProfiles("useProfile1Supplementary"); m_annotationConfigApplicationContext.register(AppConfig.class); m_annotationConfigApplicationContext.refresh(); ((ConfigurableApplicationContext) m_annotationConfigApplicationContext).close(); – user215422 May 15 '15 at 11:38
  • But ... when i run it ............it goes looping when i do a refresh. – user215422 May 15 '15 at 11:53
  • Just to clarify when i set the active profile and refresh and close. – user215422 May 15 '15 at 12:04
  • It returns back to this code because I am refreshing ?? – user215422 May 15 '15 at 12:04

1 Answers1

1

Junit won't start application context unless you write setUp method under @before annotation. In that case you can initialize your bean with any values.

IThinkSo
  • 74
  • 4
  • Ok, Thank you; From here, how can i set a particular class property (for example)............. – user215422 May 15 '15 at 10:58
  • @Configuration public class configSetup { @Property(name="db.server", settable = Settable.OPTIONAL, defaultValue = "", description = "Host address.") public String dbServer = ""; ...................................... – user215422 May 15 '15 at 11:00