错误模拟 @Inject 类 JUnit Mockito

huangapple go评论122阅读模式
英文:

Error mocking @Inject classes JUnit Mockito

问题

以下是您要翻译的内容:

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

JWTValidator.java

@Inject
JWTUtils jwtUtils;

public void validateJWT(String jwt) { // 主要函数返回true、false或异常
[...]
Claims claims = jwtUtils.getClaims(jwt); // 这里claims = null!
[...]
}

JWTUtils.java

public void getClaims(jwt) { // 这应该返回一个带有我在JWTTestServlet.java中使用的JWT的异常
[...]
}

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 = ""; // 一些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要么为空,要么该函数返回空。

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

您能帮助我吗?

非常感谢
最好的问候

英文:

I'm having some troubles using JUnit with Mockito. First of all I give you all the info that I have.

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));
}

The point is when I call the getClaims function of JWTUtils, it doesn't work, I receive a null field.
I've tried with many different annotations but it doesn't work either. JWTUtils is null or the function returns a null.

I can debug JWTValidator but I can't debug JWTUtils.

Can you please help me?

Thank you so much
Best regards

答案1

得分: 1

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

when(jwtUtils.getJWTClaims(jwt)).thenReturn(new DefaultClaims());

现在当我调用这个函数时,我就不会得到一个空对象了。

非常感谢!

英文:

Solved!
The solution was that I have to define the behaviour of the mocked class (JWTUtils)

when(jwtUtils.getJWTClaims(jwt)).thenReturn(new DefaultClaims());

Now when I call this function I don't have a null object.

Thank you very much!

答案2

得分: -2

默认情况下,模拟对象的所有方法都返回“未初始化”或“空”值,例如,对于数值类型(包括原始类型和包装类型),返回零;对于布尔值,返回false;对于大多数其他类型,返回null。

我认为你需要的不是一个Mock对象,而是一个Spy对象:https://stackoverflow.com/questions/15976008/using-mockito-to-stub-and-execute-methods-for-testing

英文:

By default, all methods of a mock return “uninitialized” or “empty” values, e.g., zeros for numeric types (both primitive and boxed), false for booleans, and nulls for most other types.

I think what you need is not a Mock but a Spy: https://stackoverflow.com/questions/15976008/using-mockito-to-stub-and-execute-methods-for-testing

huangapple
  • 本文由 发表于 2020年10月6日 23:41:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64229230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定