我和Mockito进行了Java测试:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser {
UserName = model.Email,
Email = model.Email
};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
UserManager.AddToRole(user.Id, model.rol);
return RedirectToAction("Contact", "Home");
}
AddErrors(result);
}
}
PersistentNode 是Kotlin类:
public class PersistentNodeDeserializerTests {
@Test
public void userInfoPersistentNodeDeserializer() {
PersistentNode node = mock(PersistentNode.class);
when(node.stringChild("username")).thenReturn("cliff12");
//more stuff
}
}
我收到此错误:
kotlin.TypeCastException:null无法强制转换为非null类型 的java.util.HashMap
如何正确模拟属性open class PersistentNode(private val path: PersistentNodePath, val content: Any) {
val stringPath: String
get() = path.get()
val key: String
get() {
val parts = stringPath.split("/");
return parts[parts.size - 1];
}
val mapContent: Map<String, Any>
get() {
return content as HashMap<String, Any>
}
fun stringChild(child: String): String {
return mapContent.get(child) as String
}
}
?
答案 0 :(得分:4)
此库可能会解决您的问题https://github.com/nhaarman/mockito-kotlin
编辑:抱歉,没有意识到您正在使用Java测试。如果是一个选项,请尝试在kotlin中编写测试