0

Tomcatを使用してWebページにEXT JSフォームを入力するために、EXT JSと休止状態でSpringを使用しています...データベースにアクセスしてHQLステートメントを返すメソッドを使用してテーブルにデータを入力しています

今、返されたレコードの数をカウントするために単純な JUnit テストを実行しようとしていますが、EXT JS フォームに入力する JUint テストでメソッドを呼び出すと、この例外が返されます...理由はわかりません

JUnit テスト クラス

package com.fexco.helloworld.web;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.fexco.helloworld.web.dao.CustomerDaoImpl;
import com.fexco.helloworld.web.model.Customer;

/**
* Unit test for simple App.
*   /
public class CustomerServiceTest {

@Autowired
private CustomerDaoImpl customerDaoImpl;

@Test
public void findAllCustomersTest() {        
    List<Customer> list = customerDaoImpl.findAllCustomers();
    int numberInDatabase = list.size();
    assertEquals(5, numberInDatabase);
}

}

データベースにアクセスする私の方法

public List<Customer> findAllCustomers(){
    List<Customer> list = getHibernateTemplate().find("from Customer");
    return list;
}

そして、データベースにアクセスするメソッドを呼び出す私のメソッド

public List<Customer> returnAllCustomers(){
    List<Customer> list = customerDaoImpl.findAllCustomers();
    return list;
}

ここで、junit(findAllCustomers()) と同じメソッドを使用して、フォームにデータベースのアイテムが取り込まれていることがわかります。

ここに画像の説明を入力

誰にもアイデアはありますか?

4

2 に答える 2

5

クラス CustomerServiceTest の前に次の行を追加します

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = { "file:src/main/resources/app-context.xml" })
 public class CustomerServiceTest {....

src/main/resources/app-context.xml は、アプリケーション コンテキストへのパスです。テスト用に別のコンテキストを作成するのは良いことです。

編集

spring-test.jarまた、クラスパスにある必要があります。Maven の依存関係:

       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-version}</version>
        </dependency>
于 2012-04-17T15:41:46.440 に答える
0

単体テストで Spring コンテキストをロードしていません。

ここでSpringのドキュメントを見ることができます:http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/testing.html

そして例:Spring:JUnit-Test:applicationContextがロードされていません

于 2012-04-17T15:38:15.253 に答える