Warum wird nicht die Transaktion Anfang in meinem junit-Testfälle?

Habe ich eine Feder 3.1 MVC + Hibernate 3.6 Projekt mit junit4 test-Anzug. Mein problem ist, dass es keine Transaktion starten in meinem test-Fällen, sogar dachte, dass ich Hinzugefügt eine @Transactional.

Meinem test-Fall ruft ein controller und ein dao. In der Steuerung wird eine Transaktion gestartet wird, sowieso nicht zu beschweren. In dao habe ich noch eine @Transactional(propagation = Propagation.OBLIGATORISCH) sicher, dass er die test-Transaktion. Und derzeit wird eine IllegalTransactionStateException, die ich Schätze, bedeutet es, dass keine laufende Transaktion.

Habe ich versucht zu erstellen programmaticaly eine Transaktion, und es funktioniert, was bedeutet, dass der AOP-proxy um die dao-service ist nicht die Ursache des Problems. Aber ich möchte, um eine Transaktion zu erzeugen, mit der @Transactional annotation.

hier ist meine dao:

//...imports...

@Repository("taskDao")
@Transactional(propagation = Propagation.MANDATORY)
public class TaskHome implements TaskDao {

    private static final Log log = LogFactory.getLog(TaskHome.class);

    private SessionFactory sessionFactory;

    @Autowired
    public TaskHome(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Task findById(int id) {
        log.debug("getting Task instance with id: " + id);
        try {
            Task instance = (Task) this.sessionFactory.getCurrentSession().get(
                    Task.class, id); //exception raised here!
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    ...
}

Hier ist meine test-Fall:

//...imports...

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/test-config.xml", "/applicationContext.xml" })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class TestTaskController {

    private static ClassPathXmlApplicationContext context;
    private static TaskDao taskDao;

    @BeforeClass
    public static void initHibernate() throws Exception {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        taskDao = context.getBean("taskDao", TaskDao.class);
    }

    @Test
    public void testOnSubmit() {

        //expects an existing default transaction here

        Task task = taskDao.findById(1); //fails already here

        //... calls the controller and does some tests. 

    }

}

Suchte ich in allen Spring-Dokumentation und gegoogelt, es in jeder Weise könnte ich mir vorstellen, aber ich sehe nicht ein, warum die Transaktion nicht gestartet.
Jede Hilfe ist sehr willkommen.

was passiert, wenn Sie entfernen (propagation = Propagation.OBLIGATORISCH)?
es erstellt eine neue Transaktion, so dass das rollback.

InformationsquelleAutor edwin | 2012-07-15

Schreibe einen Kommentar