创建一个测试连接到SQL Server的Junit测试

时间:2016-01-15 17:22:53

标签: sql spring unit-testing junit integration-testing

在理解我做错了什么或遗漏一些基本的东西时,我遇到了很大的困难。我已经搜索了一天左右的问题而没有理解我错过了什么。

所以我要做的是创建一个连接到我的SQL服务器的JUnit测试并进行查询以获取当前时间。我与服务器的连接工作正常,我已经在服务器上的Query中测试了我的SQL代码,并且工作正常。由于某种原因,测试不是发送我的代码并返回任何东西..不确定我做错了如果这对于这种形式来说太广泛了(对此没什么新意义)

@Override
    public Timestamp PCNow() throws PCSQLException {

        //SQL Server uses GETDATAE
        String strSQL = "SELECT GETDATE()";;

        try {
            //Get a result set with the timestamp field
            Timestamp datTs = (Timestamp)jdbcTemplate.queryForObject( strSQL, Timestamp.class );

            //Make sure there is a result
            if ( datTs == null )
                //Throw an exception indicating the server could not give a time
                throw new PCSQLException( "UNABLE_SERVER_TIME" );

            return datTs;
        }
        catch (Exception e) {
            throw new PCSQLException( "This didn't work PCNow", e );
        }
    }

这是我的测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-sql.xml"} )
//instantiate TestExecutionListener class 
@TestExecutionListeners

public class ConnectionAdapterSQLTest {

  @Autowired
    ConnectionAdapterImpl connectionAdapterPC;

    private final Log log = LogFactory.getLog( getClass() );

    @Before
    public void setUp() throws Exception {

    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
    }


    @Test
    public final void testPCNow()  {
        log.info("testPCNow()");

        //fail("Not yet implemented");
    }

applicationContext-sql.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- connection to Sql Server using JDBC sqljdbc4.2 -->
    <bean id="dataSourcePC" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://******;databaseName=******" /> 
        <property name="username" value="******" />
        <property name="password" value="******" /> 
    </bean>

    <bean id="connectionAdapterPC"
        class="com.*******.*******.connections.ConnectionAdapterSQL">
        <property name="dataSource" ref="dataSourcePC" />
        <property name="useConnectionPool" value="false" />
    </bean>

    <bean id="dxDateTimeFormatter" class="com.*******.*******.data.format.DateTimeFormatter">
        <property name="dateFormat" value="dd-MMM-yyyy" />
    </bean>

</beans>

1 个答案:

答案 0 :(得分:0)

马特,你正在记录String&#34; testPCNow()&#34;不调用方法PCNow()。

所以替换测试方法:

@Test
public void testPCNow() {
    log.info(new TheClassThatContainsTestPCNowMethod().PCNow());
}

请记住使用包含方法PCNow()的类的有效构造函数替换TheClassThatContainsTestPCNowMethod。

相关问题