嗨,我的单元测试控制器返回nullpointerexception。我把我的问题归结为注解或导入。我有这套图提拉做我的测试https://spring.io/guides/gs/testing-web/. 感谢您的帮助:)
@ExtendWith(SpringExtension.class)
@WebMvcTest(OfferCompanyController.class)
public class OfferCompanyControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private OfferCompanyService offerCompanyService;
@Test
public void getAllOfferCompanyTest() throws Exception {
List<OfferCompany> offerList = new ArrayList<>();
Date date = new Date();
offerList.add(new OfferCompany(1L, "wesharjob","premier post", "lorem ipsum", "la ville que tu veux", date, "user", "wesharejob.co", false));
offerList.add(new OfferCompany(2L, "wesharjob","premier post", "lorem ipsum", "la ville que tu veux", date, "user", "wesharejob.co", false));
given(offerCompanyService.getAllOfferCompany()).willReturn(offerList);
when(offerCompanyService.getAllOfferCompany()).thenReturn(offerList);
mockMvc.perform(get("/api/offerCompany/listOfferCompanies")
.contentType(MediaType.APPLICATION_JSON)
).andExpect(status().isOk()).andExpect(jsonPath("$", Matchers.hasSize(2)));
}
}
offerapplicationtests(我使用import“junit.test”)
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class OfferApplicationTests {
@Test
void contextLoads() {
}
}
暂无答案!
目前还没有任何答案,快来回答吧!