1

I am writing an integrated test for one of my Spring controllers. Test includes a configuration file(data-access-configuration.xml) that has DataSource configured using JNDI. When I am running this test I am getting an error error creating bean with name dataSource nested exception NoIntialContextException. Below is my Test class & Configuration file

Configuration:

<jee:jndi-lookup jndi-name="jobportal_db" id="dataSource"
    expected-type="javax.sql.DataSource">
</jee:jndi-lookup>

Test

@RunWith(value=SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "file:WebContent/WEB-INF/dispatcher-servlet.xml",
    "classpath:com/zerosolutions/configuration/service-configuration.xml",
    "classpath:com/zerosolutions/configuration/security-configuration.xml",
    "classpath:com/zerosolutions/configuration/data-access-configuration.xml",
    "classpath:com/zerosolutions/configuration/view-configuration.xml"})
public class TestingFrontController {

@Autowired
private WebApplicationContext wac;

private MockMvc mockMvc;

@Before
public void setUp(){
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}

@Test
public void getLoginSignupPage() throws Exception{
    mockMvc.perform(get("/login"))
    .andExpect(status().isOk())
    .andExpect(forwardedUrl("login"));
}

}

How can I fix it ?

Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
  • Possible duplicate: http://stackoverflow.com/questions/5940895/how-to-test-a-mocked-jndi-datasource-with-spring – Mohit Oct 30 '15 at 13:14

1 Answers1

0

This happens because you don't have a container or JNDI naming service running when you do your unit test.

I write unit tests with separate test config to inject dependencies. I also prefer injecting without Spring so I can use mocks.

My experience is that Spring bean factory slows tests down terribly, especially if I load it for every test class. I'm moving away from using Spring for unit tests beyond JDBC template. Unit tests should use mocks for dependencies.

duffymo
  • 305,152
  • 44
  • 369
  • 561