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