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

Change all the publicly-visible enum-singletons to factory methods #441

Closed
ogregoire opened this Issue Mar 14, 2018 · 6 comments

Comments

Projects
None yet
2 participants
@ogregoire

ogregoire commented Mar 14, 2018

It's hard to test code containing FacesConfigXml without having a full blown Arquillian instance to fire up. Arquillian is nice for integration testing, but not all tests happen in integration tests. Some happen in unit-tests.

So it'd be nice to be able to mock FacesConfigXml, such as making an interface of it. This way you have the concurrency and serialization guarantees offered by the singleton-enum, and the flexibility of interfaces for testing.

Typical expected actions:

  1. Rename FacesConfigXml to ConcreteFacesConfigXml and make it package-private.
  2. Extract a public interface from ConcreteFacesConfigXml named FacesConfigXml.
  3. Add a static FacesConfigXml instance() method returning ConcreteFacesConfigXml.INSTANCE.
  4. Repeat this process for all the enum singletons.
@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Mar 18, 2018

Member

How would you mock the instance() method?

Member

BalusC commented Mar 18, 2018

How would you mock the instance() method?

@ogregoire

This comment has been minimized.

Show comment
Hide comment
@ogregoire

ogregoire Mar 18, 2018

Well, that's a static factory method returning the singleton... It's not a business method: it doesn't need to be mocked. Meaning that when you mock, you simply call FacesConfigXml facesConfigXml = mock(FacesConfigXml.class) and when you access the real instance (through a @Produces, for instance), call FacesConfigXml facesConfigXml = FacesConfigXml.instance().

Example:

Main code

class MyBean {
  @Inject FacesConfigXml facesConfigXml;
}

class Settings {
  @Produces FacesConfigXml getFacesConfigXml() {
    return FacesConfigXml.instance();
  }
}

Test

@Test void testMyBean() {
  MyBean mb = new MyBean();
  mb.facesConfigXml = mock(FacesConfigXml.class);
  // Perform test
}

ogregoire commented Mar 18, 2018

Well, that's a static factory method returning the singleton... It's not a business method: it doesn't need to be mocked. Meaning that when you mock, you simply call FacesConfigXml facesConfigXml = mock(FacesConfigXml.class) and when you access the real instance (through a @Produces, for instance), call FacesConfigXml facesConfigXml = FacesConfigXml.instance().

Example:

Main code

class MyBean {
  @Inject FacesConfigXml facesConfigXml;
}

class Settings {
  @Produces FacesConfigXml getFacesConfigXml() {
    return FacesConfigXml.instance();
  }
}

Test

@Test void testMyBean() {
  MyBean mb = new MyBean();
  mb.facesConfigXml = mock(FacesConfigXml.class);
  // Perform test
}
@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Mar 19, 2018

Member

Okay, makes sense.

Member

BalusC commented Mar 19, 2018

Okay, makes sense.

@ogregoire

This comment has been minimized.

Show comment
Hide comment
@ogregoire

ogregoire Mar 19, 2018

Of course, this is omnifaces, so I guess there's a slight possibility to have FacesConfigXml automagically produced directly as a singleton instead of having to declare the full producer ourselves? But that'd be a nice cherry on top: the main request here is to decouple the mandatory initialization from the interface.

ogregoire commented Mar 19, 2018

Of course, this is omnifaces, so I guess there's a slight possibility to have FacesConfigXml automagically produced directly as a singleton instead of having to declare the full producer ourselves? But that'd be a nice cherry on top: the main request here is to decouple the mandatory initialization from the interface.

BalusC pushed a commit that referenced this issue Mar 21, 2018

bauke
#441: Extract interface from FacesConfigXml and WebXml enum singletons
Took the opportunity to add CDI producers for them
@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Mar 21, 2018

Member

It's available in today's latest 3.1-SNAPSHOT.

CDI producers have been added as well.

Member

BalusC commented Mar 21, 2018

It's available in today's latest 3.1-SNAPSHOT.

CDI producers have been added as well.

@BalusC BalusC closed this Mar 21, 2018

@ogregoire

This comment has been minimized.

Show comment
Hide comment
@ogregoire

ogregoire Mar 22, 2018

Thanks for the fast handling of this request! Nice trick on the deprecated instances ;-)

ogregoire commented Mar 22, 2018

Thanks for the fast handling of this request! Nice trick on the deprecated instances ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment