vendredi 28 juin 2019

Nested autowired field stays null in JUNIT test

I have a factory pattern implemented and wanted to test this. Except the fields in this factory method are not getting autowired. It seems like the @Autowired attribute in the factory class is staying null. I can't use @SpringBootTest annotation because of the blockchain configuration file that gets loaded then

Below is the code of the service factory, the parserfactory gets autowired correctly in the test. The problems is with the autowired fields of the parserfactory

@Service
@Slf4j
public class ParserFactory {

    @Autowired
    OsirisParser osirisParser;

    public Parser getParser(String system) {
        if (system == null) {
            return null;
        }
        if (system.equalsIgnoreCase("Progress")) {
            return ProgressCreateService();
        }
        if (system.equalsIgnoreCase("Osiris")) {
            log.debug("Osiris parsen creëren");
            return OsirisCreateService();
        }
        return null;

    }

    public OsirisParser OsirisCreateService() {
        return osirisParser;
    }

    public OsirisParser ProgressCreateService() {
        return new OsirisParser("ProgressParser");
    }

The test

@RunWith(SpringRunner.class)
public class FactoryTest {

    @Mock
    ParserFactory serviceCallFactory;

    @Test
    public void testCreateOsirisServiceSuccesFull() {
        Parser serv = serviceCallFactory.getParser("Osiris");
        assertThat(serv, instanceOf(OsirisParser.class));
    }

    @Test
    public void testCreateProgressServiceSuccesFull()  {
        Parser serv = serviceCallFactory.getParser("Progress");
        assertThat(serv, instanceOf(ProgressParser.class));
    }

    @Test
    public void testCreateProgressServiceUnSuccessFull() {
        Parser serv = serviceCallFactory.getParser("Progrddess");
        assertThat(serv, is(not(instanceOf(OsirisParser.class))));
    }

    @Test
    public void testCreateWhenStringIsNotCorrect() {
        Parser serv = serviceCallFactory.getParser("0$iri$");
        assertThat(serv, is(nullValue()));
    }

    @Test
    public void testCreateWhenStringIsNull() {
        Parser serv = serviceCallFactory.getParser("");
        assertThat(serv,  is(nullValue()));
    }
}

Aucun commentaire:

Enregistrer un commentaire