7

I'm looking to combine the flexibility of Spring Profiles and Configurations with the parallel running of JUnit tests which utilize either the Parameterized or Theories annotation. Is there any way to incorporate all of these features to get my unit tests running?

The problem I keep running into is the parameters need access to an injected bean, which isn't possible since the function annotated with @Parameters or @DataPoints is supposed to be static. I'd really hate to have to wire that into each class or even a static function somewhere because I'd like to quickly be able to switch profiles without having to change Java code. Is this possible?

1

2 Answers 2

2

Found the ticket for this request. It seems the attached file has some issues though. Looks like it's been a feature request for quite some time now.

2

I've been looking for a solution of this problem too. And there is one ! But as it comes from somebody's blog, I can't take the credit for it however. :-)

Unfortunately I can't find the original blog any more...

@RunWith(Parameterized.class)
@ContextConfiguration("/beans.xml")
public class MyTest {

  private final File file;

  public MyTest(final File file) {
    this.file = file;
  }

  @Autowired
  private PlatformTransactionManager transactionManager;

  private TestContextManager testContextManager;

  @Parameterized.Parameters
  public static Collection<File[]> getFilesToTest() throws Exception {
    return getValidFiles();
  }

  @Before
  public void setUpSpringContext() throws Exception {
    testContextManager = new TestContextManager(getClass());
    testContextManager.prepareTestInstance(this); // does the autowiring !
  }

  @Test
  public void testInTransactionContext() throws Exception {
    new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
      public Object doInTransaction(final TransactionStatus status) {
        status.setRollbackOnly();
        try {
          ... run the test ...
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        return null;
      }
    });
  }
}
5
  • 1
    Hard to see what this gains us without seeing the implementation of getValidFiles is. We've though about a method similar to this which would call a webservice to return our parameters.
    – Scott
    Jun 19, 2012 at 15:17
  • It's just an example. :-) As long as it's a collection of arrays of objects (who came up with that by the way ?). Methods annotated with @Parameterized.Parameters will be called once to get the collection. After, for each element in the collection, an instance will be created using the constructor (hence the constructor with a File argument). Jun 19, 2012 at 18:45
  • 1
    My hope is to be able to inject the parameters though.
    – Scott
    Jun 19, 2012 at 19:21
  • You mean as general properties for application context ? Jun 20, 2012 at 5:36
  • For example, what if you want your parameters (test data) to be someService.getSomeData(), and someService is autowired in? Dec 2, 2013 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.