Reputation: 41
Following is my objective:
I chose to do following [as Junit5 supports running Jupiter tests on Junit4 runner]:
IDE and Code version Details:
Problem i face:
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:
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
Upvotes: 2
Views: 2897
Reputation: 31177
@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)
Upvotes: 8