Skip to content

Commit

Permalink
Mongo container setup for integration tests (#1)
Browse files Browse the repository at this point in the history
* change mongo container setup for integration tests

* move container setup to a config class

* restore TestFlashcardsApplication.java
  • Loading branch information
alfabravo2013 authored Jan 6, 2024
1 parent 4da8f04 commit 0ccce37
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package org.hyperskill.community.flashcards;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class TestFlashcardsApplication {

@Bean
@ServiceConnection
MongoDBContainer mongoDbContainer() {
return new MongoDBContainer(DockerImageName.parse("mongo:latest"));
}

public static void main(String[] args) {
SpringApplication.from(FlashcardsApplication::main).with(TestFlashcardsApplication.class).run(args);
SpringApplication
.from(FlashcardsApplication::main)
.with(TestFlashcardsApplication.class)
.run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.hyperskill.community.flashcards;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.MongoDBContainer;

import java.util.List;

@TestConfiguration
public class TestMongoConfiguration {

@Bean
@ServiceConnection
public MongoDBContainer mongoDBContainer() {
var container = new MongoDBContainer("mongo:latest");
container.setPortBindings(List.of("27017:27017"));
return container;
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,28 @@
package org.hyperskill.community.flashcards.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.hyperskill.community.flashcards.TestMongoConfiguration;
import org.hyperskill.community.flashcards.registration.UserDto;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.MediaType;
import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@Testcontainers
@SpringBootTest(classes = TestMongoConfiguration.class)
@AutoConfigureMockMvc
@Disabled // must fix test container connections...
@DisabledInAotMode // bug in Spring 3.2 @MockBean does not work in AOT mode (https://github.com/spring-projects/spring-boot/issues/36997)
class RegisterServerSecurityIT {

@Autowired
MockMvc mockMvc;

@Container
@ServiceConnection
static MongoDBContainer mongoDbContainer = new MongoDBContainer(DockerImageName.parse("mongo:latest"));

@Autowired
ObjectMapper objectMapper;

@BeforeAll
static void setup() {
mongoDbContainer.start();
}

@AfterAll
static void tearDown() {
mongoDbContainer.stop();
}

@Test
void registerUnauthenticatedValidJson_AddsUser() throws Exception {
mockMvc.perform(post("/api/register")
Expand Down

0 comments on commit 0ccce37

Please sign in to comment.