Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactional (mybatis-cdi: org.mybatis.cdi.Transactional) annotation with rollbackOnly=true not rollbacking transactions inside a Weld / JUnit 5 test #476

Open
danielemaddaluno opened this issue Jul 7, 2024 · 0 comments

Comments

@danielemaddaluno
Copy link

I'm not sure it's an issue of the Transactional annotation, it could be some missing configuration.

I have described the issue in detail on stackoverflow.

I'm pasting a copy here:

I expect this class UserMapperTest.java to run the test rollbacking the transaction (in this case for a simple insert, where the field email is unique, but the core of the problem is about the rollbacking):

import jakarta.inject.Inject;
import org.jboss.weld.junit5.WeldInitiator;
import org.jboss.weld.junit5.WeldJunit5Extension;
import org.jboss.weld.junit5.WeldSetup;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mybatis.cdi.Transactional;
import java.math.BigInteger;

@ExtendWith(WeldJunit5Extension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
@Transactional(rollbackOnly = true)
public class UserMapperTest {
	@WeldSetup
	WeldInitiator weldInitiator = WeldInitiator.of(WeldInitiator.createWeld().enableDiscovery());

	@Inject
	@TestSessionFactory
	public UserMapper userMapper;

	@Test
	public void t1_insert_user() throws Exception {
		User u = userMapper.selectUserFromId(BigInteger.ONE);
		u.setId(null);
		u.setEmail(u.getEmail() + "2");
		userMapper.insertUser(u);
		Assertions.assertNotNull(u);
		Assertions.assertNotNull(u.getId());
	}
}

The main pom.xml dependencies useful for this test are:

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jboss.weld/weld-junit5 -->
<dependency>
    <groupId>org.jboss.weld</groupId>
    <artifactId>weld-junit5</artifactId>
    <version>${weld.junit5.version}</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jboss.weld.se/weld-se-core -->
<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-core</artifactId>
    <version>${weld-se.version}</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/jakarta.platform/jakarta.jakartaee-api -->
<dependency>
    <groupId>jakarta.platform</groupId>
    <artifactId>jakarta.jakartaee-api</artifactId>
    <version>${jakartaee-api.version}</version>
    <scope>provided</scope>
</dependency>

with these versions:

<junit.version>5.11.0-M2</junit.version>
<weld.junit5.version>4.0.3.Final</weld.junit5.version>
<weld-se.version>6.0.0.Beta4</weld-se.version>
<jakartaee-api.version>11.0.0-M4</jakartaee-api.version>

My bean.xml is this:

<?xml version="1.0"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
   <interceptors>
      <class>org.mybatis.cdi.LocalTransactionInterceptor</class>
   </interceptors>
</beans>

This is the TestSessionFactory annotation:

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import jakarta.inject.Qualifier;

@Qualifier
@Retention(RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER })
public @interface TestSessionFactory {
}

And this is the SqlSessionFactoryProviderTest test implementation of the SqlSessionFactoryProvider:

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.cdi.SessionFactoryProvider;

public class SqlSessionFactoryProviderTest implements SqlSessionFactoryProvider {
	@Override
	@TestSessionFactory
    @Produces
    @ApplicationScoped
    @SessionFactoryProvider
    public SqlSessionFactory produceFactory() {
        return ConnectionFactory.getSqlSessionFactory();
    }
}

I used to use a similar code with javax and junit 4 and worked fine. I'm trying to upgrade to jakarta and junit 5. It's possible that I'm missing something on the WeldInitiator? The userMapper is injected correctly and the query runs correctly, simply the transaction is not rollbacked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant