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

@EnableBatchProcessing doesn't work with @DataJpaTest #10703

Closed
arthurc opened this issue Oct 19, 2017 · 9 comments
Closed

@EnableBatchProcessing doesn't work with @DataJpaTest #10703

arthurc opened this issue Oct 19, 2017 · 9 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@arthurc
Copy link

arthurc commented Oct 19, 2017

If I use @EnableBatchProcessing on a configuration class and then try to write a test with @DataJpaTest, the test blows up when EntityManager#flush() is invoked and throws the following error:

javax.persistence.TransactionRequiredException: no transaction is in progress

	at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1136)
	at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1297)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
	at com.sun.proxy.$Proxy84.flush(Unknown Source)
	at org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager.flush(TestEntityManager.java:172)
	at org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager.persistAndFlush(TestEntityManager.java:132)
	at com.example.jpaandbatch.entity.PersonTest.shouldWork(PersonTest.java:19)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I have put up a minimal project that illustrates a the issue over at https://github.com/arthurc/spring-boot-datajpatest-and-batch. Notice that if @EnableBatchProcessing is removed from https://github.com/arthurc/spring-boot-datajpatest-and-batch/blob/master/src/main/java/com/example/jpaandbatch/JpaAndBatchApplication.java then test (PersonTest) works fine.

The issue is in 1.5.8 and I've managed to backtrack the issue as far back as Spring Boot 1.4.2.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 19, 2017
@eddumelendez
Copy link
Contributor

I would suggest to move @EnableBatchProcessing to another class and keep clean your Application.java class. Remember that any class annotated by @Configuration is scanned automatically if they share the same root package.

Regarding your tests, I think you should create the right annotation to load batch infrastructure to work with jpa.

@arthurc
Copy link
Author

arthurc commented Oct 20, 2017

Yes, moving the @EnableBatchProcessing annotation to a separate class and excluding that from the test works. But I expected @DataJpaTest not to include batch stuff much the same way it isn't affected by web stuff for instance. The exception that was thrown doesn't really indicate that it was the @EnableBatchProcessing annotation that was the issue :)

What do you mean by creating the right annotation to load batch infrastructure?

@snicoll
Copy link
Member

snicoll commented Oct 20, 2017

But I expected @DataJpaTest not to include batch stuff much the same way it isn't affected by web stuff for instance.

True but the thing that's missing there is that @DataJpaTest (and others) need a source context to start your application. According to the documentation the default behaviour is to find a @SpringBootConfiguration which, in most cases, will be your @SpringBootApplication itself.

So @DataJpaTest will indeed only load JPA-related stuff. If you hardcode something else in your main Spring Boot app and you don't provide an alternative source for the context (see @SpringBootConfiguration and the doc I've mentioned) we're going to process with the main class with no way to exclude that.

@snicoll snicoll closed this as completed Oct 20, 2017
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 20, 2017
@snicoll
Copy link
Member

snicoll commented Oct 20, 2017

@arthurc I've created a separate issue to improve that section with what I've just explained.

@arthurc
Copy link
Author

arthurc commented Oct 20, 2017

Ahh, I see. I'll make sure not to clutter my main application class with additional @Enable*annotations. Thanks! :)

@dcdh
Copy link

dcdh commented Dec 22, 2017

Hello I am facing the same issue.

I would like to test my batch by populating data and next run the job using jobLauncherTestUtils.

I am using TestEntityManager persistAndFlush method to save my data. It is working only if I remove all stuff from SpringBatch however I get this error javax.persistence.TransactionRequiredException: no transaction is in progress

I"ve defined @DataJpaTest, @AutoConfigureTestEntityManager, @EnableBatchProcessing in other configuration classes with no luck.

So basically, is it possible to use @DataJpaTest with batch ? If yes how can it be done ?

Thanks by advance.

Regards, Damien

@snicoll
Copy link
Member

snicoll commented Dec 22, 2017

@dcdh this issue is closed and we don't use the tracker for questions. Please ask on StackOverflow or join us on Gitter.

@pawelwloch
Copy link

pawelwloch commented Nov 26, 2018

I had a hard time trying to make Spring Batch work with JPA and Junit. The issue javax.persistence.TransactionRequiredException: no transaction is in progress has been occuring all the time.

@wilkinsona
Copy link
Member

@pawelwloch There was nothing to resolve in Spring Boot itself. The problem above was that @EnableBatchProcessing was on the main application class so it was always being enabled even when only the JPA slice of the application was being tested. The solution is to move @EnableBatchProcessing onto a separate, Batch-focused configuration class. A recommendation to do this was added to the documentation as a result of this issue.

If you have any further questions, please follow up on Stack Overflow.
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

7 participants