How to tell spring to only load the needed beans for the JUnit test?

Consider adding default-lazy-init=”true” to your spring context xml beans tag (or add lazy-init=”true” to those specific beans that take a long time starting up). This will ensure that only those beans are created that called with applicationContext.getBean(class-or-bean-name) or injected via @Autowired / @Inject into your tests. (Some other types of beans like @Scheduled beans will … Read more

Initializing Spring bean from static method from another Class?

The factory-method should only contain the method name, not including the class name. If you want to use a static factory, give the class of the factory(!) to the bean declaration, if you want to use an instance factory, give the factory-bean to the bean declaration, but don’t give both: The class of the created … Read more

ApplicationContext and ServletContext

Servlet Context: It is initialized when a Servlet application is deployed. Servlet Context holds all the configurations (init-param, context-params, etc) of the whole servlet application. Application Context: It is a Spring specific thing. It is initialized by Spring. It holds all the bean definitions and life-cycle of the beans that are defined inside the spring … Read more

Failed to load ApplicationContext from Unit Test: FileNotFound

Try with the relative path using * @ContextConfiguration(locations = { “classpath*:spring/applicationContext.xml”, “classpath*:spring/applicationContext-jpa.xml”, “classpath*:spring/applicationContext-security.xml” }) If not look if your xml are really on resources/spring/. Finally try just on without location @ContextConfiguration({“classpath*:spring/applicationContext.xml”}) The other error that you´re showing is because you have this tag duplicated on applicationContext.xml and applicationContext-security.xml Duplicate <global-method-security>

How do you reset Spring JUnit application context after a test class dirties it?

Use @DirtiesContext to force a reset. For example I have: @ContextConfiguration(classes={BlahTestConfig.class}) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class SomeTest { @Autowired XXXX xx; @Autowired YYYY yy; @Before public void setUp() { MockitoAnnotations.initMocks(this); when(YYYY.newYY()).thenReturn(zz); } @Test public void testSomeTest() { XX.changeSomething(“StringTest”); XX.doSomething(); check_for_effects(); } @Test public void testSomeOtherTest() { XX.changeSomething(“SomeotherString”); XX.doSomething(); check_for_effects(); } From spring docs DirtiesContext Indicates … Read more

What is a NoSuchBeanDefinitionException and how do I fix it?

The javadoc of NoSuchBeanDefinitionException explains Exception thrown when a BeanFactory is asked for a bean instance for which it cannot find a definition. This may point to a non-existing bean, a non-unique bean, or a manually registered singleton instance without an associated bean definition. A BeanFactory is basically the abstraction representing Spring’s Inversion of Control … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)