我在理解我在做什麼錯誤或缺少一些基本的東西時遇到了很多困難。我在一天左右的時間裏搜索了我的問題,並沒有理解我錯過了什麼。創建一個測試連接到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>
你可以將'applicationContext-sql.xml'添加到問題中嗎? –