Skip to content

Commit

Permalink
Improve dependency management, move to JUnit 5; Upgrade Mockito
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlockwood committed Jan 2, 2024
1 parent b62bd02 commit 178be77
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 179 deletions.
41 changes: 34 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,40 @@
<revision>DEV</revision>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-bom</artifactId>
<version>4.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
Expand All @@ -85,15 +109,18 @@

<!-- TEST -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.0</version>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
212 changes: 104 additions & 108 deletions src/test/java/com/github/arangobee/AbstractArangobeeTest.java
Original file line number Diff line number Diff line change
@@ -1,108 +1,104 @@
package com.github.arangobee;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.core.env.Environment;

import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.DocumentCreateEntity;
import com.github.arangobee.changeset.ChangeEntry;
import com.github.arangobee.dao.ChangeEntryDao;
import com.github.arangobee.dao.ChangeEntryIndexDao;
import com.github.arangobee.exception.ArangobeeConnectionException;
import com.github.arangobee.exception.ArangobeeLockException;

public class AbstractArangobeeTest {
protected static final String FAKE_LOCK="fakeLock";

private static final String CHANGELOG_COLLECTION_NAME="dbchangelog";

protected Arangobee runner;

@Mock
protected ChangeEntryDao dao;

@Mock
private ChangeEntryIndexDao indexDao;

@Mock
private ArangoDatabase fakeArangoDatabase;

@Mock
private AutowireCapableBeanFactory autowireCapableBeanFactory;

private final List<BaseDocument> list=new ArrayList<>();

private Environment environmentMock;

public void before() throws ArangobeeConnectionException, ArangobeeLockException {
runner=new Arangobee(fakeArangoDatabase, autowireCapableBeanFactory).dao(dao);
when(dao.connectDb(any(ArangoDatabase.class))).thenReturn(fakeArangoDatabase);
when(dao.getArangoDatabase()).thenReturn(fakeArangoDatabase);
ArangoCollection mockColletion=mock(ArangoCollection.class);
list.clear();
when(mockColletion.insertDocument(any())).then(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
list.add((BaseDocument) invocation.getArguments()[0]);
return mock(DocumentCreateEntity.class);
}
});
when(fakeArangoDatabase.collection(anyString())).thenReturn(mockColletion);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object o=invocation.getArguments()[0];
for(Field field: o.getClass().getDeclaredFields()) {
if(Environment.class.isAssignableFrom(field.getType()) && field.getAnnotation(Autowired.class)!=null) {
field.setAccessible(true);
field.set(o, environmentMock);
}
}
return null;
}
}).when(autowireCapableBeanFactory).autowireBeanProperties(any(), anyInt(), anyBoolean());
doCallRealMethod().when(dao).save(any(ChangeEntry.class));
doCallRealMethod().when(dao).setChangelogCollectionName(anyString());
doCallRealMethod().when(dao).setIndexDao(any(ChangeEntryIndexDao.class));
dao.setIndexDao(indexDao);
dao.setChangelogCollectionName(CHANGELOG_COLLECTION_NAME);

// runner.setEnabled(true);
}

protected void setSpringEnvironment(Environment environmentMock) {
this.environmentMock=environmentMock;
runner.setSpringEnvironment(environmentMock);
// autowireCapableBeanFactory.autowireBean(environmentMock);
}

protected long count(String changeId, String author) {
long count=0;
for(BaseDocument doc: list) {
if((changeId==null || changeId.equals(doc.getAttribute(ChangeEntry.KEY_CHANGEID)))
&& (author==null || author.equals(doc.getAttribute(ChangeEntry.KEY_AUTHOR)))) {
count++;
}
}
return count;
}
}
package com.github.arangobee;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.core.env.Environment;

import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.DocumentCreateEntity;
import com.github.arangobee.changeset.ChangeEntry;
import com.github.arangobee.dao.ChangeEntryDao;
import com.github.arangobee.dao.ChangeEntryIndexDao;
import com.github.arangobee.exception.ArangobeeConnectionException;
import com.github.arangobee.exception.ArangobeeLockException;


public class AbstractArangobeeTest {
protected static final String FAKE_LOCK="fakeLock";

private static final String CHANGELOG_COLLECTION_NAME="dbchangelog";

protected Arangobee runner;

@Mock
protected ChangeEntryDao dao;

@Mock
private ChangeEntryIndexDao indexDao;

@Mock
private ArangoDatabase fakeArangoDatabase;

@Mock
private AutowireCapableBeanFactory autowireCapableBeanFactory;

private final List<BaseDocument> list=new ArrayList<>();

private Environment environmentMock;

public void before() throws ArangobeeConnectionException, ArangobeeLockException {
runner=new Arangobee(fakeArangoDatabase, autowireCapableBeanFactory).dao(dao);
lenient().when(dao.connectDb(any())).thenReturn(fakeArangoDatabase);
lenient().when(dao.getArangoDatabase()).thenReturn(fakeArangoDatabase);
ArangoCollection mockCollection=mock(ArangoCollection.class);
list.clear();
lenient().when(mockCollection.insertDocument(any())).then((Answer) invocation -> {
list.add((BaseDocument) invocation.getArguments()[0]);
return mock(DocumentCreateEntity.class);
});
lenient().when(fakeArangoDatabase.collection(anyString())).thenReturn(mockCollection);
lenient().doAnswer(invocation -> {
Object o=invocation.getArguments()[0];
for(Field field: o.getClass().getDeclaredFields()) {
if(Environment.class.isAssignableFrom(field.getType()) && field.getAnnotation(Autowired.class)!=null) {
field.setAccessible(true);
field.set(o, environmentMock);
}
}
return null;
}).when(autowireCapableBeanFactory).autowireBeanProperties(any(), anyInt(), anyBoolean());
lenient().doCallRealMethod().when(dao).save(any(ChangeEntry.class));
doCallRealMethod().when(dao).setChangelogCollectionName(anyString());
doCallRealMethod().when(dao).setIndexDao(any(ChangeEntryIndexDao.class));
dao.setIndexDao(indexDao);
dao.setChangelogCollectionName(CHANGELOG_COLLECTION_NAME);

// runner.setEnabled(true);
}

protected void setSpringEnvironment(Environment environmentMock) {
this.environmentMock=environmentMock;
runner.setSpringEnvironment(environmentMock);
// autowireCapableBeanFactory.autowireBean(environmentMock);
}

protected long count(String changeId, String author) {
long count=0;
for(BaseDocument doc: list) {
if((changeId==null || changeId.equals(doc.getAttribute(ChangeEntry.KEY_CHANGEID)))
&& (author==null || author.equals(doc.getAttribute(ChangeEntry.KEY_AUTHOR)))) {
count++;
}
}
return count;
}
}
22 changes: 11 additions & 11 deletions src/test/java/com/github/arangobee/ArangobeeEnvTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.github.arangobee;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import com.github.arangobee.changeset.ChangeEntry;
import com.github.arangobee.exception.ArangobeeConnectionException;
import com.github.arangobee.exception.ArangobeeLockException;
import com.github.arangobee.resources.EnvironmentMock;
import com.github.arangobee.test.env.EnvironmentDependentTestResource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;


/**
* Created by lstolowski on 13.07.2017.
*/
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class ArangobeeEnvTest extends AbstractArangobeeTest {

@Before
@BeforeEach
@Override
public void before() throws ArangobeeConnectionException, ArangobeeLockException {
super.before();
Expand Down
23 changes: 11 additions & 12 deletions src/test/java/com/github/arangobee/ArangobeeProfileTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package com.github.arangobee;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import com.github.arangobee.changeset.ChangeEntry;
import com.github.arangobee.exception.ArangobeeConnectionException;
import com.github.arangobee.exception.ArangobeeLockException;
import com.github.arangobee.resources.EnvironmentMock;
import com.github.arangobee.test.changelogs.AnotherArangobeeTestResource;
import com.github.arangobee.test.profiles.def.UnProfiledChangeLog;
import com.github.arangobee.test.profiles.dev.ProfiledDevChangeLog;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;


/**
* Tests for Spring profiles integration
*
* @author lstolowski
* @since 2014-09-17
*/
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class ArangobeeProfileTest extends AbstractArangobeeTest {
public static final int CHANGELOG_COUNT=6;

@Before
@BeforeEach
@Override
public void before() throws ArangobeeConnectionException, ArangobeeLockException {
super.before();
Expand Down Expand Up @@ -79,7 +79,6 @@ public void shouldNotRunAnyChangeSet() throws Exception {
// given
setSpringEnvironment(new EnvironmentMock("foobar"));
runner.setChangeLogsScanPackage(ProfiledDevChangeLog.class.getPackage().getName());
when(dao.isNewChange(any(ChangeEntry.class))).thenReturn(true);

// when
runner.execute();
Expand Down
Loading

0 comments on commit 178be77

Please sign in to comment.