lundi 9 août 2021

Spring Boot JUnit test beanFactory.getBean null pointer exception

I've a functioni'm trying to test via JUnit but I am not able to find how to overcome this issue:

in the service i have this declaration:

Warning_Log Warning_Log_FRAGMENT = beanFactory.getBean(Warning_Log.class);

I tried declaring the beanFactory object in the test class with the @MockBean annotation but i get a null pointer exception.

The cherry on top of the cake is that the function i'm testing is private so i'm using reflection to access it.

Do you know how the beanFactory(Warning_Log.class) can be implemented in the junit test function?

EDIT

the code in the service is the following:

try {
        JSONObject jsonFragmentRequestWarning_Log = new JSONObject();
        jsonFragmentRequestWarning_Log.put("messaggio", "Create Session Blocked [Assertion="+policy.getString("name")+"]|[flag_block_create_session="+gateway.flag_block_create_session+"]|[ApplicationName="+Request.getHeader("ApplicationName")+"]|[ErroreDaRitornare="+erroreDaRitornare+"]");
        jsonFragmentRequestWarning_Log.put("sanitize", false);

        Warning_Log Warning_Log_FRAGMENT = beanFactory.getBean(Warning_Log.class);
        
        String sFragmentResponseWarning_Log = Warning_Log_FRAGMENT.warning_Log(jsonFragmentRequestWarning_Log.toString(), httpHeaders);

        JSONObject jsonFragmentResponseWarning_Log = new JSONObject(sFragmentResponseWarning_Log);

    }

the beanFactory is autowired in the service like so:

@Autowired
private BeanFactory beanFactory;




Aucun commentaire:

Enregistrer un commentaire