2013-01-18 25 views
9

मैं परीक्षण विधि लेनदेन के अंदर कुछ डेटाबेस संचालन करना चाहता हूं। मैं इसके लिए जूनिट टेस्ट नियम का उपयोग करना चाहता हूं। लेकिन नियम लेनदेन के बाहर निष्पादित किए जाते हैं। लेनदेन के अंदर मेरे नियमों को निष्पादित करने का कोई तरीका है?AbtrTransactionalJUnit4SpringContextTests लेनदेन के अंदर जूनिट नियम कैसे निष्पादित करें?

2013-01-18 10:33:24,845 DEBUG [main]: MySimpleSpringRule   - before 
2013-01-18 10:33:24,869 INFO [main]: ionalTestExecutionListener - Began transaction (1): transaction manager [[email protected]]; rollback [true] 
2013-01-18 10:33:24,869 DEBUG [main]: ProductServiceTest   - before 
2013-01-18 10:33:24,869 DEBUG [main]: ProductServiceTest   - running test... 
2013-01-18 10:33:24,984 DEBUG [main]: ProductServiceTest   - after 
2013-01-18 10:33:24,986 INFO [main]: ionalTestExecutionListener - Rolled back transaction after test execution for test context [[[email protected] testClass = ProductServiceTest, testInstance = [email protected], testMethod = [email protected], testException = [null], mergedContextConfiguration = [[email protected] testClass = ProductServiceTest, locations = '{classpath:/META-INF/spring/test-bean-configuration.xml, classpath:/META-INF/spring/applicationContext.xml, classpath:/META-INF/spring/infrastructure.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]] 
2013-01-18 10:33:24,987 DEBUG [main]: MySimpleSpringRule   - after 

@Before और @After परीक्षण कक्षा में सीधे तरीकों में

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { 
    "classpath:/META-INF/spring/applicationContext.xml"}) 
public class ProductServiceTest extends AbstractTransactionalJUnit4SpringContextTests { 

public @Rule @Autowired MySimpleSpringRule mySimpleSpringRule; 

@Before 
public void before() { 
    logger.debug("before"); 
} 

@After 
public void after() { 
    logger.debug("after"); 
} 

@Test 
public void testFindProducts() { 
... 

और

@Component 
public class MySimpleSpringRule implements TestRule { 

private class Banaani extends ExternalResource { 
    @Override 
    protected void before() throws Throwable { 
     logger.debug("before"); 
    }; 

    @Override 
    protected void after() { 
     logger.debug("after"); 
    }; 
} 

private final Banaani banaani = new Banaani(); 

@Override 
public Statement apply(Statement st, Description desc) { 
    return banaani.apply(st, desc); 
} 

परिणाम लेन-देन के अंदर क्रियान्वित कर रहे हैं, लेकिन MySimpleSpringRule में लोगों को बाहर क्रियान्वित कर रहे हैं लेन-देन।

उत्तर