所以这是我的代码:
@Service("MyCode")
public class CodeImpl implements CodeI {
@Autowired
private CodeMapper codeMapper;
@Autowired
private CodeAppService codeAppService;
@Override
public CodePOJO getCode(String myId) {
CodeDTO codeDTO = codeAppService.getOne(myId);
return codeMapper.mapCode(codeDTO);
}
}
这是我写的单元测试:
public class CodeImplTest {
@Tested(fullyInitialized = true)
CodeImpl codeImpl;
@Injectable
CodeAppService mockedCodeAppService;
@Injectable
CodeMapper mockedCodeMapper;
@BeforeMethod
public void setup_mocks() {
codeImpl = new CodeImpl();
}
@Test
public void testGetCode() throws Exception {
final CodeDTO codeDTO = new codeDTO();
codeDTO.setName("my name")
new NonStrictExpectations() {{
mockedCodeAppService.getOne(anyString);
result = codeDTO;
}};
CodePOJO returnedCodePOJO = codeImpl.getCode("1");
assertThat(returnedCodePOJO, is(instanceOf(CodePOJO.class)));
assertThat(returnedCodePOJO.getName(), is("my name"));
}
}
我收到了以下未初始化的例外:
java.lang.AssertionError:
Expected: is "my name"
but: was null
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at com.dev.impl.CodeImplTest.testGetCode(CodeImplTest.java:74)
我试过在论坛上搜索但是没有任何答案可以工作。
任何帮助将不胜感激。
感谢。
答案 0 :(得分:1)
您只为codeAppService.getOne()
准备了行为,但在您的代码中,该调用的结果会传递给codeMapper.mapCode()
。
我会尝试:
new NonStrictExpectations() {{
mockedCodeAppService.getOne(anyString);
result = codeDTO;
mockedCodeMapper.mapCode(codeDTO);
result = codeDTO;
}};