我不知道在单元测试之前如何运行服务器?

时间:2016-07-08 10:43:50

标签: java mysql unit-testing tomcat

我正在测试dao图层。在插入实体之前,我必须初始化DataSource context.xml,但它在web.xml中描述

 <resource-ref>
        <res-ref-name>jdbc/MyChat</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

当我运行测试时出现错误

Running dao.UserDaoTest
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.fact
ory.initial
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
        at javax.naming.InitialContext.lookup(InitialContext.java:417)
        at com.art.utils.ConnectionPool.<init>(ConnectionPool.java:21)
        at com.art.dao.impl.DaoFactoryImpl.<init>(DaoFactoryImpl.java:18)
        at dao.UserDaoTest.init(UserDaoTest.java:26)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

请告诉我,我必须做些什么才能解决这个问题? 这是我的代码

public class DaoFactoryImpl implements DaoFactory {

    private ConnectionPool connectionPool;

    public DaoFactoryImpl() {
        connectionPool = new ConnectionPool();
    }

    private Connection getConnection(){
        try {
            return connectionPool.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public UserDaoImpl getUserDao() {
        return new UserDaoImpl(getConnection());
    }

    public SecurityUserDaoImpl getSecurityDao() {
        return null;
    }
}



    public abstract class BaseDaoTest {
        protected DaoFactory daoFactory;

        @Before
        public void init(){
            daoFactory = new DaoFactoryImpl();
        }

    }


    public class UserDaoTest extends BaseDaoTest{
        private UserDaoImpl userDao;
        private User user;

        @Before
        public void init(){
            userDao = daoFactory.getUserDao();
            SecurityUserDaoImpl securityDao = daoFactory.getSecurityDao();
            SecurityUserCredentials security = new SecurityUserCredentials();
            security.setAuthToken("TestToken");
            security.setRole(UserRole.REGISTER_USER);
            security.setActivate(true);
            SecurityUserCredentials resultSecurity =  securityDao.create(security);
            user = new User();
            user.setSecurity(resultSecurity);
            user.setLogin("TestLogin");
            user.setPassword("TestPass");
        }

        @Test
        public void testAddUserInDataBase(){
            User testUser  = userDao.create(user);
            assertNotNull(testUser);
            assertEquals(testUser.getLogin(), "TestLogin");
            assertEquals(testUser.getPassword(), "TestPass");
        }
    }

0 个答案:

没有答案