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 8576445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:55:31+00:00 2026-06-11T19:55:31+00:00

*Please forgive the intricate title* Background /pom.xml … <foo.bar>stackoverflow</foo.bar> … /src/main/resources/config.properties … foo.bar=${foo.bar} …

  • 0

*Please forgive the intricate title*

Background

/pom.xml

...
<foo.bar>stackoverflow</foo.bar>
...

/src/main/resources/config.properties

...
foo.bar=${foo.bar}
...

Config.java

...

public final static String FOO_BAR;

static {
    try {
        InputStream stream = Config.class.getResourceAsStream("/config.properties");
        Properties properties = new Properties();
        properties.load(stream);
        FOO_BAR = properties.getProperty("foo.bar");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

...

Question

In /src/main/java, I’m using Config.FOO_BAR in MyClass.java. If I want to test MyClass in a /src/test/java folder using JUnit with MyClassTest.java, how can I load the properties so that the Config.FOO_BAR constant get initialized?

I tried to add a hardly-written config.properties within /src/test/resources with foo.bar=stackoverflow, but it still can’t get initialized.

  • 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-11T19:55:33+00:00Added an answer on June 11, 2026 at 7:55 pm

    I could make it work by changing some in your pom.xml and your Config.java.

    Add these lines to your pom.xml:

    <project>
        ...
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </project>
    

    And change the order of some lines in Config.java:

    public class Config {
        public final static String FOO_BAR;
    
        static {
            InputStream stream = Config.class.getResourceAsStream("/config.properties");
            Properties properties = new Properties();
            try {
                properties.load(stream);
            } catch (IOException e) {
                e.printStackTrace();
                // You will have to take some action here...
            }
            // What if properties was not loaded correctly... You will get null back
            FOO_BAR = properties.getProperty("foo.bar");
        }
    
        public static void main(String[] args) {
            System.out.format("FOO_BAR = %s", FOO_BAR);
        }
    }
    

    Output if running Config:

    FOO_BAR = stackoverflow
    

    Disclaimer

    I am not sure what purpose you have with setting these static config values. I just made it work.


    Edit after comment

    Added a simple JUnit test to src/test/java/:

    package com.stackoverflow;
    
    import org.junit.Test;
    
    import static org.junit.Assert.assertEquals;
    
    /**
     * @author maba, 2012-09-25
     */
    public class SimpleTest {
    
        @Test
        public void testConfigValue() {
            assertEquals("stackoverflow", Config.FOO_BAR);
        }
    }
    

    No problems with this test.

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

Sidebar

Related Questions

Please forgive the awkward title. I had a hard time distilling my question into
Please forgive me if this question is dense. Background: We have several internal applications
Please forgive the bad title - I had a hard time trying to think
Please forgive me. I am definitely a beginner at Haml and Ruby on Rails.
Please forgive me if this is a silly question. We're running into a problem
Please forgive me if this is an obvious one. I have an unknown amount
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
Please forgive my programming knowledge. I know this is a simple thing, but I
Please forgive the simplicity of this question. The answer will be obvious to many
Please forgive me if this is answered on SO somewhere already. I've searched, and

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.