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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:17:08+00:00 2026-05-29T06:17:08+00:00

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={/applicationContext-test.xml}) @Transactional public class MyServiceTest { @Resource(name=myService) public MyService myService; @Test public void

  • 0
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext-test.xml"})
@Transactional
public class MyServiceTest {

    @Resource(name="myService")
    public MyService myService;

    @Test
    public void testSeomthing() {
    //do some asserts using myService.whatever()
    }
}

However the tests are based on data I migrate in, so every time I run my suite of tests I want to execute my unrelated migration code. I don’t want to run a @Before in each test class. I want to run it once at beginning of complete test process, where can I put this ?

  • 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-29T06:17:09+00:00Added an answer on May 29, 2026 at 6:17 am

    I would advice you to create a test bean somewhere with startup logic invoked in @PostConstruct:

    @Service
    public class TestBean {
    
        @PostConstruct
        public void init() {
            //startup logic here
        }
    
    }
    

    Obviously this bean should only be created for tests, the easiest way to achieve this is to place it in src/test/java in a package that is component-scanned by Spring for @Service-annotated classes.

    Note: you must remember that @PostConstruct is not running in a transaction! See How to call method on spring proxy once initialised.

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

Sidebar

Related Questions

I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
Running my my Test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {classpath:eq-mo-dcc-context-block.xml}) public class SpringTest { @Autowired
I have following class: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={classpath:/com/home/app/Home-ctx.xml}) public class LoginDetailsTest { public static void main(String[]
I've got the following Test-class, but it doesn't matter what I set as ContextConfiguration-locations
I have a spring application and I make my test class as follows: @RunWith(SpringJUnit4ClassRunner.class)
Suppose I have this: @Transactional(rollbackFor = NotificationException.class) public interface PersonManagerService { public void addPerson(Person
I find myself using two identical beans in my applicationContext.xml and my applicationContext-test.xml. I'd
Spring support JUnit quite well on that: With the RunWith and ContextConfiguration annotation, things
I have an interface like below public interface FooDAO { public void callA(String x);
I'm trying to use SpringJunit4ClassRunner to test my DAO classes without leaving data behind

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.