6

Junitジャージ API をテストするために使用しています。データベースなしで DAO をテストしたい。Mockito を使用してみましたが、モック オブジェクトを使用して DB への Hibernate 呼び出しを含む DAO をテストすることはできません。DAO を呼び出すヘルパー クラスを作成したいと考えてJunitいます。DAO で DB 接続をモックするためのサンプル コードを使用してソリューションを提供できる人はいますか。

編集 :

Status.java

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getDBValue() throws SQLException {
    DatabaseConnectionDAO dbConnectiondao = new DatabaseConnectionDAO();
    String dbValue = dbConnectiondao.dbConnection();
    return dbValue;
}

データベース接続DAO.java

private Connection con;
private Statement stmt;
private ResultSet rs;
private String username;

public String dbConnection() throws SQLException{
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
        stmt = con.createStatement();
        rs =stmt.executeQuery("select * from test");

        while(rs.next()){
            username = rs.getString(1);             
        }           
    }catch(Exception e){
        e.printStackTrace();            
    }finally{
    con.close();    
    }
    return username;
}

TestDatabase.java

@Test
public void testMockDB() throws SQLException{
    DatabaseConnectionDAO mockdbDAO = mock(DatabaseConnectionDAO.class);
    Connection con = mock(Connection.class);
    Statement stmt = mock(Statement.class);
    ResultSet rs = mock(ResultSet.class);

    Client client = Client.create();
    WebResource webResource = client.resource("myurl");
    ClientResponse response = webResource.accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);

    verify(mockdbDAO).dbConnection();

    //when(rs.next()).thenReturn(true);
    when(rs.getString(1)).thenReturn(value);    

    actualResult = response.getEntity(String.class);
    assertEquals(expectedResult,actualResult );
}
4

2 に答える 2

23

DAO をどのように嘲笑すべきかという考えを見逃しているかもしれません。接続について心配する必要はありません。一般に、そのメソッドが呼び出されたときに何が起こるか、たとえばメソッドをモックしたいだけですfindXxx。たとえば、このDAOインターフェースがあるとします

public interface CustomerDAO {
    public Customer findCustomerById(long id);
}

あなたはそれを次のように嘲笑することができます

CustomerDAO customerDao = Mockito.mock(CustomerDAO.class);

Mockito.when(customerDao.findCustomerById(Mockito.anyLong()))
        .thenReturn(new Customer(1, "stackoverflow"));

次に、そのモックされたインスタンスに依存するクラスに「注入」する必要があります。たとえば、リソース クラスで必要な場合は、コンストラクターを介して注入できます。

@Path("/customers")
public class CustomerResource {
    
    CustomerDAO customerDao;
    
    public CustomerResource() {}
    
    public CustomerResource(CustomerDAO customerDao) {
        this.customerDao = customerDao;
    }
    
    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findCustomer(@PathParam("id") long id) {
        Customer customer = customerDao.findCustomerById(id);
        if (customer == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        return Response.ok(customer).build();
    }
}

...

new CustomerResource(customerDao)

いいえ、メソッドをヒットするfindCustomerと、DAO はモックされた DAO で常に Customer を返します。

ジャージー テスト フレームワークを使用した完全なテストを次に示します。

public class CustomerResourceTest extends JerseyTest {

    private static final String RESOURCE_PKG = "jersey1.stackoverflow.standalone.resource";
    
    public static class AppResourceConfig extends PackagesResourceConfig {

        public AppResourceConfig() {
            super(RESOURCE_PKG);
            
            CustomerDAO customerDao = Mockito.mock(CustomerDAO.class);
            Mockito.when(customerDao.findCustomerById(Mockito.anyLong()))
                    .thenReturn(new Customer(1, "stackoverflow"));
           
            getSingletons().add(new CustomerResource(customerDao));
        }

    }

    @Override
    public WebAppDescriptor configure() {
        return new WebAppDescriptor.Builder()
                .initParam(WebComponent.RESOURCE_CONFIG_CLASS,
                        AppResourceConfig.class.getName()).build();
    }

    @Override
    public TestContainerFactory getTestContainerFactory() {
        return new GrizzlyWebTestContainerFactory();
    }
    
    @Test
    public void testMockedDAO() {
        WebResource resource = resource().path("customers").path("1");
        String json = resource.get(String.class);
        System.out.println(json);
    }
}

Customerクラスは単純な POJO で、とlong idがありString nameます。The Jersey Test Framework の依存関係は次のとおりです。

<dependency>
    <groupId>com.sun.jersey.jersey-test-framework</groupId>
    <artifactId>jersey-test-framework-grizzly2</artifactId>
    <version>1.19</version>
    <scope>test</scope>
</dependency>

アップデート

上記の例では、OP が Jersey 1 を使用していることがわかったので、Jersey 1 を使用しています。Jersey 2 を使用した完全な例 (注釈の挿入あり) については、この投稿を参照してください。

于 2014-12-22T03:21:44.100 に答える
8

簡単な答えはありません

単体テストが必要なコードは DAO のクライアントであるため、モック化する必要があるのは DAO です。DAO は、アプリを外部システム (ここではデータベース) と統合するコンポーネントであるため、統合テストとしてテストする必要があります (つまり、実際のデータベースと)。

于 2014-12-22T10:20:50.030 に答える