Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 406647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:31:00+00:00 2026-05-12T17:31:00+00:00

The following test illustrates that this test bean is initialized twice by Spring. I’m

  • 0

The following test illustrates that this test bean is initialized twice by Spring. I’m hoping someone can tell me why this is so, since it should only be once. Here’s the test:

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {} )
public class TestAfterPropsSet implements InitializingBean {

private static final Logger logger = Logger.getLogger(TestAfterPropsSet.class);

@Test
public void test1() {
    logger.debug("Test1");
}

@Test
public void test2() {
    logger.debug("Test2");      
}

public void afterPropertiesSet() throws Exception {
    logger.debug("Bean Initialized");       
}
} // end class

Here’s the bean file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

and here’s the output:

2009-10-13 21:20:04,393 [TestAfterPropsSet.java 26] DEBUG - Bean Initialized
2009-10-13 21:20:04,393 [TestAfterPropsSet.java 17] DEBUG - Test1
2009-10-13 21:20:04,393 [TestAfterPropsSet.java 26] DEBUG - Bean Initialized
2009-10-13 21:20:04,393 [TestAfterPropsSet.java 22] DEBUG - Test2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:31:01+00:00Added an answer on May 12, 2026 at 5:31 pm

    It’s not a Spring convention. You should be following JUnit conventions, i.e. suite-wide initialization or deconstruction should be done in @BeforeClass and @AfterClass accordingly, or you can use @Autowire and let Spring handle the object’s scope.

    A new suite will be constructed for each test. This is more apparent in JUnit3 where you had to create a new suite using a specified test name.

    Take a look at the JavaDoc:

    The Test annotation tells JUnit that
    the public void method to which it is
    attached can be run as a test case. To
    run the method, JUnit first constructs
    a fresh instance of the class then
    invokes the annotated method.
    Any
    exceptions thrown by the test will be
    reported by JUnit as a failure. If no
    exceptions are thrown, the test is
    assumed to have succeeded.

    Your use case is a bit puzzling since your test isn’t actually doing anything and there is no bean, which you reference. By default, Spring beans are declared with the default scope=”singleton” attribute, so had you actually declared a bean, it would have been a cached singleton. However, this has nothing to do with method execution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following test to verify that my repository is calling it's respective
I have the following test that passes: test should create order do assert_difference('Order.count') do
The following test fails: #!/usr/bin/env python def f(*args): >>> t = 1, -1 >>>
The following test case fails in rhino mocks: [TestFixture] public class EnumeratorTest { [Test]
When I run the following test in Gallio's Icarus it passes, but when I
When my browser renders the following test case, there's a gap below the image.
I am testing against the following test document: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC
I've isolated the behaviour into the following test case. I'd be grateful to anyone
Using NUnit 2.2 on .NET 3.5, the following test fails when using DateTime.Equals. Why?
I have written the following simple test in trying to learn Castle Windsor's Fluent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.