我尝试为我的 Spring Boot 应用程序编写一些单元测试(到目前为止,它是一个仅用于用户管理的简单 CRUD 应用程序)。
@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
@WebAppConfiguration
public class UserControllerTest {
@MockBean
private UserService UserService;
@MockBean
private UserTypeService UserTypeService;
@MockBean
private UserTrainingService UserTrainingService;
@MockBean
private TrainingService trainingService;
@MockBean
private UserService userService;
@InjectMocks
UserController UserController;
@Autowired
private MockMvc mockMvc;
private static final int USER_ID = 1;
private User User1;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(UserController).build();
UserType UserType = new UserType();
UserType.setName("Regular");
// some further setup steps. Ommited for clarity
}
@Test
public void testInitUserDetailsView() throws Exception …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class Role {
private Integer id;
private String name;
private String description;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
public class UserRole {
private Integer id;
private User user;
private Role role;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
public class RoleCheckbox {
private Role role;
private Boolean checked;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
现在在我的服务类中,我得到了 Role 和 UserRole 对象的列表。角色列表包含所有可用角色,用户角色列表包含分配给特定用户的角色。
我想比较这两个列表并创建一个 RoleCheckbox 对象列表。因此,新创建的列表应包含所有角色和“已检查”字段,其值取决于用户的角色。
我已经解决了如下:
this.roleCheckboxes = new ArrayList<RoleCheckbox>();
for(Role role : roles) {
Boolean checked = false;
for (UserRole userRole : user.getUserRoles()) {
if(userRole.getRole() == role) …Run Code Online (Sandbox Code Playgroud) java ×2
collections ×1
for-loop ×1
java-8 ×1
java-stream ×1
junit ×1
spring ×1
spring-boot ×1
spring-mvc ×1