Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Missing servlet context in spring tests

  1. #11

    Default

    My datasource-config.xml just contains one line for scanning for spring-data-jpa repositories...

    <jpa:repositories base-package="net.findme.server.repository"></jpa:repositories>

    And it's in src/main/resources..:\

  2. #12

    Default

    Well something you are doing in is trying to use a web bean as Marten stated. If its not in the configuration its somethinng you are doing in your test case. Can you provide a small sample application so I can reproduce this? I am doing exactly what it sounds like you are trying to do in mulitiple projects for my integration testing and I don't have this issue.

    Thanks,

  3. #13

    Default

    This is my testcase, that causes the exception above...

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes = {DataSourceConfig.class})
    @TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
    @Transactional
    public class RoleRepositoryTest {
    	/**
    	 * EntityManager for verifying the results of data manip1ulation by the {@code RoleRepository} interface
    	 */
    	@PersistenceContext
    	private EntityManager _entityManager;
    	
    	@Inject
    	private RoleRepository _repository;
    
    	@Inject
    	private RoleGenerator _roleGenerator;
    	
    	@Test
    	public void testSaveRole() {
    		final Role role = _roleGenerator.getRole();
    		assertNull(role.getPrimaryKey());
    		assertNull(role.getRoleId());
    		assertFalse(role.primaryKeyExists());
    		final Role saved = _repository.save(role);
    		assertTrue(role.getPrimaryKey() != null);
    		assertTrue(saved.getPrimaryKey() != null);
    		assertTrue(role.getPrimaryKey() == saved.getPrimaryKey());
    		final long id = role.getPrimaryKey();
    		final TypedQuery<Role> query = _entityManager.createQuery("from Role r where r.roleId = ?1", Role.class);
    		query.setParameter(1, role.getPrimaryKey());
    		
    		final Role result = query.getSingleResult();
    		assertEquals(result, role);
    		assertEquals(result.getRoleName(), role.getRoleName());		
    	}
    Here's an excerpt of my project folder, if you want to have a look...

    Thanks!
    Attached Files Attached Files

  4. #14

    Default

    I did not have your database set up and I did not have a chance to use an embedded one to mock it out but on first glance I can see you have

    @ComponentScan(basePackages = { "net.findme.server" }) on your dbconfig. Remove that line. You already have it in one of your other configs so its not needed and when you pull it in on your test case you are bringing in the web beans.

    Alternatively if you really want that in that config file create a DbTestConfig.java in your /src/test/java/ that looks like theh one you have but does not have that line. And bring in that config file on your test case. Either way I think that will solve your issue.

    Thanks,

  5. #15
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

  6. #16
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    FYI:

    This issue has been resolved.

    Specifically, as of Spring 3.2 RC1, you can annotate your test class with @WebAppConfiguration to instruct the Spring TestContext Framework to load a WebApplicationContext for your tests. This in turn ensures that a mock ServletContext is available, etc.

    See SPR-5243 for further details.

    Regards,

    Sam

  7. #17
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    markuse1501,

    As an aside, you can simplify your test configuration as follows:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = DataSourceConfig.class)
    @Transactional
    public class RoleRepositoryTest { /* ... */ }
    Note the omission of unnecessary default overrides.

    - Sam

Posting Permissions

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