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

TestEntityManager initialize to null #415

Closed
ksundaysky opened this issue Jul 29, 2019 · 4 comments · Fixed by #455
Closed

TestEntityManager initialize to null #415

ksundaysky opened this issue Jul 29, 2019 · 4 comments · Fixed by #455

Comments

@ksundaysky
Copy link

Hi,

I'm trying to inject TestEntityManager to Stage.

I use @DataJpaTest. My test class look like this:

@RunWith(SpringRunner.class)
@DataJpaTest
@EnableJGiven
public class BookedRentalJGivenJpaTest extends SpringScenarioTest<BookedRentalGiven, BookedRentalWhen, BookedRentalThen>

And if I want to inject TestEntityManager to Given stage:

@JGivenStage
public class BookedRentalGiven extends Stage<BookedRentalGiven> {

@Autowired
private TestEntityManager testEntityManager;

it is initialized to null and NPE is thrown.

Am I doing something wrong?

@jackblack81
Copy link

jackblack81 commented Aug 1, 2019

Same for me. I would even say that @JGivenStage isn't working as it should

@janschaefer
Copy link
Contributor

Could you check the Spring logs for errors? Because I was able to inject the TestEntityManager. See https://github.com/TNG/JGiven/blob/master/example-projects/spring-boot/src/test/java/com/tngtech/jgiven/example/springboot/JpaTest.java

@Hatzen
Copy link
Contributor

Hatzen commented Apr 14, 2020

I m facing the same issue. I guess the difference in your samples is that you are using a SimpleSpringRuleScenarioTest< Stage > instead of SpringScenarioTest<GivenStage, WhenStage, ThenStage> the annotation is only applied to the givenstage in my case
In our project we also have a Stage working with inject, it is also just a simple stage model. To me it looks like that is the problem. The error log does not provide further information in my case.

@leimer
Copy link
Contributor

leimer commented May 15, 2020

It looks like the spring config is the root-cause. Lets look at the documentation of @DataJpaTest

Annotation that can be used in combination with @RunWith(SpringRunner.class) for a typical JPA test. Can be used when a test focuses only on JPA components.
Using this annotation will disable full auto-configuration and instead apply only configuration relevant to JPA tests.
By default, tests annotated with @DataJpaTest are transactional and roll back at the end of each test. They also use an embedded in-memory database (replacing any explicit or usually auto-configured DataSource). The @AutoConfigureTestDatabase annotation can be used to override these settings.

As you can see, @DataJpaTest disables the auto-configuration. This also includes the default Spring-Boot @ComponentScan which would pick up the Stage classes (@JGivenStage is meta-annotated with @Component).

As a workaround you can just define this on your test class:
@ComponentScan(includeFilters = @ComponentScan.Filter(JGivenStage.class))
But be aware this will activate every JGivenStage in your project. Which also means you need to provide every dependency the stages need. Those are normally not active in a DataJpaTest. To fine-tune that you can adapt the @ComponentScan to your needs.

I am also thinking about adding this to the @EnableJGiven annotation and make it overwriteable by the meta-annotation mechanism of Spring. I will discuss this option with @janschaefer.

leimer added a commit to leimer/JGiven that referenced this issue May 15, 2020
leimer added a commit to leimer/JGiven that referenced this issue May 15, 2020
leimer added a commit to leimer/JGiven that referenced this issue May 15, 2020
leimer added a commit to leimer/JGiven that referenced this issue May 15, 2020
@componentscan ist not active when using @DataJpaTest. Adding it manually works for now. We should discuss if we want to support that mechanism of Spring Data more deeply.
@leimer leimer mentioned this issue May 15, 2020
janschaefer pushed a commit that referenced this issue May 16, 2020
janschaefer pushed a commit that referenced this issue May 16, 2020
@componentscan ist not active when using @DataJpaTest. Adding it manually works for now. We should discuss if we want to support that mechanism of Spring Data more deeply.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants