Issue
I am trying to run spring boot crud example with following code
@SpringBootTest @RunWith(MockitoJUnitRunner.class)
public class CreateUserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private CreateUserService createUserService;
@Test
public void whenSaveUser_shouldReturnUser() {
User user = new User();
user.setName("Test Name");
when(userRepository.save(ArgumentMatchers.any(User.class))).thenReturn(user);
User created = createUserService.createNewUser(user);
assertThat(created.getName()).isSameAs(user.getName());
verify(userRepository).save(user);
}
}
But after run it gives bellow error.
"NO test found with test runner 'junit5'"
anyone please help me.
Please check the error message from this file.
Solution
Edit: I found the root cause.
JUnit 5 uses annotations from a different package than JUnit 4. When you start a test, Eclipse automatically creates a launch configuration for JUnit 5, but if your annotations are not in the new package, Eclipse would not find the tests and will display the error message.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
// ... other imports from org.junit.jupiter.api
Old annotations:
import org.junit.Test;
// ... imports without "jupiter" in their name
You need to add the following dependencies in a Maven project in order to use JUnit 5:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
Workaround: Use JUnit 4. You need to select JUnit 4 in your launch configuration. Otherwise this error is shown.
From the main menu select Run -> Run configurations, then select your JUnit test configuration from the navigation bar on the left hand side.
Answered By - roccobaroccoSC