0

I have the following test case. I am having issue with @Rollback(false). It is not working. But the junit works fine. I am using mongodb.

I tried to debug the code and I could dee the data in the database. But after the unit test case completes the data is not persisting in the db.

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()));
}

The other class is :

@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);
}
}

Any pointer would help.

Amit

4

1 回答 1

0

I found the reason....

If you closely analyze

@Before
public void setUp() 

In the AbstractIntegrationTest class it is removing all customers before running each test cases. That is why even though rollback is set to false the data is getting removed by the unit test.

@Thilo --- Yes mongodb supports rollback.

Thanks a lot for your time for looking into the issue.

Regards, Amit

于 2013-10-03T13:26:54.647 回答