Mithlesh Singh
Mithlesh Singh

Reputation: 137

Spring 1 with Junit 4

I am trying to Unit test of spring 1 service method but during execution fetching "bean is undefined" .. so my concern is that Is it possible to load spring 1 beans in Junit 4 test file with below code

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration ("/someclasspath/applcationContextOfSpring1.xml")

Or is there any other way to use Junit 4 and Spring 1 together ?

Upvotes: 0

Views: 84

Answers (1)

Florian Schaetz
Florian Schaetz

Reputation: 10662

Well, I have never used Spring 1, but according to the documentation, (Spring 1.2.9) something like this should work...

public class MyTest {

    private MyBean myBean; // The spring bean you want to test

    @Before
    public void initBean() {
       ClassPathResource res = new ClassPathResource("application-context.xml");
       XmlBeanFactory factory = new XmlBeanFactory(res);
       this.myBean = factory.getBean("myBean", MyBean.class);
    }

    @Test
    public void test_something() {
        ...
    }
}

There are other ways according to the documentation, but this seems to be the easiest. On how to use the BeanFactory itself, you can refer to this documentation.

Upvotes: 1

Related Questions