0

I Am using spring boot 2.0.1 , but when I perform the test, it seems to start, before the test class, also the main class. In my main class I use spring cloud config, discovery service and kafka. I have this test class:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:bootstrap-test.properties")
@ActiveProfiles("test")
public class DaemonLogServiceTest {

@Autowired
private LogService logService;

@Before
public void setUp() {
    ResultLog log = new ResultLog();
    log.setNumberOfRowsProcessed(10);
    log.setLastCodOperProcessed(1000);
    log.setStatus(Status.SUCCESS.name());
}

@Test
public void whenOperationsProcessedThenLog() {
    logService.newActivity(10, 1000L, Status.SUCCESS);
    ResultLog log = logService.saveActivity();
    Assert.assertNotNull(log);
}

}

When I run it, it seems to start the Main method (not from test) that uses Kafka and Spring cloud discovery. Here is the output:

018-06-19 14:45:01.397 ERROR 17124 --- [           main] o.s.boot.SpringApplication               : Application run failed
java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at []
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.kafka.bootstrap-servers' in value "${spring.kafka.bootstrap-servers}"

I only use Kafka in the main application, not in test classes. My property file is:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.schema=classpath:schema.sql
daemon.delay=2000
spring.cloud.discovery.enabled = false
spring.cloud.config.discovery.enabled = false
spring.cloud.config.enabled = false
eureka.client.enabled=false
spring.profiles.active=test

1 Answer 1

4

I think your issue lies in @SpringBootTest which loads your main application context with your main ConfigurationClass. So then even you are not using Kafka, Spring tries to initialize your kafka beans which are missing: spring.kafka.bootstrap-servers property.

You can run your tests by specifying a separate config class without kafka beans and other unnecessary beans using @ContextConfiguration

@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:bootstrap-test.properties")
@ContextConfiguration(classes = LogServiceTestConfig.class)
public class DaemonLogServiceTest {

@Autowired
private LogService logService;

@Before
public void setUp() {
    ResultLog log = new ResultLog();
    log.setNumberOfRowsProcessed(10);
    log.setLastCodOperProcessed(1000);
    log.setStatus(Status.SUCCESS.name());
}

@Test
public void whenOperationsProcessedThenLog() {
    logService.newActivity(10, 1000L, Status.SUCCESS);
    ResultLog log = logService.saveActivity();
    Assert.assertNotNull(log);
}

}
Sign up to request clarification or add additional context in comments.

2 Comments

maybe it would be helpful, if you would post also your configuration file, where you specify Profile "test"
I created a new test config class and now it is working, without bringing up the whole kafka context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.