如何使用 Spring Hateoas 为 Resource Assembler 编写 mockito 测试用例?
How to write a mockito test case for ResourceAssembler with in Spring Hateos?
我正在尝试为下面的汇编程序编写单元测试,但我一直收到 无法通过 RequestContextHolder 找到当前请求。这是从 Spring MVC 处理程序调用的吗?。我想知道如何模拟资源创建?
@Component
public class LoginResourceAssembler extends ResourceAssemblerSupport<User, ResourceSupport> {
public LoginResourceAssembler() {
super(User.class, ResourceSupport.class);
}
@Override
public ResourceSupport toResource(User user) {
ResourceSupport resource = new ResourceSupport();
final String id = user.getId();
resource.add(linkTo(MyAccountsController.class).slash(id).slash("accounts").withRel("accounts"));
return resource;
}
}
我看到错误无法通过 RequestContextHolder 找到当前请求。这是从 Spring MVC 处理程序 调用的,因为我的测试 class 是用 @RunWith(MockitoJUnitRunner.class) 注释的,并且这没有注入控制器。
为了修复这个错误,我用
注释了我的测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
我的案例中的一个工作测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class LoginResourceAssemblerTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@InjectMocks
private LoginResourceAssembler loginResourceAssembler;
@Before
public void setUp() {
initMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void testToResource() {
User user = new User();
user.setId("1234");
ResourceSupport resource = loginResourceAssembler.toResource(user);
assertEquals(1,resource.getLinks().size());
assertEquals("accounts",resource.getLinks().get(0).getRel());
assertTrue(resource.getLinks().get(0).getHref().contains("accounts"));
}
}
与其从普通单元测试更改为 IMO 集成测试(考虑到 spring 框架的依赖性),不如这样做:
@RunWith(MockitoJUnitRunner.class)
public class LoginResourceAssemblerTest {
@InjectMocks
private LoginResourceAssembler loginResourceAssembler;
@Before
public void setup() {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
}
@Test
public void testToResource() {
//...
}
}
我正在尝试为下面的汇编程序编写单元测试,但我一直收到 无法通过 RequestContextHolder 找到当前请求。这是从 Spring MVC 处理程序调用的吗?。我想知道如何模拟资源创建?
@Component
public class LoginResourceAssembler extends ResourceAssemblerSupport<User, ResourceSupport> {
public LoginResourceAssembler() {
super(User.class, ResourceSupport.class);
}
@Override
public ResourceSupport toResource(User user) {
ResourceSupport resource = new ResourceSupport();
final String id = user.getId();
resource.add(linkTo(MyAccountsController.class).slash(id).slash("accounts").withRel("accounts"));
return resource;
}
}
我看到错误无法通过 RequestContextHolder 找到当前请求。这是从 Spring MVC 处理程序 调用的,因为我的测试 class 是用 @RunWith(MockitoJUnitRunner.class) 注释的,并且这没有注入控制器。 为了修复这个错误,我用
注释了我的测试用例@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
我的案例中的一个工作测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class LoginResourceAssemblerTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@InjectMocks
private LoginResourceAssembler loginResourceAssembler;
@Before
public void setUp() {
initMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void testToResource() {
User user = new User();
user.setId("1234");
ResourceSupport resource = loginResourceAssembler.toResource(user);
assertEquals(1,resource.getLinks().size());
assertEquals("accounts",resource.getLinks().get(0).getRel());
assertTrue(resource.getLinks().get(0).getHref().contains("accounts"));
}
}
与其从普通单元测试更改为 IMO 集成测试(考虑到 spring 框架的依赖性),不如这样做:
@RunWith(MockitoJUnitRunner.class)
public class LoginResourceAssemblerTest {
@InjectMocks
private LoginResourceAssembler loginResourceAssembler;
@Before
public void setup() {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
}
@Test
public void testToResource() {
//...
}
}