0

I'm using Embedded Mongodb in my tests and all MongoRepository tests working fine. But services tests working fine only if I run their one by one or all in the test class using IDE and don't work during the build.

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    private User user = new User(.....);

    @Test
    public void findByEmail() {
        // Arrange
        String email = user.getEmail();
        User savedUser = userService.save(user);

        // Act
        Optional<User> optionalUser = userService.findByEmail(email);

        // Assert
        assertThat(optionalUser.isPresent()).isTrue();
        assertThat(optionalUser.get().getEmail()).isEqualTo(email);
        assertThat(optionalUser.get()).isEqualTo(savedUser);
    } 
.....

dependency:

    testCompile 'de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.2.0'

Here you can see my error logs:

Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    .....
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Invocation of init method failed; nested exception is java.io.IOException: Could not start process: <EOF>
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1771)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    .......
Caused by: java.io.IOException: Could not start process: <EOF>
    at de.flapdoodle.embed.mongo.AbstractMongoProcess.onAfterProcessStart(AbstractMongoProcess.java:79)
    at de.flapdoodle.embed.process.runtime.AbstractProcess.<init>(AbstractProcess.java:116)
    at de.flapdoodle.embed.mongo.AbstractMongoProcess.<init>(AbstractMongoProcess.java:53)
    at de.flapdoodle.embed.mongo.MongodProcess.<init>(MongodProcess.java:50)
    at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:44)
    at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:34)
    at de.flapdoodle.embed.process.runtime.Executable.start(Executable.java:108)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1897)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1839)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1767)
    ... 65 more

Vitaliy Denys
  • 43
  • 1
  • 9
  • possible duplicate: https://stackoverflow.com/questions/49990340/why-does-my-flapdoodle-embedded-mongodb-test-fail-to-run-creating-embeddedmon – Toerktumlare May 05 '20 at 00:13
  • Resolved!! I've added separate test property file with mongo port: 0 and @ActiveProfiles("test") ` spring: data: mongodb: database: embedded host: localhost port: 0 ` – Vitaliy Denys May 05 '20 at 09:51

1 Answers1

0

Try using

System.setProperty("os.arch", "x86_64");

As show in below code snippet.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class EmbeddedMongoApplication {

    public static void main(String[] args) {
         System.setProperty("os.arch", "x86_64");
         SpringApplication.run(EmbeddedMongoApplication.class, args);
    }
    
    @Bean
    public EmbeddedMongoAutoConfiguration embeddedMongoAutoConfiguration(MongoProperties mongoProperties) {
        return new EmbeddedMongoAutoConfiguration(mongoProperties);
    }
}
Punit Tiwari
  • 496
  • 4
  • 8