mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Polish "Add @DataNeo4jTest" contribution
Closes gh-8618
This commit is contained in:
parent
b6feb47574
commit
b08016f15a
@ -5710,6 +5710,59 @@ A list of the auto-configuration that is enabled by `@DataMongoTest` can be
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-neo4j-test]]
|
||||||
|
==== Auto-configured Data Neo4j tests
|
||||||
|
`@DataNeo4jTest` can be used if you want to test Neo4j applications. By default, it will
|
||||||
|
use an in-memory embedded Neo4j (if the embedded driver is available), scan for
|
||||||
|
`@NodeEntity` classes and configure Spring Data Neo4j repositories. Regular `@Component`
|
||||||
|
beans will not be loaded into the `ApplicationContext`:
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@DataNeo4jTest
|
||||||
|
public class ExampleDataNeo4jTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private YourRepository repository;
|
||||||
|
|
||||||
|
//
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
Data Neo4j tests are transactional and rollback at the end of each test by default,
|
||||||
|
see the {spring-reference}#testcontext-tx-enabling-transactions[relevant section] in the
|
||||||
|
Spring Reference Documentation for more details. If that's not what you want, you can
|
||||||
|
disable transaction management for a test or for the whole class as follows:
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@DataNeo4jTest
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
|
public class ExampleNonTransactionalTests {
|
||||||
|
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
|
||||||
|
A list of the auto-configuration that is enabled by `@DataNeo4jTest` can be
|
||||||
|
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]]
|
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]]
|
||||||
==== Auto-configured REST clients
|
==== Auto-configured REST clients
|
||||||
The `@RestClientTest` annotation can be used if you want to test REST clients. By default
|
The `@RestClientTest` annotation can be used if you want to test REST clients. By default
|
||||||
|
@ -217,11 +217,5 @@
|
|||||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot</artifactId>
|
|
||||||
<type>test-jar</type>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -43,9 +43,11 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
* configuration relevant to Neo4j tests.
|
* configuration relevant to Neo4j tests.
|
||||||
* <p>
|
* <p>
|
||||||
* By default, tests annotated with {@code @DataNeo4jTest} will use an embedded in-memory
|
* By default, tests annotated with {@code @DataNeo4jTest} will use an embedded in-memory
|
||||||
* Neo4j process (if available).
|
* Neo4j process (if available). They will also be transactional with the usual
|
||||||
|
* test-related semantics (i.e. rollback by default).
|
||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
|
@ -28,7 +28,6 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
|
|||||||
* {@link TypeExcludeFilter} for {@link DataNeo4jTest @DataNeo4jTest}.
|
* {@link TypeExcludeFilter} for {@link DataNeo4jTest @DataNeo4jTest}.
|
||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
* @since 2.0.0
|
|
||||||
*/
|
*/
|
||||||
class DataNeo4jTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
|
class DataNeo4jTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
|
||||||
|
|
||||||
|
@ -24,16 +24,16 @@ import org.neo4j.ogm.session.Session;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.neo4j.Neo4jTestServer;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sample test for {@link DataNeo4jTest @DataNeo4jTest}
|
* Integration test for {@link DataNeo4jTest}.
|
||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DataNeo4jTest
|
@DataNeo4jTest
|
||||||
@ -41,7 +41,7 @@ public class DataNeo4jTestIntegrationTests {
|
|||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public Neo4jTestServer server = new Neo4jTestServer(
|
public Neo4jTestServer server = new Neo4jTestServer(
|
||||||
new String[]{"org.springframework.boot.test.autoconfigure.data.neo4j"});
|
new String[] { "org.springframework.boot.test.autoconfigure.data.neo4j" });
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
@ -59,8 +59,9 @@ public class DataNeo4jTestIntegrationTests {
|
|||||||
public void testRepository() {
|
public void testRepository() {
|
||||||
ExampleGraph exampleGraph = new ExampleGraph();
|
ExampleGraph exampleGraph = new ExampleGraph();
|
||||||
exampleGraph.setDescription("Look, new @DataNeo4jTest!");
|
exampleGraph.setDescription("Look, new @DataNeo4jTest!");
|
||||||
exampleGraph = this.exampleRepository.save(exampleGraph);
|
assertThat(exampleGraph.getId()).isNull();
|
||||||
assertThat(exampleGraph.getId()).isNotNull();
|
ExampleGraph savedGraph = this.exampleRepository.save(exampleGraph);
|
||||||
|
assertThat(savedGraph.getId()).isNotNull();
|
||||||
assertThat(this.session.countEntitiesOfType(ExampleGraph.class)).isEqualTo(1);
|
assertThat(this.session.countEntitiesOfType(ExampleGraph.class)).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.neo4j.Neo4jTestServer;
|
|
||||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
@ -49,4 +49,5 @@ public class ExampleGraph {
|
|||||||
public void setDescription(String description) {
|
public void setDescription(String description) {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,12 +16,12 @@
|
|||||||
|
|
||||||
package org.springframework.boot.test.autoconfigure.data.neo4j;
|
package org.springframework.boot.test.autoconfigure.data.neo4j;
|
||||||
|
|
||||||
import org.springframework.data.neo4j.repository.GraphRepository;
|
import org.springframework.data.neo4j.repository.Neo4jRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example repository used with {@link DataNeo4jTest} tests.
|
* Example repository used with {@link DataNeo4jTest} tests.
|
||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
*/
|
*/
|
||||||
public interface ExampleRepository extends GraphRepository<ExampleGraph> {
|
public interface ExampleRepository extends Neo4jRepository<ExampleGraph, Long> {
|
||||||
}
|
}
|
||||||
|
@ -37,4 +37,5 @@ public class ExampleService {
|
|||||||
public boolean hasNode(Class<?> clazz) {
|
public boolean hasNode(Class<?> clazz) {
|
||||||
return this.session.countEntitiesOfType(clazz) == 1;
|
return this.session.countEntitiesOfType(clazz) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.neo4j;
|
package org.springframework.boot.test.autoconfigure.data.neo4j;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
@ -23,13 +23,14 @@ import org.junit.rules.TestRule;
|
|||||||
import org.junit.runner.Description;
|
import org.junit.runner.Description;
|
||||||
import org.junit.runners.model.Statement;
|
import org.junit.runners.model.Statement;
|
||||||
import org.neo4j.ogm.config.Configuration;
|
import org.neo4j.ogm.config.Configuration;
|
||||||
import org.neo4j.ogm.config.DriverConfiguration;
|
|
||||||
import org.neo4j.ogm.session.SessionFactory;
|
import org.neo4j.ogm.session.SessionFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link TestRule} for working with an optional Neo4j server.
|
* {@link TestRule} for working with an optional Neo4j server running on localhost. Make
|
||||||
|
* sure to disable authentication if you haven't done so already.
|
||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class Neo4jTestServer implements TestRule {
|
public class Neo4jTestServer implements TestRule {
|
||||||
|
|
||||||
@ -55,12 +56,13 @@ public class Neo4jTestServer implements TestRule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SessionFactory createSessionFactory() {
|
public SessionFactory getSessionFactory() {
|
||||||
Configuration configuration = new Configuration();
|
return this.sessionFactory;
|
||||||
DriverConfiguration driverConfiguration = configuration.driverConfiguration();
|
}
|
||||||
driverConfiguration.setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver");
|
|
||||||
driverConfiguration.setURI("http://localhost:7474");
|
|
||||||
|
|
||||||
|
private SessionFactory createSessionFactory() {
|
||||||
|
Configuration configuration = new Configuration.Builder()
|
||||||
|
.uri("bolt://localhost:7687").build();
|
||||||
SessionFactory sessionFactory = new SessionFactory(configuration, this.packages);
|
SessionFactory sessionFactory = new SessionFactory(configuration, this.packages);
|
||||||
testConnection(sessionFactory);
|
testConnection(sessionFactory);
|
||||||
return sessionFactory;
|
return sessionFactory;
|
@ -1,20 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2017 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Neo4j support classes
|
|
||||||
*/
|
|
||||||
package org.springframework.boot.neo4j;
|
|
Loading…
Reference in New Issue
Block a user