2

I am trying to write a integration test using DBUnit and SpringFramework. I have copied the code that i have write but i have problem with in-memory database connection. I have also copied the stack trace.

  @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    @Transactional
    @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
            TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
    @DatabaseSetup(type = DatabaseOperation.CLEAN_INSERT, value = "login.xml")
    public class LoginControllerTest {

        private MockMvc mockMvc;

        @Before
        public void setUp() {
            mockMvc = MockMvcBuilders.xmlConfigSetup("applicationContext.xml").build();
        }

        @Test
        @ExpectedDatabase("login.xml")
        public void testShowForm() throws Exception {
            mockMvc.perform(get("/login")).andExpect(status().isOk()).andExpect(view().name("/login"))
                    .andExpect(forwardedUrl("/WebContent/j/login.jsp"))
                    .andExpect(model().attribute("form", hasProperty("id", nullValue())))
                    .andExpect(model().attribute("form", hasProperty("email", isEmptyOrNullString())))
                    .andExpect(model().attribute("form", hasProperty("username", isEmptyOrNullString())))
                    .andExpect(model().attribute("form", hasProperty("hostname", isEmptyOrNullString())))
                    .andExpect(model().attribute("form", hasProperty("pass", isEmptyOrNullString())));
        }
    }

login.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <person id="1" email="[email protected]" username="qwerty"
        hostname="asdf.com" pass="password1234" />

    <person id="2" email="[email protected]" username="zxcvb"
        hostname="asdf.com" pass="password1234" />
</dataset>

Stack Trace

java.lang.IllegalStateException: Unable to find a DB Unit database connection, missing one the following beans: [dbUnitDatabaseConnection, dataSource]
    at com.github.springtestdbunit.DbUnitTestExecutionListener.getDatabaseConnectionUsingCommonBeanNames(DbUnitTestExecutionListener.java:111)
    at com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestExecutionListener.java:94)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
0

1 Answer 1

1

If you already configured the transactionManager and dataSource, most probably the problem is that the configuration file is not found in the location you've given.

If it is located in the classpath, you should put the classpath: prefix to the config location string.

mockMvc = MockMvcBuilders.xmlConfigSetup("classpath:applicationContext.xml").build();
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.