[SPRING] Junit / Fongo : NotNull 확인을위한 단위 테스트에서 Fongo를 사용하는 방법
SPRINGJunit / Fongo : NotNull 확인을위한 단위 테스트에서 Fongo를 사용하는 방법
반환 된 쿼리 (형식 DataVersion)가 null이 아닌지 확인하는 기본 단위 테스트를 작성했습니다. 내 DB를 테스트하기 위해 Fongo를 사용하고 있는지 확인해야합니다.
이것은 repo 클래스입니다.
@Repository
public class DataVersionDaoMongo extends MongoBaseDao<DataVersion> implements DataVersionDao {
@Autowired
MongoOperations mongoOperations;
public DataVersionDaoMongo() {
initType();
}
@Override
public DataVersion findByDBAndCollection(String dbName, String collectionName) {
Criteria criteria = Criteria.where("dbName").is(dbName).and("collectionName").is(collectionName);
Query query = Query.query(criteria);
return mongoOperations.findOne(query, DataVersion.class);
}
}
이것이 나의 단위 테스트입니다.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/testApplicationContext.xml")
public class DataVersionDaoMongoTest {
@Autowired
private DataVersionDaoMongo dataVersionDaoMongo;
@Autowired
private MongoOperations mongoOperations;
private DataVersion dataVersion;
@Rule
public FongoRule fongoRule = new FongoRule();
@Test
public void findByDBAndCollection() {
//String dbname = "mydb";
//String collectionName = "mycollection";
// DB db = fongoRule.getDB(dbname);
//DBCollection collection = db.getCollection(collectionName);
//Mongo mongo = fongoRule.getMongo();
//collection.insert(new BasicDBObject("name", "randomName"));
DataVersion dataVersion = new DataVersion();
dataVersion.setDbName("DBDataVersion");
dataVersion.setVersion("version1");
dataVersion.setCollectionName("DataVersion");
mongoOperations.insert(dataVersion);**strong text**
assertThat(dataVersionDaoMongo.findByDBAndCollection(dbname, collectionName)).isNotNull();
}
}
다음은 DataVersion 클래스입니다.
@Document(collection = "DataVersion")
public class DataVersion {
@Id
private String id;
private String dbName;
private String collectionName;
private String version;
private boolean isCompleted;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public boolean isCompleted() {
return isCompleted;
}
public void setCompleted(boolean isCompleted) {
this.isCompleted = isCompleted;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((collectionName == null) ? 0 : collectionName.hashCode());
result = prime * result + ((dbName == null) ? 0 : dbName.hashCode());
result = prime * result + (isCompleted ? 1231 : 1237);
result = prime * result + ((version == null) ? 0 : version.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DataVersion other = (DataVersion) obj;
if (collectionName == null) {
if (other.collectionName != null)
return false;
} else if (!collectionName.equals(other.collectionName))
return false;
if (dbName == null) {
if (other.dbName != null)
return false;
} else if (!dbName.equals(other.dbName))
return false;
if (isCompleted != other.isCompleted)
return false;
if (version == null) {
if (other.version != null)
return false;
} else if (!version.equals(other.version))
return false;
return true;
}
}
이것은 응용 프로그램 컨텍스트 파일을 테스트하는 것입니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<bean name="fongo" class="com.github.fakemongo.Fongo">
<constructor-arg value="InMemoryMongo" />
</bean>
<bean id="mongo" factory-bean="fongo" factory-method="getMongo" />
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" />
<!-- localhost settings for mongo -->
<!--<mongo:db-factory id="mongoDbFactory" />-->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory"/>
</bean>
</beans>
dataVersion 변수를 선언하고 DbName, Version, CollectionName 같은 값을 내 테스트 내에 설정하고 findByDBandCollection 메서드를 호출하기 때문에 내 단위 테스트가 통과 되긴하지만. 하지만 여기서 Fongo DB를 사용하지 않고 있으며이를 활용하고 싶습니다. 어떻게하면 Fongo db를 테스트하고 assertThat을 사용하여 findByDBAndCollection 메서드를 호출하여 반환 된 DataVersion이 null이 아닌지 확인할 수 있습니까?
해결법
-
==============================
1.나는 이것을 보여주는 예를 만들었습니다.
나는 이것을 보여주는 예를 만들었습니다.
from https://stackoverflow.com/questions/53166437/junit-fongo-how-to-make-use-of-fongo-in-the-unit-test-for-checking-notnull by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] Java - SpringMVC - 컨트롤러에서 매개 변수 가져 오기 (0) | 2019.05.04 |
---|---|
[SPRING] Spring Rest 웹 서비스에서 돌아 오는 json 응답을 필터링하는 방법 (0) | 2019.05.04 |
[SPRING] 기본 MVC와 로케일 변경이 작동하지 않는 Spring MVC (0) | 2019.05.04 |
[SPRING] Spring RESTful 클라이언트 : 루트 태그 예외 (0) | 2019.05.04 |
[SPRING] JavaFX : Spring 프레임 워크를 JavaFX 앱과 통합 (잘못된 구성) (0) | 2019.05.04 |