0

I would like to convert code from https://github.com/mattnworb/hibernate-sample

so that it would not run from test directory, but from src dir, as a class with method main. In tests the dbunit database in xml file is correclty read.

I can see several problems with transforming tests to app:

  1. Tests are run with SpringJUnit4ClassRunner and test classes extend AbstractTransactionalJUnit4SpringContextTests. Applying this to class with main method resulted in context initialization exception.

  2. In Java main method is always static. If I try to inject fields like

@Autowired private SomeClass instance;

Then I cannot call it in main class, since injected field is not static (and cannot be)

What could be the solution?

4
  • It’s spring boot application ? Commented Dec 2, 2018 at 20:56
  • No. Do I need to go Spring Boot? I don't need Tomcat in my application Commented Dec 2, 2018 at 20:57
  • No this is fine Commented Dec 2, 2018 at 21:05
  • Added answer please take a look Commented Dec 2, 2018 at 21:38

1 Answer 1

1

You can just create application context object in your main class and get there bean :

ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(“your.package.of.beans”);
 // you are passing here the package where beans  are located  

  YourClass yourClass = ctx.getBean(YourClass.class);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.