Junit testing using ConfigProperties injected object is null

Viewed 1128

I have a Quarkus application, and I want to test one part of it. That part is using kafka streams, and I do not want to connect to the network during testing, so I am not using the @QuarkusTest flag.

Instead I would use MockProcessorContext and the KafkaStreams junit testing approach.

As I am using a Transformer object, I need to use the same topic name throughout, so I have put the topics in an @ConfigProperties class (simplified example):

@ConfigProperties(prefix = "test")
public class AppConfiguration {

    public TopicsConfig topics;
    public StoresConfig stores;

    public static class TopicsConfig {
        public String first = "first-topic";
        public String second = "second-topic";
    }
    
    public static class StoresConfig {
       public String first = "first-store";
    }
}

Then in my junit test class I @Inject this as I would normally in my application

@Inject
AppConfiguration config

The problem I have is that when I come to use it in my setup function, it is always null.

@BeforeEach
public void setUp() {
   config == null
}

I do not know how to get around this - I don't want to use @QuarkusTest as I do not need to run the application and connect to all my servers.

Help and advice please!

1 Answers

If you want to use injection inside your test, you need to use @QuarkusTest as your need something to intiantiate your beans and make the injection happens. And this is the role of the bean factory of Quarkus (Arc).

So there is no other way than making your test a Quarkus test via @QuarkusTest.

Another solution would be to use Mockito and mock the object instead of injecting it inside your test.

Related