Spring Parameterized / Theories JUnit Tests

I want to combine the flexibility of Spring Profiles and Configurations with running JUnit tests in parallel, which use either Parameterized or Theories annotation. Is there a way to enable all of these features to run my unit tests?

The problem that I continue to work with is the parameters that require access to the entered bean, which is impossible, since the function annotated with @Parameters or @DataPoints should be static. I would really like it to be associated with every class or even some kind of static function, because I would like to quickly switch profiles without having to change Java code. Is it possible?

+6
source share
2 answers

A ticket was found for this request. There seems to be some problems in the attached file. It seems like this has been a feature request for quite some time.

+2
source

I was looking for a solution to this problem. And there is one! But, as it comes from a blog, I can’t take responsibility for it. :-)

Unfortunately, I can no longer find the original blog ...

@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; } }); } } 
+2
source

Source: https://habr.com/ru/post/906683/


All Articles