I'm trying to test my Spring 5 web controllers with JUnit 5. The two way to test controller (as mentionned in spring documentation) always give me null pointer.
This is my test class
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("/"));
}
}
Here my web configuration for tests
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;
}
}
And now my controller
@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";
}
}
EDIT : The stacktrace given by the IDE
java.lang.NullPointerException at com.lacunasaurus.gamesexplorer.test.controller.TestAuthenticationCreateAccountController.getAccount(TestAuthenticationCreateAccountController.java:41)
Results :
Tests in error:
TestAuthenticationCreateAccountController.getAccount:41 NullPointer
I've got already test my backend with junit 5 and spring and everything work well.
Thanks to thoses who will help me to understand how to test controller :)
Unit Tests should be written under the src/test/java directory and classpath resources for writing a test should be placed under the src/test/resources directory. For Writing a Unit Test, we need to add the Spring Boot Starter Test dependency in your build configuration file as shown below.
The @Profile(“test”) annotation is used to configure the class when the Test cases are running. Now, you can write a Unit Test case for Order Service under the src/test/resources package. The complete code for build configuration file is given below.
If you're using Junit5, See this 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("/"));
}
}
The new test for controllers :
@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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With