在Junit测试中是否可以发出发布请求并使用BDDMockito.given()设置响应RestController帖子的方法所使用的Service的返回?如果可能,该怎么办?
我正在尝试设置Service的返回值,并且在RestController方法中,Service返回null并且测试失败。
这些是代码:
测试:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SecurityBeanOverrideConfiguration.class, AccountApp.class})
public class UserSystemResourceIntTest {
@MockBean
private UserSystemService userSystemServiceMock;
private MockMvc restUserSystemMockMvc;
private UserSystem userSystem;
public static UserSystem createEntity(EntityManager em) {
UserSystem userSystem = new UserSystem()
.name(DEFAULT_NAME)
.email(DEFAULT_EMAIL)
.phone(DEFAULT_PHONE)
.documentCode(DEFAULT_DOCUMENT_CODE)
.documentType(DEFAULT_DOCUMENT_TYPE)
.gender(DEFAULT_GENDER)
.createdOn(null)
.updateOn(null)
.active(null)
.userId(null);
return userSystem;
}
@Before
public void initTest() {
userSystem = createEntity(em);
}
@Test
@Transactional
public void createUserSystem() throws Exception {
int databaseSizeBeforeCreate = userSystemRepository.findAll().size();
// Create the UserSystem
UserSystemDTO userSystemDTO = userSystemMapper.toDto(userSystem);
userSystemDTO.setLangKey(DEFAULT_LANG_KEY);
given(userSystemServiceMock.newAccount(userSystemDTO)).willReturn(createNewUserSystemDTO());
restUserSystemMockMvc.perform(post("/api/user-systems")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(userSystemDTO)))
.andExpect(status().isCreated()); // FAIL BECAUSE RETURN STATUS 500
// Validate the UserSystem in the database
List<UserSystem> userSystemList = userSystemRepository.findAll();
assertThat(userSystemList).hasSize(databaseSizeBeforeCreate + 1);
UserSystem testUserSystem = userSystemList.get(userSystemList.size() - 1);
assertThat(testUserSystem.getName()).isEqualTo(DEFAULT_NAME);
assertThat(testUserSystem.getEmail()).isEqualTo(DEFAULT_EMAIL);
assertThat(testUserSystem.getPhone()).isEqualTo(DEFAULT_PHONE);
assertThat(testUserSystem.getDocumentCode()).isEqualTo(DEFAULT_DOCUMENT_CODE);
assertThat(testUserSystem.getDocumentType()).isEqualTo(DEFAULT_DOCUMENT_TYPE);
assertThat(testUserSystem.getGender()).isEqualTo(DEFAULT_GENDER);
assertThat(testUserSystem.getCreatedOn()).isNotNull(); //isEqualTo(DEFAULT_CREATED_ON);
assertThat(testUserSystem.getUpdateOn()).isNull(); // EqualTo(DEFAULT_UPDATE_ON);
assertThat(testUserSystem.isActive()).isEqualTo(DEFAULT_ACTIVE);
assertThat(testUserSystem.getUserId()).isEqualTo(DEFAULT_USER_ID);
// Validate the UserSystem in Elasticsearch
verify(mockUserSystemSearchRepository, times(1)).save(testUserSystem);
}
private static UserSystemDTO createNewUserSystemDTO() {
UserSystemDTO newUserSystemDTO = new UserSystemDTO(
DEFAULT_USER_ID, DEFAULT_NAME, DEFAULT_EMAIL,
DEFAULT_PASSWORD, DEFAULT_PHONE, DEFAULT_PHONE,
DEFAULT_DOCUMENT_TYPE, DEFAULT_GENDER, DEFAULT_LANG_KEY,
null, null, null, null);
return newUserSystemDTO;
}
}
RestController:
@RestController
@RequestMapping("/api")
public class UserSystemResource {
private final UserSystemService userSystemService;
private final UserSystemQueryService userSystemQueryService;
public UserSystemResource(UserSystemService userSystemService, UserSystemQueryService userSystemQueryService) {
this.userSystemService = userSystemService;
this.userSystemQueryService = userSystemQueryService;
}
@PostMapping("/user-systems")
public ResponseEntity<UserSystemDTO> createUserSystem(@Valid @RequestBody UserSystemDTO userSystemDTO) throws URISyntaxException {
log.debug("REST request to save UserSystem : {}", userSystemDTO);
if (userSystemDTO.getId() != null) {
throw new BadRequestAlertException("A new userSystem cannot already have an ID", ENTITY_NAME, "idexists");
}
UserSystemDTO result = userSystemService.newAccount(userSystemDTO);
return ResponseEntity.created(new URI("/api/user-systems/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}
}
控制台错误:
java.lang.NullPointerException: null
at com.fournetwork.account.web.rest.UserSystemResource.createUserSystem(UserSystemResource.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
结果测试错误:
createUserSystem(com.fournetwork.account.web.rest.UserSystemResourceIntTest) Time elapsed: 205.661 s <<< FAILURE!
ava.lang.AssertionError: Status expected:<201> but was:<500>