2
votes

Following is my objective:

  • To write Jupiter test case [Test case in Junit5] for Spring code in version 4.1.6.RELEASE.

I chose to do following [as Junit5 supports running Jupiter tests on Junit4 runner]:

  • Write a Jupiter test case [Test case in Junit5]
  • Run with Junit4 Runner [In IntelliJ IDE]

IDE and Code version Details:

  • Spring Version: 4.1.6.RELEASE
  • Java Version: 1.8 [Java 8]
  • IDE: IntelliJ Community 2018.3

Problem i face:

  • When i run the jupiter test case in IntelliJ IDE: i see the test case is run by junit5. [IntelliJ trace shows: "com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5"] Resultantly, the intended object is not getting autowired/injected in my test class.

I have added both junit4 and junit5 to classpath from code.

Can someone suggest how to configure intellij to take only junit4 runner everytime i run test.

Class which is to be Tested:

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Repository
public class MyClassImpl implements IMyClass {

@PersistenceContext
private EntityManager entityManager;

@Override
public String getName(Long Id) {
    String queryString = "some query string";
    Query query = entityManager.createNativeQuery(queryString);
    List<String> resultList = query.getResultList();
    return resultList.isEmpty() ? null : resultList.get(0);
}

Unittest code: TestClass to test MyClassImpl class.

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {EnvironmentConfig.class, DataConfig.class, WebConfig.class})
@ActiveProfiles("dev")
class MyClassImplTest {

    @Autowired
    private IMyClass myClassObj;

     @Test
    void getAssessmentNameTestIT() 
    {
        if (myClassObj == null)
            System.out.println(" ************ I am NULL ************");
        else {
            System.out.println(" ************ I am NOT NULL ************");
            myClassObj.getName(21L);
        }
    }
}

Note: Same test case if i write using only Junit 4 [without using any junit5 libraries], the intended object is getting injected.

Maven Dependencies:

  • junit-jupiter-api RELEASE
  • junit-jupiter-engine RELEASE
  • junit-platform-runner 1.2.0
  • junit 4.10 [test]

Maven Plugin:

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<dependencies>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-surefire-provider</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>RELEASE</version>
    </dependency>
</dependencies>

Thanks in advance

1

1 Answers

7
votes

@RunWith(SpringJUnit4ClassRunner.class) only works with JUnit 4, hence the "JUnit4" in the class name.

Thus you cannot use annotations from JUnit 5 (JUnit Jupiter) -- such as org.junit.jupiter.api.Test -- if you are using a JUnit 4 Runner. That will never work.

To use Spring's testing support with JUnit Jupiter, you have to use the SpringExtension instead of the SpringJUnit4ClassRunner or SpringRunner -- like this: @ExtendWith(SpringExtension.class).

The SpringExtension is officially supported as part of the spring-test artifact since Spring Framework 5.0: https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testcontext-junit-jupiter-extension

Note that it is possible to use the original version of the SpringExtension with Spring Framework 4.3.x if you use it from my GitHub repository: https://github.com/sbrannen/spring-test-junit5

In your case, it will not be possible to use Spring's testing support with JUnit 5 if you are still on Spring Framework 4.1.6.RELEASE. You will need to upgrade to at least Spring Framework 4.3.x or, preferably, Spring Framework 5.1.x.

Regards,

Sam (author of the Spring TestContext Framework)