Ich benutze die Postgres-Datenbank und möchte die H2-Datenbank für Tests verwenden. Das Problem ist, wenn ich ein neues Objekt in der Datenbank erstelle, scheint es, dass H2 überhaupt nicht im Test verwendet wird.H2 db für SpringBoot-Tests
Prüfklasse:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@WithMockUser(roles = "ADMIN")
@ActiveProfiles("test")
public class CompanyTests {
@SpyBean
private CompanyService companyServiceMock;
@Autowired
private MockMvc mockMvc;
@Autowired
EntityManager entityManager;
@Test
@Transactional
public void testaaaa() {
entityManager.persist(new Company("nnnn", new Address()));
List<Company> all = companyServiceMock.findAll();
all.forEach(company -> System.out.println(company.getName()));
}
application.properties:
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/EDI
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.platform=postgresql
spring.datasource.initialize=true
spring.datasource.continue-on-error=true
spring.jackson.serialization.fail-on-empty-beans=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
application-test.properties:
spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-
1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
Und wenn ich findAll() in meinem Test, es listet alle Unternehmen von postgresql und neu auf, die von entityManager erstellt werden.
ich es vor versucht haben, und es ist immer noch das gleiche. :(und in application.properties muss ich haben spring.queries.users und spring.queries.roles, aber es ist nicht wichtig in diesem Fall denke ich – Helosze
Haben Sie Profile Abschnitt in Ihrer 'pom.xml' mit entsprechenden Abhängigkeiten für Jedes Profil? –
Es gibt überall "kann nicht aufgelöst werden" im Projekt, wenn ich Abhängigkeiten zu Profilen hinzufüge.: / – Helosze