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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:43:47+00:00 2026-06-15T02:43:47+00:00

I have a class which looks like the below public class Abcd{ private @Value(${username})

  • 0

I have a class which looks like the below

public class Abcd{

private  @Value("${username}")
String username;

private @Value("${password}")
String password;

public Abcd(){
   ServiceService serv = new ServiceService();
   Service port = serv.getServicePort();
   BindingProvider bp = (BindingProvider) port;
   bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
   bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);

}

public void getSomeValueMethod(){
....
}

So how do I write a test for this? As I’m reading the values from the properties file, while testing when I try to call the constructor, as the username and password are null I get a null pointer exception and the test fails. Is there any way I can overcome this problem and test it successfully? How can I set those annotated values before calling the constructor?

  • 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-15T02:43:48+00:00Added an answer on June 15, 2026 at 2:43 am

    Just like everything injected by Spring: by injecting them yourself in the unit test:

    public class Abcd{
    
        private String username;
        private String password;
    
        public Abcd(@Value("${username}") userName, @Value("${password}") String password) {
            ...
        }
        ...
    }
    

    And in your unit test:

    Abcd abcd = new Abcd("someUserName", "somePassword");
    

    Remember that dependency injection’s main goal is to be able to manually inject fake or mock dependencies in unit tests.

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

Sidebar

Related Questions

I have abstract BaseController, which basically looks like below: public abstract class BaseController :
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
Good afternoon all, I have a class which looks like this: public class Grapheme
Suppose I have a function which looks like this: template <class In, class In2>
I have a function which looks something like this, it returns a noncopyable class
I have class which have one public method Start , one private method and
I have a simple servlet which looks like like below. Ite uses the Google's
I have a class definition that looks like this: public class SolrObj { [SolrUniqueKey(id)]
I have a DataTable which looks like below: TOTAL_CODE COD_NAME AP0001 School AP0002 Hospital
We have a modular MVVM application. One of the interfaces looks like below public

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.