NovatecConsulting / BeanTest

Bean Testing for Java EE Applications
http://blog.novatec-gmbh.de/unit-testing-jee-applications-cdi/
Apache License 2.0
25 stars 12 forks source link

JUnit Runner API #1

Closed bbq2100 closed 7 years ago

bbq2100 commented 10 years ago

Features:

bbq2100 commented 10 years ago

Dropped the following feature :

There is no need for it since there are only two scopes available in the JUnit test environment: Request and ApplicationScoped. So the user should autom. be able to choose accordingly the desired scope.

bbq2100 commented 10 years ago

Regarding the feature of providing additional classes: this feature also has the following positive effect: there is no need for the user to provide a beans.xml in the test folder ( test/ressources. ) This implies that all classes, interceptor or extensions which are needed for running the test should be provided via this feature.

bbq2100 commented 10 years ago

After working with arquillian and the ShrinkWrap API, maybe BeanTesting should also provide such a feature regarding the creation of needed deployment descriptors (Convention over configuration): beans.xml, persistence.xml etc.a declarative way of setting deployment descriptors

agori commented 9 years ago

What about this?

@RunWith(BeanTest.class)
public class ServiceTest {

   @Inject MyService service;
   @Inject UserTransaction tx

    @Test @AutoRollback
    public void test() {
        myService.saveUser(new User("lagoria"));
    }

    @Test @ManualTransaction
    public void test() {
        tx.begin();
        myService.saveUser(new User("lagoria"));
        tx.rollback();

    }

}
bbq2100 commented 9 years ago

So you´re basically proposing a transaction control mechanism ('AutoRollback' and 'ManualTransaction') inside BeanTests? Further your code snippet contains ‘@Inject UserTransaction‘. Currently this is not possible...I just tried it out. So 'BeanTestingRunner' should also provide the current 'UserTransaction'.

carlosbarragan commented 9 years ago

I'm not sure that providing a facility to tests transactions is a good idea. IMHO if you need to test transaction behavior, you should refactor your code. BeanTest tries to hide the whole transaction propagation, so the user can concentrate on testing business logic inside an application module.

I also think this feature could be complicated to implement because we have to somehow expose the local transaction through UserTransaction. On the other hand, some time in the future we will probably have to provide access to the UserTransaction inside an EJB.

@qabbasi the injection of UserTransaction doesn't work because there is no Producer for it.

agori commented 9 years ago

@carlosbarragan its purpose is not to test transaction behavior but to give the possibility of rolling back the current transaction so that the database is not modified.

carlosbarragan commented 9 years ago

@agori why do you want to avoid DB updates? Every test method should start with a clean DB state.

agori commented 9 years ago

@carlosbarragan I am not fostering a rollback after each test, but it's a common technique used by a lot of developers. IMHO it would be nice to leave the freedom.

carlosbarragan commented 9 years ago

@agori IMHO rolling back the transaction is not a good practice. Even if you called the persist or merge method and rollback the transaction, you can still have DB-errors undiscovered because of the underlying database constraints.

Having a clean DB state at every test method is the best practice. Yes, the developers should have the freedom to choose but a framework should lead into using best practices.