[英]Unit testing code that relies on a SessionManager class with a dependency on HttpContext.Current
[英]Unit testing a class that relies on an EJB and UserTransaction
如何为包含EJBContext对象的类编写单元测试? 即
public class MyProcessor {
public MyProcessor (EJBContext ejbContext, SomePojo somePojo) { }
ReturnObject saveMethod(InputObject inputObject){
UserTransaction utx = this.context.getUserTransaction();
try {
utx.begin();
....
....
}
return ReturnObject;
}
}
我正在使用Mockito和Junit。 我已经阅读了很多初学者教程,但找不到想要测试的类的任何可靠信息。
到目前为止,我已经通过了:
@Mock
@RunWith(MockitoJUnitRunner.class)
public class MyProcessorTest{
@Mock
MyProcessor myProcessor;
@Test
public void testSaveNewAssignment() {
InputObject inputObject= new InputObject();
ReturnObject returnObject = new ReturnObject();
when(myProcessor.saveMethod(inputObject)).thenReturn(returnObject);
ReturnObject actualReturnObject= myProcessor.saveMethod(inputObject);
assertSame(returnObject, actualReturnObject);
}
我不确定这实际上在做什么,以及这是否是正确的解决方法。
许多模拟示例使用“ new”关键字来实例化我正在测试的类。 但是,当我在MyProcessor上调用new关键字时,我猜想模拟了EJBContext和SomePojo,但是当我这样做时,我在UserTransaction utx中收到java.lang.NullPointerException错误。
更新:感谢RadekSohlich的回答,我使用了InjectMocks批注,并且没有使我的处理器类成为我正在做的模拟(当然是错误的)。
发生的另一件有趣的事情是,我不得不模拟UserTransaction,并使用Mockito的“ when”关键字来告诉它使用该模拟。 否则我将有一个nullpointerException。
这是有效的解决方案的示例:
@RunWith(MockitoJUnitRunner.class)
public class MyProcessorTest {
@Mock
private InputObject inputObject
@Mock
private UserTransaction utx;
@Mock
private EJBContext context;
@Mock
private SomePojo somePojo
@InjectMocks
private MyProcessor myProcessor;
public MyProcessorTest() {
}
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(context.getUserTransaction()).thenReturn(utx);
}
@Test
public void testSaveNewAssignment_instantiate() {
ReturnObject response = myProcessor.saveMethod(inputObject);
// no exception encountered
assertTrue(true);
}
}
好吧,关键是对要测试的bean所依赖的对象/对象使用注释@Mock。 例如,我正在测试类AssignmentRest,该类依赖于AssignmentRepository,FileRepository,SubmissionHelper和PluginService类,因此您必须模拟所有bean,并对被测试bean使用注释@InjectMocks。 参见示例:
public class AssignmentRestTest extends GenericTest {
@Mock AssignmentRepository assignmentRepo;
@Mock FileRepository fileRepo;
@Mock SubmissionHelper submissionHelper;
@Mock PluginService pluginHolder;
@InjectMocks AssignmentRest assignRest;
public AssignmentRestTest() throws IOException {
super();
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(assignRest).build();
RestAssuredMockMvc.mockMvc = mockMvc;
presetBeans();
}
public void presetBeans() {
//Preset the return values
Mockito.when(pluginHolder.isSupportedLang(anyString())).thenReturn(Boolean.TRUE);
Mockito.when(assignmentRepo.save(any(Assignment.class))).thenReturn(new Assignment("1234", "rada", "C", "Test", "NEW", new Date()));
}
@Test
public void testCreateAssignment() {
given().body("{\"owner\":\"rada\",\"name\": \"CHistory\",\"lang\": \"C\",\"assignmentId\": \"0\"}")
.contentType("application/json")
.when().
post("/rest/assignment/create").
then().
statusCode(HttpServletResponse.SC_OK);
}
@Test
public void testGetAll(){
given().
when().
get("/rest/assignment/all/rada").
then().
statusCode(HttpServletResponse.SC_OK);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.