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

Allow Spring @Autowired dependency injections and Spring Boot integration #99

Closed
zifnab87 opened this issue Mar 1, 2017 · 3 comments
Closed

Comments

@zifnab87
Copy link

zifnab87 commented Mar 1, 2017

In case other users find it interesting you could include that in your Documentation:

I have created a BDDTest class that runs with Spectrum.java and also initializes Spring ApplicationContext

@RunWith(Spectrum.class)
@Ignore
@SpringBootTest
public class BDDTest {

    //global autowired objects
    @Autowired
    protected DbC conf;


    @Before
    public void beforeTest() {
        // do stuff
    }
    @After
    public void afterTest() {
        // do stuff
    }

    private TestContextManager testContextManager;

    public BDDTest() {
        try {
            this.testContextManager = new TestContextManager(getClass());
            this.testContextManager.prepareTestInstance(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

classes that are BDD-style can extend BDDTest like that:

public class AttributeControllerTest extends BDDTest {
    @Autowired
    private IAttributeService attributeService;
    private MockMvc mockMvc;

    {
        beforeAll(() -> {
            this.graph = graphService.getGraph();
            mockMvc = MockMvcBuilders
                    .standaloneSetup(new AttributeController(attributeService))
                    .setControllerAdvice(new GlobalExceptionHandler())
                    .build();
        });

        describe("after creation of attribute", () -> {
            Attribute storedAttribute = attributeService.storeAndFetch(new Attribute());

            describe( "fetchByUuid", () -> {
                it("should return 200", () -> {
                    mockMvc.perform(get("/api/attribute/" + storedAttribute.getUuid())).andExpect(status().isOk());
             });
        });
   }
}

Haven't tried it yet but constructor Dependency Injection using @Autowired should work too

Hope someone finds this helpful!

@rkawala
Copy link

rkawala commented Apr 7, 2017

Have a look at https://github.com/cloudfoundry-incubator/credhub. We've been using Spectrum to write Spring tests for almost a year, and now the project is open source so we can share this. There's probably a better way to do it, but it's worked for us so far.

@ashleyfrieze
Copy link
Contributor

There are limitations of this approach which are overcome by the junitMixin feature that is in the latest version of Spectrum. Please see the docs and post any questions on this thread.

@ashleyfrieze
Copy link
Contributor

I suggest we close this as I believe junitMixin and the native @Rule support should now render it obsolete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants