Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Parameterized/Theories JUnit Tests

Tags:

java

junit

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?

like image 713
Scott Avatar asked Jan 23 '12 16:01

Scott


People also ask

What are parameterized tests used for in JUnit?

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values.

Can JUnit tests have parameters?

Overview. JUnit 5, the next generation of JUnit, facilitates writing developer tests with shiny new features. One such feature is parameterized tests. This feature enables us to execute a single test method multiple times with different parameters.

Is it possible to use JUnit 4 and JUnit 5 tests in the same test project or suite )?

Running tests with IntelliJ IDEA Vintage engine discovers and runs the JUnit 4 tests. Jupiter runs its own discovery and execution phases where it discovers and executes JUnit 5 tests only. So not that you can only run JUnit 4 and JUnit 5 tests in the same project, you can also do that in the same test class.


2 Answers

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.

like image 81
Scott Avatar answered Nov 09 '22 17:11

Scott


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;
      }
    });
  }
}
like image 31
Jan Goyvaerts Avatar answered Nov 09 '22 16:11

Jan Goyvaerts