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

  • Home
  • SEARCH
  • 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 8340345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:09:27+00:00 2026-06-09T05:09:27+00:00

I’m working with Spring 3.0 and jUnit 4.8 and I’m trying to develop some

  • 0

I’m working with Spring 3.0 and jUnit 4.8 and I’m trying to develop some unit test .

In fact, I’m only trying to set a property (a file) of a bean using dependency injection in a test case definined in the XML loaded in the application context used by the jUnit.

I’m using an XML file configuration loaded using the annotation for jUnit 4 aproach. This is the main BaseTest that all test classes used:

@ContextConfiguration("/test-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Ignore
public class BaseTest { ... }

And this is a section of the test-context.xml

<context:component-scan base-package="com.test" />  

<bean id="ExtractorTest" class="com.test.service.ExtractorTest">
    <property name="file" value="classpath:xls/SimpleFile.xls"></property>
</bean>

So what I’m trying to do in my class with the test (ExtractorTest) is only set the ‘file’ property with a file loaded within the classpath, nothing else.

Here is a section of the class with the test:

public class ExtractorTest extends BaseTest {

    private Resource file;
    private InputStream is;

    public void setFile(Resource file) {
        this.file = file;
    }

    @Before
    public void init() {
        this.is = this.getClass().getResourceAsStream("/xls/SimpleFile.xls");
        Assert.assertNotNull(is);
    }

    @Test
    public void testLoadExcel() throws IOException {
        // file is always null, but the InputStream (is) isn't!
        Assert.assertNotNull(file.getFile());
        InputStream is = new FileInputStream(file.getFile());
        HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is));
        // todo...
    }

}

The problem is that the setter works, because I’ve added a breakpoint and Spring is setting it’s property ok. But when the test method starts it’s null, may be because it’s another instance that is running, but why? How I can set the ‘file’ to be loaded using the XML of the application context for testing? I’m not able to assign it using jUnit and I don’t understand why and how to do it. I’m trying to avoiding writing in the @Before method, but I don’t know it’s a good approach definitely…

Thank you.

PD: Sorry about my english 😉

  • 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-06-09T05:09:28+00:00Added an answer on June 9, 2026 at 5:09 am

    Your configuration doesn’t work because Spring doesn’t create the instance of ExtractorTest which JUnit uses; instead the instance is created by JUnit and then passed to Spring for post processing.

    The effect you see is because the application context creates a bean with the id ExtractorTest but nobody ever uses that.

    Pseudocode:

    ApplicationContect appContext = new ...
    appContext.defineBean("ExtractorTest", new ExtractorTest()); // Calls setter
    
    ExtractorTest test = new ExtractorTest(); // Doesn't call setter
    test.postProcess(appContext); // inject beans from appContext -> does nothing in your case
    

    So the solution is to define a bean file:

    <bean id="file" class="..." />
    

    (see the documentation how to build a Resource bean) and then let Spring inject that:

    @Autowired
    private Resource file;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters

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.