Giter Site home page Giter Site logo

Comments (25)

odrotbohm avatar odrotbohm commented on May 18, 2024 2

Looks like @sbrannen was kind enough to add support for an escape hatch by adding an explicit annotation on the test class in Spring Framework 5.1.

Would you mind giving this another look on 5.1, @thombergs?

from moduliths.

odrotbohm avatar odrotbohm commented on May 18, 2024

Ideally, I'd love to see the annotations to work properly in combination rather than having to replicate all of them into this project. I guess that's currently not working properly as Boot is not expecting multiple of these annotations to be used at the same time.

I'll take this request forward to the Boot team to see what we can do here.

from moduliths.

thombergs avatar thombergs commented on May 18, 2024

With "in combination", you mean "in combination with @ModuleTest (or any other scope-constraining annotation, for that matter)", so that it would look something like this, right?

@RunWith(SpringRunner.class)
@DataJpaTest
@ModuleTest
public class MyModuleDataLayerTests {
  // ...
}

from moduliths.

odrotbohm avatar odrotbohm commented on May 18, 2024

Exactly. Did you try that already?

from moduliths.

thombergs avatar thombergs commented on May 18, 2024

Did so just now:

@RunWith(SpringRunner.class)
@WebMvcTest
@ModuleTest
public class BookingControllerTest {
  ...
}

leads to

java.lang.IllegalStateException: Configuration error: found multiple declarations of 
@BootstrapWith for test class [com.acme.myproject.booking.web.BookingControllerTest]: 
[@org.springframework.test.context.BootstrapWith(value=class 
org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper), 
@org.springframework.test.context.BootstrapWith(value=class 
org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]

from moduliths.

odrotbohm avatar odrotbohm commented on May 18, 2024

Interesting. Does it also fail in this way if you use e.g. @DataJpaTest which also declares SpringBootTestContextBootstrapper? Nevermind, I just checked Spring Test Context Framework's code and it will.

@wilkinsona – it looks like Spring's BootstrapUtils.resolveExplicitTestContextBootstrapper(…) is rather strict as it already fails if two @BootstrapWith annotations are found without ever considering them to potentially point to the same (or at least compatible) bootstrappers. I guess it's something we need @sbrannen's help for.

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

I've created the following JIRA issue to address this (at least partially).

https://jira.spring.io/browse/SPR-17006

Feedback on the JIRA issue is welcome.

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

it looks like Spring's BootstrapUtils.resolveExplicitTestContextBootstrapper(…) is rather strict as it already fails if two @BootstrapWith annotations are found without ever considering them to potentially point to the same (or at least compatible) bootstrappers.

I actually just introduced a test for "the same" use case, and that already works since resolveExplicitTestContextBootstrapper() uses a Set.

For example, the bootstrapper resolved for DuplicateMetaAnnotatedBootstrapWithAnnotationClass is FooBootstrapper.

	@BootstrapWith(FooBootstrapper.class)
	@Retention(RetentionPolicy.RUNTIME)
	@interface BootWithFoo {}

	@BootstrapWith(FooBootstrapper.class)
	@Retention(RetentionPolicy.RUNTIME)
	@interface BootWithFooAgain {}

	@BootWithFoo
	@BootWithFooAgain
	static class DuplicateMetaAnnotatedBootstrapWithAnnotationClass {}

Thus, it would appear that only the "compatible" use case is not supported.

Can you confirm that?

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

For the new test, see: spring-projects/spring-framework@755add3

from moduliths.

odrotbohm avatar odrotbohm commented on May 18, 2024

According to Sam, @DataJpaTest and @ModuleTest should then already work together. Would you mind trying that, Tom?

from moduliths.

thombergs avatar thombergs commented on May 18, 2024

Yes, @DataJpaTest works in combination with @ModuleTest. I actually had already tried that already and found that all repositories are loaded and not just the ones from the module (see issue #4).

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

OK. Thanks for the confirmation.

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

@olivergierke, do you think you still need new support for the "compatible" use case in the TCF?

from moduliths.

odrotbohm avatar odrotbohm commented on May 18, 2024

@sbrannen – Ultimately, yes. I'd love to get the input of the Boot team (@wilkinsona, @philwebb) on whether using these annotations in combination is something they're intending to work towards. I totally understand that the annotations that Boot currently ships don't really create that scenario.

@thombergs – Re #4, did you see my comment there?

from moduliths.

odrotbohm avatar odrotbohm commented on May 18, 2024

@sbrannen – I've just tried a naive attempt for disambiguation by explicitly declaring an @BootstrapWith(…) locally hoping that local declaration would override the meta-ones defined in the other annotations:

@RunWith(SpringRunner.class)
@DataJpaTest
@WebMvcTest
@BootstrapWith(WebMvcTestContextBootstrapper.class)
public class SomeTest { … }

Is making that work maybe a cheap intermediate solution?

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

Ultimately, yes. I'd love to get the input of the Boot team (@wilkinsona, @philwebb) on whether using these annotations in combination is something they're intending to work towards. I totally understand that the annotations that Boot currently ships don't really create that scenario.

OK. Then I'll hold off until the Boot team provides feedback in this regard.

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

Is making that work maybe a cheap intermediate solution?

By "that" do you mean favoring a locally declared annotation over a meta-annotation on a locally declared composed annotation?

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

Oh wait.... embarrassing... just read your lead-in comment. 😇

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

In any case, to answer your question....

See https://jira.spring.io/browse/SPR-12602.

That was an issue raised by @philwebb to ensure that multiple competing declarations of @BootstrapWith are not allowed.

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

Is making that work maybe a cheap intermediate solution?

In light of SPR-12602, that would be a breaking change.

So I'm not so sure we should do that.......

from moduliths.

odrotbohm avatar odrotbohm commented on May 18, 2024

I’d argue it wouldn’t. The same code would still fail with the same error message. It would only allow the developer to resolve the ambiguity by adding an explicit override.

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

mmmm... The meta-presence of more than one such annotation would continue to be an error. So in that sense, I suppose you're correct.

Plus, if anyone had previously tried what you tried above, that would have failed then, and they would have stopped doing it.

So, yeah, it wouldn't actually be a breaking change to any "working" code. Rather, it would be a change in behavior that someone would have to opt in for.

In other words, I agree. 😉

from moduliths.

sbrannen avatar sbrannen commented on May 18, 2024

See updated deliverables for SPR-17006.

from moduliths.

tilm4nn avatar tilm4nn commented on May 18, 2024

I just tried to verify this with @BootstrapWith(WebMvcTestContextBootstrapper.class) on the test class.
Unfortunately this does not work with our SpringFramework in use (version 5.3.19) because class WebMvcTestContextBootstrapper is access level friendly and not public. So no access from Test.

In addition I tried to use @AutoConfigureMockMvc on the test class instead of @WebMvcTest.
This works well when used together with @SpringBootTest but unfortunately not with @ModuleTest.

from moduliths.

tilm4nn avatar tilm4nn commented on May 18, 2024

I just realized that the test works when I specify all three together

@ModuleTest
@SpringBootTest
@AutoConfigureMockMvc

But what does this mean for the bootsrapping? Does it initialize the whole application or only the current module?

from moduliths.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.