Mocking EJB injection in tests

后端 未结 4 1257
盖世英雄少女心
盖世英雄少女心 2021-01-13 03:25

Whenever I want to test a class which uses resource injection I end up including a constructor that will only be used within the test:

public class A {

             


        
4条回答
  •  有刺的猬
    2021-01-13 04:13

    you could use easy gloss to that effect, it mocks the EJBs injection system.

    another way is to set the field using reflexion in your tests, I sometime use something like this :

    public static void setPrivateField(Class instanceFieldClass, Object instance, String fieldName, Object fieldValue) throws Exception {
        Field setId = instanceFieldClass.getDeclaredField(fieldName);
        setId.setAccessible(true);
        setId.set(instance, fieldValue);
    }
    

提交回复
热议问题