Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 模拟@Inject类JUnit Mockito时出错_Java_Junit_Mocking_Mockito - Fatal编程技术网

Java 模拟@Inject类JUnit Mockito时出错

Java 模拟@Inject类JUnit Mockito时出错,java,junit,mocking,mockito,Java,Junit,Mocking,Mockito,我在使用JUnit和Mockito时遇到了一些问题。首先,我给你我所有的信息 JWTValidator.java @Inject JWTUtils jwtUtils; public void validateJWT(String jwt) { //The main function returns true, false or an exception [...] Claims claims = jwtUtils.getClaims(jwt); //claims = null here! [.

我在使用JUnit和Mockito时遇到了一些问题。首先,我给你我所有的信息

JWTValidator.java

@Inject
JWTUtils jwtUtils;

public void validateJWT(String jwt) { //The main function returns true, false or an exception
[...]
Claims claims = jwtUtils.getClaims(jwt); //claims = null here!
[...]
}
JWTUtils.java

public void getClaims(jwt) { //This should return an Exception with the JWT that I use on the JWTTestServlet.java
[...]
}
JWTTestServlet.java

@InjectMocks
private JWTValidator jwtValidator;
        
@Mock
private JWTUtils jwtUtils;

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
}

@Test(expected=Exception.class)
public void testJWTWithoutGUID() {
final String jwt = ""; //Some JWT 
    String message = "";
    try {
        jwtValidator.validateJWT(jwt);
    } catch (Exception e) {
        message = e.getMessage();
        System.out.println(message);
    }
    assertTrue(message.contains(MSG_WITHOUT_GUID));
}
关键是当我调用JWTUtils的getClaims函数时,它不起作用,我收到一个空字段。
我尝试了许多不同的注释,但也不起作用。JWTUtils为null或函数返回null

我可以调试JWTValidator,但无法调试JWTUtils

你能帮帮我吗

多谢各位
最好的注意

默认情况下,模拟的所有方法都返回“未初始化”或“空”值,例如,数字类型(原语和方框)为零,布尔值为false,大多数其他类型为null

我想你需要的不是嘲笑而是间谍:

解决了! 解决方案是我必须定义模拟类(JWTUtils)的行为

现在,当我调用这个函数时,我没有空对象


多谢各位

“JWTUtils为null或函数返回null”-是哪一个?可以发布整个类而不是部分类吗
when(jwtUtils.getJWTClaims(jwt)).thenReturn(new DefaultClaims());