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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:24:02+00:00 2026-05-22T18:24:02+00:00

I have the following configuration bean for a non web app @Configuration public class

  • 0

I have the following configuration bean for a non web app

@Configuration
public class MyBeans {

    @Bean
    @Scope(value="prototype")
    MyObject myObject() {
        return new MyObjectImpl();
    }
}

On the other side I have my class

public class MyCommand implements Command {

  @Autowired
  private MyObject myObject;

  [...]

}

How can I make myCommand be autowired with the configuration in MyBeans without using XML so I can inject mocks in my other test classes?

Thanks a lot in advance.

  • 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-22T18:24:03+00:00Added an answer on May 22, 2026 at 6:24 pm

    With XML-based configuration you’d use the ContextConfiguration annotation. However, the ContextConfiguration annotation doesn’t appear to work with Java Config. That means that you have to fall back on configuring your application context in the test initialization.

    Assuming JUnit4:

    @RunWith(SpringJUnit4ClassRunner.class)
    public class MyTest{
    
        private ApplicationContext applicationContext;
    
        @Before
        public void init(){
            this.applicationContext = 
                new AnnotationConfigApplicationContext(MyBeans.class);
    
                //not necessary if MyBeans defines a bean for MyCommand
                //necessary if you need MyCommand - must be annotated @Component
                this.applicationContext.scan("package.where.mycommand.is.located");
                this.applicationContext.refresh(); 
    
            //get any beans you need for your tests here
            //and set them to private fields
        }
    
        @Test
        public void fooTest(){
            assertTrue(true);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following spring bean configuration <bean id=fileBean class=java.io.File> <constructor-arg type=java.lang.String value=$prop{file.path.property} />
I have the following configuration: @Aspect public class MyAspect { @Around(@annotation(SomeAnnotation)) public Object myMethod(ProceedingJoinPoint
I have the following configuration in my applicationContext.xml: <bean class=org.springframework.beans.factory.config.PropertyPlaceholderConfigurer> <property name=locations> <list> <value>classpath:app.properties</value>
Let's say I have the following dependencies: @Configuration public class MyCfg { // ...
I have the following definition... <bean id=fsi class=org.springframework.security.intercept.web.FilterSecurityInterceptor> <property name=authenticationManager ref=authenticationManager/> <property name=accessDecisionManager ref=httpRequestAccessDecisionManager/>
I have the following XML configuration: <bean id=bean1 class=Simple/> <bean id=bean2 class=Simple/> <bean id=tasks
I have the following log4j.xml configuration: <log4j:configuration> <appender name = CONSOLE class = org.apache.log4j.ConsoleAppender>
I have the following virtual host configuration <VirtualHost petyo.net:443> ServerAdmin m@petyo.net DocumentRoot /home/ren/public/ <Directory
I have the following spring configuration: <context:component-scan base-package=uk.co.mysite.googlecontactsync.aop/> <bean name=simpleEmailSender class=uk.co.mysite.util.email.simple.SimpleEmailSenderImplementation/> <aop:aspectj-autoproxy/> Then I
I have exported Spring managed service beans into RMI service using following configuration: <bean

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.