public class FooTest {
//this test is succeeded
@Test(expected=NumberFormatException.class)
public void testException() {
throw new NumberFormatException();
}
}
Showing posts with label JUnit. Show all posts
Showing posts with label JUnit. Show all posts
Friday, 24 February 2012
JUnit: How to test if an exception is thrown from a test method
Labels:
JUnit
Wednesday, 30 November 2011
Spring+JUnit: the db results of a method are persisted during methods calls
Problem description
I want the db to become empty on each method call of a JUnit Test. My persistence.xml contains<property name="hibernate.hbm2ddl.auto" value="create-drop" />The service layer is transactional (@Transactional) and my test is transactional ("defaultRollback = true"). But db changes are persisted among method calls so my tests fail!
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class MyTest {
@Test
public void test1() {
/*db is empty*/
write sth to db;
/*db is empty*/
}
@Test
public void test2() {
/*db is NOT empty*/
}
Solution
Test should extend AbstractTransactionalJUnit4SpringContextTests
Labels:
JUnit,
Spring,
Testing,
Troubleshooting
Subscribe to:
Posts (Atom)