2016-01-15 75 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