0

次のテストケースがあります。@Rollback(false) に問題があります。機能していません。しかし、junit は正常に動作します。私はmongodbを使用しています。

コードをデバッグしようとしましたが、データベース内のデータを確認できました。しかし、単体テスト ケースが完了した後、データはデータベースに保持されません。

public class CustomerRepositoryIntegrationTest extends AbstractIntegrationTest {

@Autowired
CustomerRepository repository;

@Test
@Rollback(false)
public void savesCustomerCorrectly() {

    EmailAddress email = new EmailAddress("alicia@keys.com");

    Customer dave = new Customer("Alicia", "Keys");
    dave.setEmailAddress(email);
    dave.add(new Address("27 Broadway", "New York", "United States"));

    Customer result = repository.save(dave);
    assertThat(result.getId(), is(notNullValue()));
}

他のクラスは次のとおりです。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationConfig.class })
public abstract class AbstractIntegrationTest {

@Autowired
Mongo mongo;

@Before
public void setUp() {

    DB database = mongo.getDB("e-store");

    // Customers

    DBCollection customers = database.getCollection("customer");
    customers.remove(new BasicDBObject());

    BasicDBObject address = new BasicDBObject();
    address.put("city", "New York");
    address.put("street", "Broadway");
    address.put("country", "United States");

    BasicDBList addresses = new BasicDBList();
    addresses.add(address);

    DBObject dave = new BasicDBObject("firstname", "Dave");
    dave.put("lastname", "Matthews");
    dave.put("email", "dave@dmband.com");
    dave.put("addresses", addresses);

    customers.insert(dave);

    // Products

    DBCollection products = database.getCollection("product");
    products.drop();

    DBObject iPad = new BasicDBObject("name", "iPad");
    iPad.put("description", "Apple tablet device");
    iPad.put("price", 499.0);
    iPad.put("attributes", new BasicDBObject("connector", "plug"));

    DBObject macBook = new BasicDBObject("name", "MacBook Pro");
    macBook.put("description", "Apple notebook");
    macBook.put("price", 1299.0);

    BasicDBObject dock = new BasicDBObject("name", "Dock");
    dock.put("description", "Dock for iPhone/iPad");
    dock.put("price", 49.0);
    dock.put("attributes", new BasicDBObject("connector", "plug"));

    products.insert(iPad, macBook, dock);

    // Orders

    DBCollection orders = database.getCollection("order");
    orders.drop();

    // Line items

    DBObject iPadLineItem = new BasicDBObject("product", iPad);
    iPadLineItem.put("amount", 2);

    DBObject macBookLineItem = new BasicDBObject("product", macBook);
    macBookLineItem.put("amount", 1);

    BasicDBList lineItems = new BasicDBList();
    lineItems.add(iPadLineItem);
    lineItems.add(macBookLineItem);

    DBObject order = new BasicDBObject("customer", new DBRef(database,                "customer", dave.get("_id")));
    order.put("lineItems", lineItems);
    order.put("shippingAddress", address);

    orders.insert(order);
}
}

どんなポインタでも役に立ちます。

アミット

4

1 に答える 1

0

その理由が分かった……。

細かく分析すると

@Before
public void setUp() 

AbstractIntegrationTest クラスでは、各テスト ケースを実行する前にすべての顧客を削除しています。これが、ロールバックが false に設定されていても、単体テストによってデータが削除される理由です。

@Thilo --- はい、mongodb はロールバックをサポートしています。

この問題を調査していただき、ありがとうございます。

よろしく、アミット

于 2013-10-03T13:26:54.647 に答える