Results 1 to 2 of 2

Thread: JUnit 4x Test Suite and Spring

  1. #1
    Join Date
    Mar 2012
    Posts
    6

    Question JUnit 4x Test Suite and Spring

    Hi.

    I've got multiple data providers in my web application. Each one effectively loads data from DBMS.

    I need to apply something like SpringJUnit4ClassRunner but for test suite. How can I achieve this?

    Here is sample code for unit test:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })
    public class SecurityDataProviderTest {
    
    	private static HsqlDatabase database;
    
    	private static final int NORMAL_USER_ID = 10001;
    	private static final int DISABLED_USER_ID = 10002;
    	private static final int DELETED_USER_ID = 10003;
    	private static final int NOT_EXISTING_USER_ID = 10000;
    	private static final int ORGANIZATION_ID = 10001;
    
    	@Autowired
    	private SecurityDataProvider securityDataProvider;
    
    	@BeforeClass
    	public static void setUp() throws InstantiationException, IllegalAccessException,
    			ClassNotFoundException, SQLException, LiquibaseException {
    		database = new HsqlDatabase();
    		database.setUp("test");
    	}
    
    	@Test
    	public void getUser_UserExists_UserIsLoaded() {
    		User user = securityDataProvider.getUser(NORMAL_USER_ID);
    		Assert.assertNotNull(user);
    		Assert.assertEquals(NORMAL_USER_ID, user.getId());
    		Assert.assertEquals("John", user.getFirstName());
    		Assert.assertEquals("Doe", user.getLastName());
    		Assert.assertFalse(user.getIsDeleted());
    		Assert.assertFalse(user.getIsDisabled());
    
    		Organization organization = user.getOrganization();
    		Assert.assertNotNull(organization);
    		Assert.assertEquals(ORGANIZATION_ID, organization.getId());
    	}
    So if I've got several data providers, I need to create test context for each of them. I'm expecting to init test context and database one time per suite and reuse it for several test classes

    Please advise

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    With Spring 3.1 use profiles... Then for each test run select a different profile.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •