java - Spring - Switch between loading test or main configuration -


I am trying to swap application-reference files so that I can test using the actual one in my IDE / JUnit with a test

An example of a bare bone would be:

mainConfig.xml

   & Lt; Bean id = "Maybian" class = "my.class" />   

testConfig.xml

  & lt; Reference: component-scan base-package = "myPackage" /> & Lt; Bean id = "Maybian" class = "my.test.class" />   

My Java code that relates to the context of the application is similar to the one below (but this is actually a singleton)

  Public Last Class MyLoader { Private ApplicationContext Reference; Public MyLoader () {context = new classpathxmlApplicationContext ("Classpath: / hardcoded / path / to / manconfig.exml"); } Public & lt; T & gt; T obtained time (final class  type) {return (t) context.getBean (type); }}   

Myloder is said to have many points in the code because there are many different entry points in the application.

My JUnit Test Code

  @RunWith (SpringJUnit4ClassRunner.class) @ContextConfiguration ("Classpath: / hardcoded / path / tet / test config. XML") Public Final Class When I test a JUnit, it will load testConfig.xml (due to @ ContextConfiguration in JUnit file), but this mainConfig.xml will also be loaded (ClassPathXmlApplication reason. From). Due to the separation of the two files, the reason for the collision will be.  

What is the correct way to ward off all this where there is no such mismatch?

A solution to your specific problem:
  • Cell Preferences . Keep main.xml and test.xml files in different directories, but use the same name for both (e.g. main / config.xml And test / config.xml ). Then configure your IDE so that when the jUnit is running, the test directory gets priority on the main directory on the main path.

    This is actually what you maven . In this case you may have to cook yourself by playing with your IDE.

  • Use that value in the code> MyLoader , maybe avoid additional configuration in default default execution on main.xml .

      Public MyLoader () {context = new classPathXmlApplicationContext (System.getProperty ("config.location", "classpath: path / to / mainConfig.xml")); After that, you can configure your IDE to set the environment variable when jUnit moves, or increases your test with the base class, which defines the variable :  
      System.setProperty ("config.location", "classpath: /path/to/testConfig.xml")   

    You actually have SpringJUnit4ClassRunner Is not required because you are using MyLoader

    These are not very clear solutions.

    A better solution at the expense of some refactoring:

    Remove MyLoader , retaining spring reference and / or obtaining beans Do not use singleton.

    The application is initializing the ClassPath XMLAPP contacts at your entry point, then you do not need ApplicationContext after getting the first bean / s.

    Use appropriate autowiring / injection for reliance between components instead of retrieving them from MyLoader .

    It is believed that this is a non-web application, to load the config xml to the web application using ContextLoaderListener .

    After following this approach, you should not have any problem loading a separate config.xml with SpringJUnit4ClassRunner because you do not have application from the entry point while running the test Are running

    And you will not try to load lazy every time a spring component is needed by MyLoader .

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

jsp - Google line chart x-axis shrinks on transition -

java - Reaching JTextField in a DocumentListener -