Mongodb integration testing. Test data is not created - java

I am trying to test my mongodb dao using embedded mongodb- but the test data is not being created.
By contrast, if I use the same way to creating objects in real db then it works OK.
This is my config:
public class UnmappableDataDAOTest
{
private static final String LOCALHOST = "127.0.0.1";
private static final String DB_NAME = "itest";
private static final int MONGO_TEST_PORT = 27028;
private static final String CONTRACT_COLLECTION = "ContractAnswers";
private UnmappableDataDAO dao;
private static MongodProcess mongoProcess;
private static Mongo mongo;
private MongoTemplate template;
#BeforeClass
public static void initializeDB() throws IOException
{
RuntimeConfig config = new RuntimeConfig();
config.setExecutableNaming(new UserTempNaming());
MongodStarter starter = MongodStarter.getInstance(config);
MongodExecutable mongoExecutable = starter.prepare(new MongodConfig(Version.V2_2_0, MONGO_TEST_PORT, false));
mongoProcess = mongoExecutable.start();
mongo = new Mongo(LOCALHOST, MONGO_TEST_PORT);
mongo.getDB(DB_NAME);
}
#AfterClass
public static void shutdownDB() throws InterruptedException {
mongo.close();
mongoProcess.stop();
}
#Before
public void setUp() throws Exception {
dao = new UnmappableDataDAO();
template = new MongoTemplate(mongo, DB_NAME);
dao.setMongoOperations(template);
}
#After
public void tearDown() throws Exception
{
template.dropCollection(ContractAnswers.class);
}
#Test
public void testCreate()
{
// Setup Test Data
List<ContractAnswers> contractAnswersList = new ArrayList<>();
ContractAnswers first = new ContractAnswers();
first.setContractId(1L);
first.setTemplateName("firstTemplate");
first.setId("1");
contractAnswersList.add(first);
ContractAnswers second = new ContractAnswers();
second.setContractId(2L);
second.setTemplateName("secondTemplate");
second.setId("2");
contractAnswersList.add(second);
for (ContractAnswers contractAnswers : contractAnswersList)
{
template.save(contractAnswers, CONTRACT_COLLECTION);
}
ContractAnswers third = new ContractAnswers();
third.setContractId(3L);
third.setTemplateName("thirdTemplate");
third.setId("3");
dao.create(third);
List<ContractAnswers> list = template.findAll(ContractAnswers.class);
}
}
The result of findAll() method is null. That means, that data has not been created.
When I use this way of creation: template.save(contractAnswers, CONTRACT_COLLECTION);, test data is created, but I need the way with CONTRACT_COLLECTION as a parameter. All methods in my dao depend on it.
It is strange, but this is how my create method looks like and it works OK:
public void createOrUpdate(ContractAnswers contractAnswers)
{
this.mongoOperations.save(contractAnswers, CONTRACT_COLLECTION);
}
The same code works ok in real database, but not work in test.
Thank you.

Related

Mock method return type in java

Below is main code consist of one util class and service class using it
#PropertySource("classpath:atlas-application.properties")
public class ApacheAtlasUtils {
#Value("${atlas.rest.address}")
private String atlasURL;
#Value("${atlas.rest.user}")
private String atlasUsername;
#Value("${atlas.rest.password}")
private String atlasPassword;
private AtlasClientV2 client;
public AtlasClientV2 createClient() {
if (client == null) {
return new AtlasClientV2(new String[] {atlasURL}, new String[] {atlasUsername, atlasPassword});
} else {
return client;
}
}
}
Service Class is below :-
#Override
public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
throws AtlasServiceException {
// Some code
client = new ApacheAtlasUtils().createClient();
//some code
}
I am writing unit test for service method and I am getting exception for createClient method asking for values for url, username and password which should not happen as this should be mocked but the mocking is giving me below error
java.lang.IllegalArgumentException: Base URL cannot be null or empty.
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:141)
at org.apache.atlas.AtlasServerEnsemble.<init>(AtlasServerEnsemble.java:35)
at org.apache.atlas.AtlasBaseClient.determineActiveServiceURL(AtlasBaseClient.java:318)
at org.apache.atlas.AtlasBaseClient.initializeState(AtlasBaseClient.java:460)
at org.apache.atlas.AtlasBaseClient.initializeState(AtlasBaseClient.java:448)
at org.apache.atlas.AtlasBaseClient.<init>(AtlasBaseClient.java:132)
at org.apache.atlas.AtlasClientV2.<init>(AtlasClientV2.java:82)
at com.jlr.stratus.commons.utils.ApacheAtlasUtils.createClient(ApacheAtlasUtils.java:40)
at com.jlr.stratus.rest.service.impl.FileSearchService.findFilesWithPages(FileSearchService.java:49)
The Test code is as follows:-
private FileSearchService fileSearchService;
#Spy
private ApacheAtlasUtils apacheAtlasUtils;
#Mock
private AtlasClientV2 client;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
fileSearchService = new FileSearchService();
}
#Test
public void findFilesWithPages_searchAll() throws AtlasServiceException {
Mockito.doReturn(client).when(apacheAtlasUtils).createClient();
service.search(queryParent,pageable);
}
Your idea with spying is adequate (you can even go for mocking if you do not actually need any true implementation of that class).
The problem lies in the implementation:
// Some code
client = new ApacheAtlasUtils().createClient();
//some code
}
Instead of having the ApacheAtlasUtils as an instance variable (or a supplier method) you create the instance on the fly.
Mockito is not smart enough to catch that operation and replace the real object with you spy.
With the supplier method you can set up your test as follows:
#Spy
private FileSearchService fileSearchService = new FileSearchService();
#Spy
private ApacheAtlasUtils apacheAtlasUtils = new ApacheAtlasUtils();
#Mock
private AtlasClientV2 client;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
doReturn(apacheAtlasUtils).when(fileSearchService).getApacheUtils();
}
in your SUT:
#Override
public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
throws AtlasServiceException {
// Some code
client = getApacheUtils().createClient();
//some code
}
ApacheAtlasUtils getApacheUtils(){
return new ApacheAtlasUtils();
}

how do i mock method, calling one method from another method?

I'm New in Mocking.
I've a service I'm trying to call is let say name A, I need to test someMethod.
#Service
public class A {
private Logger logger = LoggerFactory.getLogger(getClass());
private final CoreXReader coreXReader;
#Autowired
B b;
#Autowired
C c;
#Async
public void someMethod(Config config) throws Exception {
pushConfig(config);
}
private void pushConfig(Config config) throws Exception {
String url = config.getBaseurl() + config.getId();
ABCRestClient restClient = new ABCRestClient(url);
String jobJson = restClient.callRestMethod(HttpMethod.GET, "");
}
}
sample of ABCRestClient
public class ABCRestClient {
private Logger logger = LoggerFactory.getLogger(getClass());
private String url;
public ABCRestClient(String url) {
this.url = url;
}
public String callRestMethod(HttpMethod method, String payload) throws Exception {
someresponse="example response";
return someresponse;
}
}
I'm trying to test by creating mockSpy but it still Calling its 'callRestMethod'
#RunWith(SpringRunner.class)
#SpringBootTest // (webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {
#Autowired
private A a;
private Logger logger = LoggerFactory.getLogger(getClass());
#Before
public void prepareMockDataForService() throws Exception {
ABCRestClient apiClient = new ABCRestClient(config.getBaseurl() + config.getId() );
ABCRestClient apiClientSpy=Mockito.spy(apiClient);
doReturn(getCallResponse()).when(apiClientSpy).callRestMethod(HttpMethod.GET, "");
}
#Test
public void TestPushConfig() throws Exception {
a.someMethod(StubDataGenerator.getConfig());
}
private String getCallResponse() {
return "{"msg":"sample response"}";
}
}
i'm not sure what I'm doing wrong here why its calling the actual callRestMethod as i already create a spy .
I tried using this too Mockito.doReturn(getCallResponse()).when(apiClientSpy.callRestMethod(HttpMethod.GET, ""))
Also, is there any difference in these two statement if I use it Mockito.doReturn() or directly doReturn()? In my case both seems behaving same.
Before I tried with this as well when().thenReturn(); but I read somewhere that use when().thenReturn() when you actually want to make call. Please correct if my understanding is wrong.
You can try mock instead of spy:
#RunWith(SpringRunner.class)
#SpringBootTest // (webEnvironment=
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {
#Autowired
private A a;
private Logger logger = LoggerFactory.getLogger(getClass());
#Before
public void prepareMockDataForService() throws Exception {
ABCRestClient apiClientSpy=Mockito.mock(ABCRestClient.class);
doReturn(getCallResponse()).when(apiClientSpy).callRestMethod(HttpMethod.GET, "");
}
#Test
public void TestPushConfig() throws Exception {
a.someMethod(StubDataGenerator.getConfig());
}
private String getCallResponse() {
return "{"msg":"sample response"}";
}
}

Use Dropwizard configuration in a method that establishes a connection to a MongoDB database

I am coding Dropwizard micro-services that fetch data in a MongoDB database. The micro-services work fine but I'm struggling to use in my DAO the configuration coming from my Dropwizard configuration Java class. Currently I have
public class XDAO implements IXDAO {
protected DB db;
protected DBCollection collection;
/* singleton */
private static XDAO instance;
/* Get singleton */
public static synchronized XDAO getSingleton(){
if (instance == null){
instance = new XDAO();
}
return instance;
}
/* constructor */
public XDAO(){
initDatabase();
initDatabaseIndexes();
}
private void initDatabase(){
MongoClient client = null;
try {
client = new Mongo("10.126.80.192",27017);
db = client.getDB("terre");
//then some other code
}
catch (final MongoException e){
...
}
catch (UnknownHostException e){
...
}
}
}
I want to unhard-code the three arguments in these two lines :
client = new Mongo("10.126.80.192", 27017);
db = client.getDB("terre");
My MongoConfiguration Java class is :
public class MongoConfiguration extends Configuration {
#JsonProperty
#NotEmpty
public String host;
#JsonProperty
public int port = 27017;
#JsonProperty
#NotEmpty
public String db_name;
public String getMongohost() {
return host;
}
public void setMongohost(String host) {
this.host = host;
}
public int getMongoport() {
return port;
}
public void setMongoport(int port) {
this.port = port;
}
public String getDb_name() {
return db_name;
}
public void setDb_name(String db_name) {
this.db_name = db_name;
}
}
My Resource class that uses the DAO is :
#Path("/mongo")
#Produces(MediaType.APPLICATION_JSON)
public class MyResource {
private XDAO xDAO = XDAO.getSingleton();
private String mongohost;
private String db_name;
private int mongoport;
public MyResource(String db_name, String mongohost, int mongoport) {
this.db_name = db_name;
this.mongohost = mongohost;
this.mongoport = mongoport;
}
public MyResource() {
}
#GET
#Path("/findByUUID")
#Produces(value = MediaType.APPLICATION_JSON)
#Timed
public Entity findByUUID(#QueryParam("uuid") String uuid) {
return xDAO.findByUUid(uuid);
}
}
And in my application class there is
#Override
public void run(final MongoConfiguration configuration, final Environment environment) {
final MyResource resource = new MyResource(configuration.getDb_name(), configuration.getMongohost(), configuration.getMongoport());
environment.jersey().register(resource);
}
To solve my problem I tried many things. The last thing I tried was to add these four fields in my XDAO
private String mongohost;
private String db_name;
private int mongoport;
private static final MongoConfiguration configuration = new MongoConfiguration();
Coming with this piece of code in the constructor of the XDAO:
public XDAO(){
instance.mongohost = configuration.getMongohost();
instance.mongoport = configuration.getMongoport();
instance.db_name = configuration.getDb_name();
/* then like before */
initDatabase();
initDatabaseIndexes();
}
When I try this I have a null pointer exception when my initDatabase method is invoked : mongoHost and db_name are null
The problem is that you are creating a new configuration in your XDAO with private static final MongoConfiguration configuration = new MongoConfiguration(); instead of using the config from Dropwizard's run method.
When you do this, the fields host and db_name in the new configuration are null, which is why you are getting the NPE when instantiating XDAO
You need to pass the instance of MongoConfiguration that you get from Dropwizard in your application class to your XDAO, ideally when the singleton XDAO is created so it has non-null values for db_name and host
This code below part of the problem - you are creating the singleton without giving XDAO the MongoConfiguration configuration instance.
public class XDAO implements IXDAO {
//... snip
/* Get singleton */
public static synchronized XDAO getSingleton(){
if (instance == null){
instance = new XDAO(); // no configuration information is included!
}
return instance;
}
/* constructor */
public XDAO(){
initDatabase(); // this call needs db_name & host but you haven't set those yet!!
initDatabaseIndexes();
}
I recommend you modify your application class to create XDAO along the lines of this:
#Override
public void run(final MongoConfiguration configuration, final Environment environment) {
XDAO XDAOsingleton = new XDAO(configuration);
XDAO.setSingletonInstance(XDAOsingleton); // You need to create this static method.
final MyResource resource = new MyResource(configuration.getDb_name(), configuration.getMongohost(), configuration.getMongoport()); // MyResource depends on XDAO so must be created after XAO's singleton is set
environment.jersey().register(resource);
}
You may also need to take initDatabase() etc out of XDAO's constructor depending on if you keep public static synchronized XDAO getSingleton()
I also recommend you change the constructor of MyResource to public MyResource(XDAO xdao). The resource class doesn't appear to need the configuration information, and it is better to make the dependency on an XDAO explicit (you then also don't need to keep the XDAO singleton in a static field inside XDAO's class).
To get MongoDB integrated in a simple way to Dropwizard, please try and use MongoDB Managed Object. I will explain this in 3 simple steps:
Step 1: Create a simple MongoManged class:
import com.mongodb.Mongo;
import io.dropwizard.lifecycle.Managed;
public class MongoManaged implements Managed {
private Mongo mongo;
public MongoManaged(Mongo mongo) {
this.mongo = mongo;
}
#Override
public void start() throws Exception {
}
#Override
public void stop() throws Exception {
mongo.close();
}
}
Step 2: Mention MongoDB Host, Port, DB Name in a config yml file:
mongoHost : localhost
mongoPort : 27017
mongoDB : softwaredevelopercentral
Step 3: Bind everything together in the Application Class:
public class DropwizardMongoDBApplication extends Application<DropwizardMongoDBConfiguration> {
private static final Logger logger = LoggerFactory.getLogger(DropwizardMongoDBApplication.class);
public static void main(String[] args) throws Exception {
new DropwizardMongoDBApplication().run("server", args[0]);
}
#Override
public void initialize(Bootstrap<DropwizardMongoDBConfiguration> b) {
}
#Override
public void run(DropwizardMongoDBConfiguration config, Environment env)
throws Exception {
MongoClient mongoClient = new MongoClient(config.getMongoHost(), config.getMongoPort());
MongoManaged mongoManaged = new MongoManaged(mongoClient);
env.lifecycle().manage(mongoManaged);
MongoDatabase db = mongoClient.getDatabase(config.getMongoDB());
MongoCollection<Document> collection = db.getCollection(config.getCollectionName());
logger.info("Registering RESTful API resources");
env.jersey().register(new PingResource());
env.jersey().register(new EmployeeResource(collection, new MongoService()));
env.healthChecks().register("DropwizardMongoDBHealthCheck",
new DropwizardMongoDBHealthCheckResource(mongoClient));
}
}
I have used these steps and written a blog post and a sample working application code is available on GitHub. Please check: http://softwaredevelopercentral.blogspot.com/2017/09/dropwizard-mongodb-tutorial.html

Spring JUnit + Mockito + In memory Database (H2)

I'm currently using Mockito and In memory Database (H2) in order to test some services and dao.
I've tried to test a method (createRoad) of my service that contains some call of dao method :
#Service(value = "RoadsService")
public class RoadsService implements IRoadsService {
#Autowired
private IRoadsDao roadsDao;
...
#Override
public long createRoad(CreateRoadCriteria criteria) {
String roaCodeToCopy = "";
RoadsCriteria roadsCriteria = new RoadsCriteria();
roadsCriteria.setCe(criteria.getCe());
roadsCriteria.setDeliveryDate(new Date());
List<WebRoadsModel> listRoadsCe = roadsDao
.getRoadsByCEAndDeliveryDate(roadsCriteria);
for (WebRoadsModel wrm : listRoadsCe) {
if (wrm.getRoaCode().endsWith("90")) {
roaCodeToCopy = wrm.getRoaCode();
break;
}
}
criteria.setRoadCode(StringUtils.substring(roaCodeToCopy, 0, 4)
+ criteria.getRoadCode());
// 2. Creation of the new road
long idNewRoad = roadsDao.createNewRoad(criteria);
if (!"".equals(roaCodeToCopy) && listRoadsCe.get(0) != null) {
roaCodeToCopy = listRoadsCe.get(0).getRoaCode();
// 3. Copy dates closed of this trn
RoadsCriteria newRoadCreated = new RoadsCriteria();
newRoadCreated.setCe(criteria.getCe());
newRoadCreated.setRoadId(idNewRoad);
// 4. Paste it for the new road
deliveryCalendarDao.copyClosedDates(roadsDao
.getRoadByCriteria(newRoadCreated).get(0).getRoaCode(),
roaCodeToCopy);
}
return idNewRoad;
}
...
}
I need to mock the method called getRoadsByCEAndDeliveryDate, because the sql query behind that is not managed by the H2 driver (query with "RANK"). Then, I want to use H2 with the other method in this service.
I've tried this :
public class RoadsServiceTest {
#InjectMocks
private RoadsService roadsService;
#Mock
private MockRoadDao roadsDao;
#BeforeClass
public static void setUpClass() throws Exception {
}
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
#Test
public void createRoad(){
CreateRoadCriteria criteria = new CreateRoadCriteria();
criteria.setRoadCode("AA");
criteria.setRoadName("ANAME");
criteria.setCe("2");
criteria.setDriver(1);
criteria.setVehicle(1);
criteria.setSupplier(1);
List<WebRoadsModel> expected = new ArrayList<WebRoadsModel>();
Mockito.when(roadsDao.getRoadsByCEAndDeliveryDate((RoadsCriteria) Mockito.any())).thenReturn(expected);
Mockito.when(roadsDao.getNextVal(Mockito.anyString())).thenCallRealMethod();
Mockito.when(roadsDao.createNewRoad(criteria)).thenCallRealMethod();
long result = roadsService.createRoad(criteria);
Assert.assertEquals(5, result);
}
}
It is almost good except the assertEquals.
According to my sql file used with my in memory database, i have a sql sequence like this :
CREATE SEQUENCE "SEQ_SITI_ROADS" MINVALUE 1 MAXVALUE 99999 INCREMENT BY 1 START WITH 5 CACHE 20 ;
When I run my test, the result is 0.
It seems that the MockRoadDao is not initialized.
public class MockRoadDao extends RoadsDao {
public MockRoadDao() throws DatabaseUnitException, SQLException, MalformedURLException {
IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File("./src/test/resources/dataset.xml"));
DataSource dataSource = TestUtils.dataSource();
IDatabaseConnection dbConn = new DatabaseDataSourceConnection(dataSource);
DatabaseOperation.CLEAN_INSERT.execute(dbConn, dataSet);
User user = new User();
user.setLangCountry("ENFR");
BackendContextHolder.setBackendIdentifier(new BackendIdentifierImpl(user));
final NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
setJdbcTemplate(jdbcTemplate);
}
}
public class TestUtils {
private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
public static DataSource datasource;
/**
* #return
*/
public static DataSource dataSource() {
if (datasource == null) {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2).addScript("classpath:/siti-create.sql").build();
datasource = db;
}
return datasource;
}
...
}
Could you please help me :)
Thank you in advance
Edit :
#Baski : When I remove #Mock and use ReflectionUtils instead, the constructor of MockRoadDAO is called. But, when I run the test, the method roadsDao.getRoadsByCEAndDeliveryDate() is not mocked with Mockito, the method is really called and unfortunately the SQL query is not managed by the H2 driver.
public class RoadsServiceTest {
#InjectMocks
private RoadsService roadsService;
private MockRoadDao roadsDao;
#BeforeClass
public static void setUpClass() throws Exception {
}
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
roadsDao = new MockRoadDao();
ReflectionTestUtils.setField(roadsService, "roadsDao", roadsDao);
}
#Test
public void createRoad(){
CreateRoadCriteria criteria = new CreateRoadCriteria();
criteria.setRoadCode("AA");
criteria.setRoadName("ANAME");
criteria.setCe("2");
criteria.setDriver(1);
criteria.setVehicle(1);
criteria.setSupplier(1);
List<WebRoadsModel> expected = new ArrayList<WebRoadsModel>();
Mockito.when(roadsDao.getRoadsByCEAndDeliveryDate((RoadsCriteria) Mockito.any())).thenReturn(expected);
Mockito.when(roadsDao.getNextVal(Mockito.anyString())).thenCallRealMethod();
Mockito.when(roadsDao.createNewRoad(criteria)).thenCallRealMethod();
long result = roadsService.createRoad(criteria);
Assert.assertEquals(5, result);
}
}

Spring Framework and TestNG: unable to pass some parameters

I have some WebdriverSelenium/TestNG/Maven/Java continuous integration tests, that I refactored (removed a huge chain of inheritances) and now I've also installed Spring DI framework.
I just cant pass the parameters to the Test method (oneUserTwoUser)
This is the dataprovider
public class AppData {
public static WebDriver driver;
public static WebDriverWait wait;
final static String FILE_PATH = "src/test/resources/250.csv";
final static String FILE_PATH2 = "src/test/resources/places.csv";
public static ArrayList<ArrayList<String>> array;
public static Object[][] setUp() throws Exception {
//prepare data
//read data from CSV files
array = getCSVContent(FILE_PATH, 5);
array2 = getCSVContent(FILE_PATH2, 7);
//pass the data to the test case
Object[][] setUp = new Object[1][3];
setUp[0][0] = driver;
setUp[0][1] = wait;
setUp[0][2] = array;
return setUp;
}
This is the test class:
public class AppTest3 {
public static AppData appdata;
public static void main (String[] args) {
BeanFactory beanfactory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
appdata = (AppData) beanfactory.getBean("data");
}
#Parameters({ "driver", "wait", "array" })
#Factory(dataProviderClass = AppData.class, dataProvider = "setUp")
#Test
public void oneUserTwoUser(WebDriver driver, WebDriverWait wait, ArrayList<ArrayList<String>> array) throws Exception {
Error
org.testng.TestNGException:
Parameter 'driver' is required by #Test on method oneUserTwoUser but has not been marked #Optional or defined
As described by the documentation:
Preface your setUp() function with #DataProvider(name="standardTestData")
Then remove all other annotations except for #Test(dataProvider="standardTestData", dataProviderClass=AppData.class)

Categories