我正在尝试使用JUnit 5测试我的Spring 5 Web控制器。 测试控制器的两种方法(在spring文档中提到)总是给我空指针。
这是我的测试类
import com.lacunasaurus.gamesexplorer.test.configuration.TestBackEndConfiguration;
import com.lacunasaurus.gamesexplorer.test.configuration.TestWebConfig;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@RunWith(JUnitPlatform.class)
@ExtendWith(SpringExtension.class)
@WebAppConfiguration()
@ContextConfiguration(classes = {TestWebConfig.class, TestBackEndConfiguration.class})
public class TestAuthenticationCreateAccountController {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
// this.mockMvc = MockMvcBuilders.standaloneSetup(new AuthenticationCreateAccountController()).build();
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void getAccount() throws Exception {
// Here i've got an null pointer
mockMvc.perform(get("/"));
}
}
这是我的测试网页配置
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@ComponentScan(basePackages = {"com.lacunasaurus.gamesexplorer.web"})
public class TestWebConfig implements WebMvcConfigurer, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
现在我的控制器
@Controller
@RequestMapping("/create-account")
public class AuthenticationCreateAccountController {
@Autowired
UserAccountValidator accountValidator;
@Autowired
AuthenticationService authenticationService;
@GetMapping
public String navigate() {
return "authentication/create-account";
}
@ModelAttribute("userAccount")
public UserAccount setDefaultAccount() {
return new UserAccount();
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.addValidators(accountValidator);
}
@PostMapping
public String createAccount(@Validated UserAccount userAccount, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "authentication/create-account";
}
authenticationService.createUserAccount(userAccount);
return "authentication/create-account";
}
}
编辑:IDE提供的堆栈跟踪
java.lang.NullPointerException at com.lacunasaurus.gamesexplorer.test.controller.TestAuthenticationCreateAccountController.getAccount(TestAuthenticationCreateAccountController.java:41)
Results :
Tests in error:
TestAuthenticationCreateAccountController.getAccount:41 NullPointer
我已经用junit 5和spring测试了我的后端,一切运行良好。
感谢那些帮助我了解如何测试控制器的人员。)
答案 0 :(得分:6)
控制器的新测试:
@ExtendWith(SpringExtension.class)
@WebAppConfiguration()
@ContextConfiguration(classes = {TestWebConfig.class, TestBackEndConfiguration.class})
@TestInstance(Lifecycle.PER_CLASS)
public class TestAuthenticationCreateAccountController {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@BeforeEach
void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
void getAccount() throws Exception {
mockMvc.perform(get("/toto")).andExpect(status().isOk());
}
}
答案 1 :(得分:2)
如果您使用的是Junit5,请参见此reference
@ExtendWith(MockitoExtension.class) // For Junit5
public class TestAuthenticationCreateAccountController {
@Mock
private WebApplicationContext wac;
@InjectMocks
private AuthenticationCreateAccountController ac;
private MockMvc mockMvc;
@BeforeEach // For Junit5
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(ac).build();
}
@Test
public void getAccount() throws Exception {
mockMvc.perform(get("/"));
}
}
答案 2 :(得分:1)
要使用JUnit Jupiter,必须使用自动配置MockMvc。这有助于解决所有相关的配置。
@ExtendWith(SpringExtension.class)
@WebAppConfiguration()
@AutoConfigureMockMvc
答案 3 :(得分:-3)
@Autowired
private MockMvc mockMvc;
这应该可以解决您的问题。