[英]Spring MVC Test failing
I am trying to run a Spring MVC Test but keep getting this exception. 我正在尝试运行Spring MVC测试,但仍然遇到此异常。
org.springframework.web.util.NestedServletException: Request processing failed; org.springframework.web.util.NestedServletException:请求处理失败; nested exception is java.lang.NullPointerException
嵌套异常是java.lang.NullPointerException
The exception is occuring because the autowired dependency, 发生异常是因为自动连接依赖项,
@Autowired
private AccountService accountService;
is not getting injected in the test (works fine outside of the test). 没有被注入测试(在测试之外工作正常)。
Can anyone help me with this. 谁能帮我这个。 Here is my code:
这是我的代码:
//AccountControllerITest Class // AccountControllerITest类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class AccountControllerITest {
private MockMvc mvc;
ObjectMapper om;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new AccountController()).build();
}
@Test
public void getAccounts() throws Exception {
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/api/accounts"))
.andExpect(status().isOk())
.andReturn();
}
}
} }
//AccountController //的AccountController
@RestController
@RequestMapping("/api/accounts")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Set<AccountInfo>> getAccounts(@RequestParam(value = "firstName", required = false) String firstName,
@RequestParam(value = "surName", required = false) String surName) {
Set<AccountInfo> accounts = accountService.getAccounts(firstName, surName);
return new ResponseEntity<>(accounts, HttpStatus.OK);
}
} }
Thanks for the help! 谢谢您的帮助!
Because you are using standalone setup: mvc = MockMvcBuilders.standaloneSetup(new AccountController()).build();
因为您使用的是独立设置:
mvc = MockMvcBuilders.standaloneSetup(new AccountController()).build();
. 。 If you create controller via
new AccountController()
, Spring doesn't have a chance to inject accountService
as it doesn't control instances creation and wiring. 如果您通过
new AccountController()
创建控制器,Spring没有机会注入accountService
因为它不控制实例创建和连接。
You have two options: 您有两种选择:
Switch your test to unit test and do not use SpringJUnit4ClassRunner
nor MockServletContext
at all. 将测试切换到单元测试,根本不使用
SpringJUnit4ClassRunner
和MockServletContext
。 You can use @InjectMocks
to inject private accountService
: 您可以使用
@InjectMocks
注入私有accountService
:
public class AccountControllerITest { private MockMvc mvc; ObjectMapper om; @Mock private AccountController accountController = new AccountController(); @InjectMocks private AccountService accountService = new Mockito.mock(AccountService.class); @Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup(accountController).build(); }
There is also enhancement you can apply to your controller. 您还可以对控制器应用增强功能。 Replace field injection with constructor injection and you can pass
accountService
to controller via constructor in test. 使用构造函数注入替换字段注入,您可以通过test中的构造函数将
accountService
传递给控制器。 This way you don't need to use @InjectMocks
at all. 这样您根本不需要使用
@InjectMocks
。
Use webAppContext setup: 使用webAppContext设置:
@Autowired private WebApplicationContext webApplicationContext; @BeforeMethod public void init() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); }
You need to configure your test to inject your autowired properties. 您需要配置测试以注入自动装配的属性。 See the following code block:
请参阅以下代码块:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AccountControllerITest {
// ... your test code
@Configuration
public static class YourConfiguration {
@Bean
AccountService accountService() {
// init and return your AccountService
}
}
}
You can init your AccountService
with Mockito . 您可以使用Mockito初始化您的
AccountService
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.