All my tests runs perfectly with import org.junit.Jupiter.api.Test; except those with junit. This test works only with import org.junit.Test;. With jupiter I have an error like "mockMvc is null". The problem is that maven is not running my junit tests with a mvn test, only the jupiter tests.
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.lessThan;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(classes = MyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
@ExtendWith(MockitoExtension.class)
@RunWith(SpringRunner.class)
public class EndpointControllerTest {
@Mock
private EndpointService endpointService; // mock the service
@InjectMocks
private EndpointController endpointController; // mock the controller
private MockMvc mockMvc;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(endpointController)
.build();
}
@Test
public void getEndpoint() throws Exception {
MvcResult result = mockMvc.perform(get("/endpoint")
.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("utf-8"))
.andExpect(status().isOk())
.andReturn();
}
}
I tried a lot of different annotations, a lot of junit versions, changed the name of the file
Aucun commentaire:
Enregistrer un commentaire