@Rollback(false)无效

时间:2013-10-02 04:47:46

标签: mongodb junit junit4

我有以下测试用例。我遇到了@Rollback的问题(假)。它不起作用。但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);
}
}

任何指针都会有所帮助。

阿米特

1 个答案:

答案 0 :(得分:0)

我找到了原因......

如果你仔细分析

@Before
public void setUp() 

在AbstractIntegrationTest类中,它在运行每个测试用例之前删除所有客户。这就是为什么即使rollback设置为false,单元测试也会删除数据。

@Thilo ---是mongodb支持回滚。

非常感谢您抽出时间研究这个问题。

此致 阿米特