2016-01-15 78 views
1

我在理解我在做什麼錯誤或缺少一些基本的東西時遇到了很多困難。我在一天左右的時間裏搜索了我的問題,並沒有理解我錯過了什麼。創建一個測試連接到SQL Server的Junit測試

所以我想要做的是創建一個JUnit測試,連接到我的SQL服務器並執行查詢以獲取當前時間。我與服務器的連接正常工作,並且已經在服務器上的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> 
+0

你可以將'applicationContext-sql.xml'添加到問題中嗎? –

回答

0

馬特,你正在登錄的字符串 「testPCNow()」 不能調用方法PCN低()。

所以更換測試方法:

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

記得用包含方法PCNow類的構造函數有效替代TheClassThatContainsTestPCNowMethod()。

+0

爲了讓這個類能夠運行,我不需要依賴關係和其他嗎?也許我只是不瞭解Junit如何在Spring中進行測試。我也試過,並得到一個空指針 – Matt

+0

嗨馬特,你可以請發佈堆跟蹤與NullPointerException? – leaqui

+0

我認爲問題是我的連接到我的SQL服務器無法正常工作我已經把這個系統連接起來,看看連接是否正常工作,它的返回值是否正確,所以我認爲這就是爲什麼我的班級工作不正常。除非你能幫助我,否則呢?但總的來說感謝你的幫助@Before \t public void setUp()throws Exception { \t \t System.out.println(connectionAdapterPC == null); \t \t System.out.println(dataSourcePC == null); \t} – Matt