Getting nullpointer exception While implementing Test cases using Cucumber, Mockito and BDD











up vote
0
down vote

favorite












We are trying to implement test cases using Cucumber framework.
I am not much aware about that framework. but i had implemented some of test cases by doing some research on cucumber.



so i need two help from you guys.



First look at the code i have.



step Definition class:-



package com.motocho.business.businessServiceImpl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.motocho.exchange.database.dto.ExchangePollingConfig;
import com.motocho.exchange.database.repo.BuySellMotochoRepository;
import com.motocho.exchange.database.repo.ExchangePollingConfigRepository;
import com.motocho.exchange.database.repo.MarketInfoRepository;
import com.motocho.exchange.examples.allcoin.AllCoinExUtils;
import com.motocho.influx.database.InfluxDBHelper;
import com.motocho.influx.database.dao.impl.TickerPollDaoImpl;
import com.motocho.influx.database.dao.impl.TickerPollNormDaoImpl;
import com.motocho.influx.database.model.TickerPoll;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import org.influxdb.dto.Point;
import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trades;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(InfluxDBHelper.class)
public class SdAllCoinApiServiceImplTests extends BaseTestCaseCucumber
{
private final CurrencyPair currencyPair = AllCoinExUtils.TEST_CURRENCY_PAIR;
private AllCoinApiServiceImpl apiService;
private SdAllCoinApiServiceImplTests sdAllCoinApiServiceImplTests;
InfluxDBHelper dbHelper;
TickerPollDaoImpl tickerPollDao;
TickerPollNormDaoImpl tickerPollDao2;
TickerPoll tickerPoll = new TickerPoll();

void SdAllCoinApiServiceImplTests(){
dbHelper = Mockito.mock(InfluxDBHelper.class);
PowerMockito.mockStatic(InfluxDBHelper.class);
when(InfluxDBHelper.getInstance()).thenReturn(dbHelper);
tickerPollDao = new TickerPollDaoImpl();
tickerPoll.setLast(new BigDecimal(100));
tickerPoll.setBase("BTC");
tickerPoll.setCounter("USD");
tickerPoll.setCurrencyPair("BTC/USD");
tickerPoll.setCurrencyPairMarket("BTC/USD");
tickerPoll.setVolumeBase(new BigDecimal(7000));
tickerPoll.setTime(Instant.now());
tickerPoll.setAsk(new BigDecimal(500));
tickerPoll.setMarket("GEMINI");
tickerPoll.setTimestamp(System.currentTimeMillis());
tickerPoll.toString();
tickerPollDao2 = new TickerPollNormDaoImpl();
com.motocho.influx.database.model.normalized.TickerPoll poll = new com.motocho.influx.database.model.normalized.TickerPoll();
poll.setLast("" + (100));
poll.setBase("BTC");
poll.setCounter("USD");
poll.setCurrencyPair("BTC/USD");
poll.setCurrencyPairMarket("BTC/USD");
poll.setVolumeBase("" + (7000));
poll.setTime(System.currentTimeMillis());
poll.setAsk("" + (500));
poll.setMarket("GEMINI");
poll.setTimestamp(System.currentTimeMillis());
poll.toString();
}
@Before
@Given("^setup$")
public void setup() {
System.out.println("hello1");
MockitoAnnotations.initMocks(this);
dbHelper=Mockito.mock(InfluxDBHelper.class);
tickerPollDao=Mockito.mock(TickerPollDaoImpl.class);
tickerPollDao2=Mockito.mock(TickerPollNormDaoImpl.class);
marketInfoRepository = Mockito.mock(MarketInfoRepository.class);
pollingConfigRepository = Mockito.mock(ExchangePollingConfigRepository.class);
buySellMotochoRepository = Mockito.mock(BuySellMotochoRepository.class);
sdAllCoinApiServiceImplTests=new SdAllCoinApiServiceImplTests();
apiService = new AllCoinApiServiceImpl(buySellMotochoRepository, marketInfoRepository, pollingConfigRepository);
super.setUp(apiService, currencyPair);
}

@Given("^get data through api test case$")
public void get_data_through_api_test_case() throws Throwable {
System.out.println("hello2");
Trades publicTrades = apiService.getPublicTrades(currencyPair);
Ticker ticker=apiService.getTicker(currencyPair);
OrderBook orderBook=apiService.getOrderBook(currencyPair,20);
assert (publicTrades != null);
assert (ticker !=null);
assert (orderBook !=null);
}
@When("^processing of fetched data$")
public void processing_of_fetched_data()throws Throwable{
sdAllCoinApiServiceImplTests.write();
sdAllCoinApiServiceImplTests.writeWithLogs();
sdAllCoinApiServiceImplTests.getAll();
sdAllCoinApiServiceImplTests.query();
sdAllCoinApiServiceImplTests.queryWithResult();
}
@Override
public ExchangePollingConfig getExchangeName() {
return new ExchangePollingConfig();
}
private void write() {
when(dbHelper.write(Mockito.any(Point.class))).thenReturn(null);
Assert.assertEquals(tickerPollDao.create(tickerPoll), tickerPoll);
}
private void writeWithLogs() throws JsonProcessingException {
when(dbHelper.writeWithLogs(anyString(), Mockito.any(Point.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
Assert.assertEquals(tickerPollDao.createAndLog(data, tickerPoll), tickerPoll);
}

private void getAll() throws JsonProcessingException {
when(dbHelper.getAll(anyString(), Mockito.any(Consumer.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
tickerPollDao.readAll("ABC", null);

}
private void query() throws JsonProcessingException {
when(dbHelper.query(anyString(), Mockito.any(Consumer.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
tickerPollDao.query("ABC", null);
}

private void queryWithResult() throws JsonProcessingException {
when(dbHelper.query(anyString(), any(TimeUnit.class))).thenReturn(null);
Assert.assertNull(tickerPollDao.queryWithQueryResult("ABC"));
}
}


Cucumber Runner class:-



package com.motocho.business.businessServiceImpl;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class RunAllCoinApiServiceImplTests {
}


Feature File:-



Feature: AllCoinApiServiceImpl test cases

Background:
Given setup

Scenario: all coin data api calling and storing data in database
Given get data through api test case
When processing of fetched data


Help 1:



I got Error in third(test Case of "When") test case.



Error:




java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
F
1 Scenarios (1 failed)
3 Steps (1 failed, 2 passed)
0m6.738s
java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
Process finished with exit code 255




Help 2:



need to more understand how cucumber works and execute in details.










share|improve this question
























  • You're using JUnits @Before rather then Cucumbers. This means your setup isn't done. Also @RunWith(MockitoJUnitRunner.class) and @PrepareForTest(InfluxDBHelper.class) won't do anything. Cucumber doesn't process JUnit annotations on anything but the runner class and only a limited set at that.
    – mpkorstanje
    Nov 20 at 12:23












  • @mpkorstanje so any suggestion for this. any guidance from you will be appreciated.
    – karan shah
    Nov 20 at 13:29










  • anyone who can answer this question?
    – karan shah
    Nov 21 at 11:24










  • I can't help you much more then that. The things I mentioned are wrong. Trying fixing those and then you can continue by your problem smaller stackoverflow.com/help/mcve
    – mpkorstanje
    Nov 21 at 16:56















up vote
0
down vote

favorite












We are trying to implement test cases using Cucumber framework.
I am not much aware about that framework. but i had implemented some of test cases by doing some research on cucumber.



so i need two help from you guys.



First look at the code i have.



step Definition class:-



package com.motocho.business.businessServiceImpl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.motocho.exchange.database.dto.ExchangePollingConfig;
import com.motocho.exchange.database.repo.BuySellMotochoRepository;
import com.motocho.exchange.database.repo.ExchangePollingConfigRepository;
import com.motocho.exchange.database.repo.MarketInfoRepository;
import com.motocho.exchange.examples.allcoin.AllCoinExUtils;
import com.motocho.influx.database.InfluxDBHelper;
import com.motocho.influx.database.dao.impl.TickerPollDaoImpl;
import com.motocho.influx.database.dao.impl.TickerPollNormDaoImpl;
import com.motocho.influx.database.model.TickerPoll;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import org.influxdb.dto.Point;
import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trades;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(InfluxDBHelper.class)
public class SdAllCoinApiServiceImplTests extends BaseTestCaseCucumber
{
private final CurrencyPair currencyPair = AllCoinExUtils.TEST_CURRENCY_PAIR;
private AllCoinApiServiceImpl apiService;
private SdAllCoinApiServiceImplTests sdAllCoinApiServiceImplTests;
InfluxDBHelper dbHelper;
TickerPollDaoImpl tickerPollDao;
TickerPollNormDaoImpl tickerPollDao2;
TickerPoll tickerPoll = new TickerPoll();

void SdAllCoinApiServiceImplTests(){
dbHelper = Mockito.mock(InfluxDBHelper.class);
PowerMockito.mockStatic(InfluxDBHelper.class);
when(InfluxDBHelper.getInstance()).thenReturn(dbHelper);
tickerPollDao = new TickerPollDaoImpl();
tickerPoll.setLast(new BigDecimal(100));
tickerPoll.setBase("BTC");
tickerPoll.setCounter("USD");
tickerPoll.setCurrencyPair("BTC/USD");
tickerPoll.setCurrencyPairMarket("BTC/USD");
tickerPoll.setVolumeBase(new BigDecimal(7000));
tickerPoll.setTime(Instant.now());
tickerPoll.setAsk(new BigDecimal(500));
tickerPoll.setMarket("GEMINI");
tickerPoll.setTimestamp(System.currentTimeMillis());
tickerPoll.toString();
tickerPollDao2 = new TickerPollNormDaoImpl();
com.motocho.influx.database.model.normalized.TickerPoll poll = new com.motocho.influx.database.model.normalized.TickerPoll();
poll.setLast("" + (100));
poll.setBase("BTC");
poll.setCounter("USD");
poll.setCurrencyPair("BTC/USD");
poll.setCurrencyPairMarket("BTC/USD");
poll.setVolumeBase("" + (7000));
poll.setTime(System.currentTimeMillis());
poll.setAsk("" + (500));
poll.setMarket("GEMINI");
poll.setTimestamp(System.currentTimeMillis());
poll.toString();
}
@Before
@Given("^setup$")
public void setup() {
System.out.println("hello1");
MockitoAnnotations.initMocks(this);
dbHelper=Mockito.mock(InfluxDBHelper.class);
tickerPollDao=Mockito.mock(TickerPollDaoImpl.class);
tickerPollDao2=Mockito.mock(TickerPollNormDaoImpl.class);
marketInfoRepository = Mockito.mock(MarketInfoRepository.class);
pollingConfigRepository = Mockito.mock(ExchangePollingConfigRepository.class);
buySellMotochoRepository = Mockito.mock(BuySellMotochoRepository.class);
sdAllCoinApiServiceImplTests=new SdAllCoinApiServiceImplTests();
apiService = new AllCoinApiServiceImpl(buySellMotochoRepository, marketInfoRepository, pollingConfigRepository);
super.setUp(apiService, currencyPair);
}

@Given("^get data through api test case$")
public void get_data_through_api_test_case() throws Throwable {
System.out.println("hello2");
Trades publicTrades = apiService.getPublicTrades(currencyPair);
Ticker ticker=apiService.getTicker(currencyPair);
OrderBook orderBook=apiService.getOrderBook(currencyPair,20);
assert (publicTrades != null);
assert (ticker !=null);
assert (orderBook !=null);
}
@When("^processing of fetched data$")
public void processing_of_fetched_data()throws Throwable{
sdAllCoinApiServiceImplTests.write();
sdAllCoinApiServiceImplTests.writeWithLogs();
sdAllCoinApiServiceImplTests.getAll();
sdAllCoinApiServiceImplTests.query();
sdAllCoinApiServiceImplTests.queryWithResult();
}
@Override
public ExchangePollingConfig getExchangeName() {
return new ExchangePollingConfig();
}
private void write() {
when(dbHelper.write(Mockito.any(Point.class))).thenReturn(null);
Assert.assertEquals(tickerPollDao.create(tickerPoll), tickerPoll);
}
private void writeWithLogs() throws JsonProcessingException {
when(dbHelper.writeWithLogs(anyString(), Mockito.any(Point.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
Assert.assertEquals(tickerPollDao.createAndLog(data, tickerPoll), tickerPoll);
}

private void getAll() throws JsonProcessingException {
when(dbHelper.getAll(anyString(), Mockito.any(Consumer.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
tickerPollDao.readAll("ABC", null);

}
private void query() throws JsonProcessingException {
when(dbHelper.query(anyString(), Mockito.any(Consumer.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
tickerPollDao.query("ABC", null);
}

private void queryWithResult() throws JsonProcessingException {
when(dbHelper.query(anyString(), any(TimeUnit.class))).thenReturn(null);
Assert.assertNull(tickerPollDao.queryWithQueryResult("ABC"));
}
}


Cucumber Runner class:-



package com.motocho.business.businessServiceImpl;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class RunAllCoinApiServiceImplTests {
}


Feature File:-



Feature: AllCoinApiServiceImpl test cases

Background:
Given setup

Scenario: all coin data api calling and storing data in database
Given get data through api test case
When processing of fetched data


Help 1:



I got Error in third(test Case of "When") test case.



Error:




java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
F
1 Scenarios (1 failed)
3 Steps (1 failed, 2 passed)
0m6.738s
java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
Process finished with exit code 255




Help 2:



need to more understand how cucumber works and execute in details.










share|improve this question
























  • You're using JUnits @Before rather then Cucumbers. This means your setup isn't done. Also @RunWith(MockitoJUnitRunner.class) and @PrepareForTest(InfluxDBHelper.class) won't do anything. Cucumber doesn't process JUnit annotations on anything but the runner class and only a limited set at that.
    – mpkorstanje
    Nov 20 at 12:23












  • @mpkorstanje so any suggestion for this. any guidance from you will be appreciated.
    – karan shah
    Nov 20 at 13:29










  • anyone who can answer this question?
    – karan shah
    Nov 21 at 11:24










  • I can't help you much more then that. The things I mentioned are wrong. Trying fixing those and then you can continue by your problem smaller stackoverflow.com/help/mcve
    – mpkorstanje
    Nov 21 at 16:56













up vote
0
down vote

favorite









up vote
0
down vote

favorite











We are trying to implement test cases using Cucumber framework.
I am not much aware about that framework. but i had implemented some of test cases by doing some research on cucumber.



so i need two help from you guys.



First look at the code i have.



step Definition class:-



package com.motocho.business.businessServiceImpl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.motocho.exchange.database.dto.ExchangePollingConfig;
import com.motocho.exchange.database.repo.BuySellMotochoRepository;
import com.motocho.exchange.database.repo.ExchangePollingConfigRepository;
import com.motocho.exchange.database.repo.MarketInfoRepository;
import com.motocho.exchange.examples.allcoin.AllCoinExUtils;
import com.motocho.influx.database.InfluxDBHelper;
import com.motocho.influx.database.dao.impl.TickerPollDaoImpl;
import com.motocho.influx.database.dao.impl.TickerPollNormDaoImpl;
import com.motocho.influx.database.model.TickerPoll;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import org.influxdb.dto.Point;
import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trades;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(InfluxDBHelper.class)
public class SdAllCoinApiServiceImplTests extends BaseTestCaseCucumber
{
private final CurrencyPair currencyPair = AllCoinExUtils.TEST_CURRENCY_PAIR;
private AllCoinApiServiceImpl apiService;
private SdAllCoinApiServiceImplTests sdAllCoinApiServiceImplTests;
InfluxDBHelper dbHelper;
TickerPollDaoImpl tickerPollDao;
TickerPollNormDaoImpl tickerPollDao2;
TickerPoll tickerPoll = new TickerPoll();

void SdAllCoinApiServiceImplTests(){
dbHelper = Mockito.mock(InfluxDBHelper.class);
PowerMockito.mockStatic(InfluxDBHelper.class);
when(InfluxDBHelper.getInstance()).thenReturn(dbHelper);
tickerPollDao = new TickerPollDaoImpl();
tickerPoll.setLast(new BigDecimal(100));
tickerPoll.setBase("BTC");
tickerPoll.setCounter("USD");
tickerPoll.setCurrencyPair("BTC/USD");
tickerPoll.setCurrencyPairMarket("BTC/USD");
tickerPoll.setVolumeBase(new BigDecimal(7000));
tickerPoll.setTime(Instant.now());
tickerPoll.setAsk(new BigDecimal(500));
tickerPoll.setMarket("GEMINI");
tickerPoll.setTimestamp(System.currentTimeMillis());
tickerPoll.toString();
tickerPollDao2 = new TickerPollNormDaoImpl();
com.motocho.influx.database.model.normalized.TickerPoll poll = new com.motocho.influx.database.model.normalized.TickerPoll();
poll.setLast("" + (100));
poll.setBase("BTC");
poll.setCounter("USD");
poll.setCurrencyPair("BTC/USD");
poll.setCurrencyPairMarket("BTC/USD");
poll.setVolumeBase("" + (7000));
poll.setTime(System.currentTimeMillis());
poll.setAsk("" + (500));
poll.setMarket("GEMINI");
poll.setTimestamp(System.currentTimeMillis());
poll.toString();
}
@Before
@Given("^setup$")
public void setup() {
System.out.println("hello1");
MockitoAnnotations.initMocks(this);
dbHelper=Mockito.mock(InfluxDBHelper.class);
tickerPollDao=Mockito.mock(TickerPollDaoImpl.class);
tickerPollDao2=Mockito.mock(TickerPollNormDaoImpl.class);
marketInfoRepository = Mockito.mock(MarketInfoRepository.class);
pollingConfigRepository = Mockito.mock(ExchangePollingConfigRepository.class);
buySellMotochoRepository = Mockito.mock(BuySellMotochoRepository.class);
sdAllCoinApiServiceImplTests=new SdAllCoinApiServiceImplTests();
apiService = new AllCoinApiServiceImpl(buySellMotochoRepository, marketInfoRepository, pollingConfigRepository);
super.setUp(apiService, currencyPair);
}

@Given("^get data through api test case$")
public void get_data_through_api_test_case() throws Throwable {
System.out.println("hello2");
Trades publicTrades = apiService.getPublicTrades(currencyPair);
Ticker ticker=apiService.getTicker(currencyPair);
OrderBook orderBook=apiService.getOrderBook(currencyPair,20);
assert (publicTrades != null);
assert (ticker !=null);
assert (orderBook !=null);
}
@When("^processing of fetched data$")
public void processing_of_fetched_data()throws Throwable{
sdAllCoinApiServiceImplTests.write();
sdAllCoinApiServiceImplTests.writeWithLogs();
sdAllCoinApiServiceImplTests.getAll();
sdAllCoinApiServiceImplTests.query();
sdAllCoinApiServiceImplTests.queryWithResult();
}
@Override
public ExchangePollingConfig getExchangeName() {
return new ExchangePollingConfig();
}
private void write() {
when(dbHelper.write(Mockito.any(Point.class))).thenReturn(null);
Assert.assertEquals(tickerPollDao.create(tickerPoll), tickerPoll);
}
private void writeWithLogs() throws JsonProcessingException {
when(dbHelper.writeWithLogs(anyString(), Mockito.any(Point.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
Assert.assertEquals(tickerPollDao.createAndLog(data, tickerPoll), tickerPoll);
}

private void getAll() throws JsonProcessingException {
when(dbHelper.getAll(anyString(), Mockito.any(Consumer.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
tickerPollDao.readAll("ABC", null);

}
private void query() throws JsonProcessingException {
when(dbHelper.query(anyString(), Mockito.any(Consumer.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
tickerPollDao.query("ABC", null);
}

private void queryWithResult() throws JsonProcessingException {
when(dbHelper.query(anyString(), any(TimeUnit.class))).thenReturn(null);
Assert.assertNull(tickerPollDao.queryWithQueryResult("ABC"));
}
}


Cucumber Runner class:-



package com.motocho.business.businessServiceImpl;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class RunAllCoinApiServiceImplTests {
}


Feature File:-



Feature: AllCoinApiServiceImpl test cases

Background:
Given setup

Scenario: all coin data api calling and storing data in database
Given get data through api test case
When processing of fetched data


Help 1:



I got Error in third(test Case of "When") test case.



Error:




java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
F
1 Scenarios (1 failed)
3 Steps (1 failed, 2 passed)
0m6.738s
java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
Process finished with exit code 255




Help 2:



need to more understand how cucumber works and execute in details.










share|improve this question















We are trying to implement test cases using Cucumber framework.
I am not much aware about that framework. but i had implemented some of test cases by doing some research on cucumber.



so i need two help from you guys.



First look at the code i have.



step Definition class:-



package com.motocho.business.businessServiceImpl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.motocho.exchange.database.dto.ExchangePollingConfig;
import com.motocho.exchange.database.repo.BuySellMotochoRepository;
import com.motocho.exchange.database.repo.ExchangePollingConfigRepository;
import com.motocho.exchange.database.repo.MarketInfoRepository;
import com.motocho.exchange.examples.allcoin.AllCoinExUtils;
import com.motocho.influx.database.InfluxDBHelper;
import com.motocho.influx.database.dao.impl.TickerPollDaoImpl;
import com.motocho.influx.database.dao.impl.TickerPollNormDaoImpl;
import com.motocho.influx.database.model.TickerPoll;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import org.influxdb.dto.Point;
import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trades;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(InfluxDBHelper.class)
public class SdAllCoinApiServiceImplTests extends BaseTestCaseCucumber
{
private final CurrencyPair currencyPair = AllCoinExUtils.TEST_CURRENCY_PAIR;
private AllCoinApiServiceImpl apiService;
private SdAllCoinApiServiceImplTests sdAllCoinApiServiceImplTests;
InfluxDBHelper dbHelper;
TickerPollDaoImpl tickerPollDao;
TickerPollNormDaoImpl tickerPollDao2;
TickerPoll tickerPoll = new TickerPoll();

void SdAllCoinApiServiceImplTests(){
dbHelper = Mockito.mock(InfluxDBHelper.class);
PowerMockito.mockStatic(InfluxDBHelper.class);
when(InfluxDBHelper.getInstance()).thenReturn(dbHelper);
tickerPollDao = new TickerPollDaoImpl();
tickerPoll.setLast(new BigDecimal(100));
tickerPoll.setBase("BTC");
tickerPoll.setCounter("USD");
tickerPoll.setCurrencyPair("BTC/USD");
tickerPoll.setCurrencyPairMarket("BTC/USD");
tickerPoll.setVolumeBase(new BigDecimal(7000));
tickerPoll.setTime(Instant.now());
tickerPoll.setAsk(new BigDecimal(500));
tickerPoll.setMarket("GEMINI");
tickerPoll.setTimestamp(System.currentTimeMillis());
tickerPoll.toString();
tickerPollDao2 = new TickerPollNormDaoImpl();
com.motocho.influx.database.model.normalized.TickerPoll poll = new com.motocho.influx.database.model.normalized.TickerPoll();
poll.setLast("" + (100));
poll.setBase("BTC");
poll.setCounter("USD");
poll.setCurrencyPair("BTC/USD");
poll.setCurrencyPairMarket("BTC/USD");
poll.setVolumeBase("" + (7000));
poll.setTime(System.currentTimeMillis());
poll.setAsk("" + (500));
poll.setMarket("GEMINI");
poll.setTimestamp(System.currentTimeMillis());
poll.toString();
}
@Before
@Given("^setup$")
public void setup() {
System.out.println("hello1");
MockitoAnnotations.initMocks(this);
dbHelper=Mockito.mock(InfluxDBHelper.class);
tickerPollDao=Mockito.mock(TickerPollDaoImpl.class);
tickerPollDao2=Mockito.mock(TickerPollNormDaoImpl.class);
marketInfoRepository = Mockito.mock(MarketInfoRepository.class);
pollingConfigRepository = Mockito.mock(ExchangePollingConfigRepository.class);
buySellMotochoRepository = Mockito.mock(BuySellMotochoRepository.class);
sdAllCoinApiServiceImplTests=new SdAllCoinApiServiceImplTests();
apiService = new AllCoinApiServiceImpl(buySellMotochoRepository, marketInfoRepository, pollingConfigRepository);
super.setUp(apiService, currencyPair);
}

@Given("^get data through api test case$")
public void get_data_through_api_test_case() throws Throwable {
System.out.println("hello2");
Trades publicTrades = apiService.getPublicTrades(currencyPair);
Ticker ticker=apiService.getTicker(currencyPair);
OrderBook orderBook=apiService.getOrderBook(currencyPair,20);
assert (publicTrades != null);
assert (ticker !=null);
assert (orderBook !=null);
}
@When("^processing of fetched data$")
public void processing_of_fetched_data()throws Throwable{
sdAllCoinApiServiceImplTests.write();
sdAllCoinApiServiceImplTests.writeWithLogs();
sdAllCoinApiServiceImplTests.getAll();
sdAllCoinApiServiceImplTests.query();
sdAllCoinApiServiceImplTests.queryWithResult();
}
@Override
public ExchangePollingConfig getExchangeName() {
return new ExchangePollingConfig();
}
private void write() {
when(dbHelper.write(Mockito.any(Point.class))).thenReturn(null);
Assert.assertEquals(tickerPollDao.create(tickerPoll), tickerPoll);
}
private void writeWithLogs() throws JsonProcessingException {
when(dbHelper.writeWithLogs(anyString(), Mockito.any(Point.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
Assert.assertEquals(tickerPollDao.createAndLog(data, tickerPoll), tickerPoll);
}

private void getAll() throws JsonProcessingException {
when(dbHelper.getAll(anyString(), Mockito.any(Consumer.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
tickerPollDao.readAll("ABC", null);

}
private void query() throws JsonProcessingException {
when(dbHelper.query(anyString(), Mockito.any(Consumer.class))).thenReturn(null);
String data = new ObjectMapper().writeValueAsString(tickerPoll);
tickerPollDao.query("ABC", null);
}

private void queryWithResult() throws JsonProcessingException {
when(dbHelper.query(anyString(), any(TimeUnit.class))).thenReturn(null);
Assert.assertNull(tickerPollDao.queryWithQueryResult("ABC"));
}
}


Cucumber Runner class:-



package com.motocho.business.businessServiceImpl;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class RunAllCoinApiServiceImplTests {
}


Feature File:-



Feature: AllCoinApiServiceImpl test cases

Background:
Given setup

Scenario: all coin data api calling and storing data in database
Given get data through api test case
When processing of fetched data


Help 1:



I got Error in third(test Case of "When") test case.



Error:




java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
F
1 Scenarios (1 failed)
3 Steps (1 failed, 2 passed)
0m6.738s
java.lang.NullPointerException
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.write(SdAllCoinApiServiceImplTests.java:144)
at com.motocho.business.businessServiceImpl.SdAllCoinApiServiceImplTests.processing_of_fetched_data(SdAllCoinApiServiceImplTests.java:132)
at ✽.When processing of fetched data(com/motocho/business/businessServiceImpl/AllCoinApiServiceImpl.feature:8)
Process finished with exit code 255




Help 2:



need to more understand how cucumber works and execute in details.







java unit-testing automated-tests mockito cucumber






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 10:48

























asked Nov 20 at 5:18









karan shah

4514




4514












  • You're using JUnits @Before rather then Cucumbers. This means your setup isn't done. Also @RunWith(MockitoJUnitRunner.class) and @PrepareForTest(InfluxDBHelper.class) won't do anything. Cucumber doesn't process JUnit annotations on anything but the runner class and only a limited set at that.
    – mpkorstanje
    Nov 20 at 12:23












  • @mpkorstanje so any suggestion for this. any guidance from you will be appreciated.
    – karan shah
    Nov 20 at 13:29










  • anyone who can answer this question?
    – karan shah
    Nov 21 at 11:24










  • I can't help you much more then that. The things I mentioned are wrong. Trying fixing those and then you can continue by your problem smaller stackoverflow.com/help/mcve
    – mpkorstanje
    Nov 21 at 16:56


















  • You're using JUnits @Before rather then Cucumbers. This means your setup isn't done. Also @RunWith(MockitoJUnitRunner.class) and @PrepareForTest(InfluxDBHelper.class) won't do anything. Cucumber doesn't process JUnit annotations on anything but the runner class and only a limited set at that.
    – mpkorstanje
    Nov 20 at 12:23












  • @mpkorstanje so any suggestion for this. any guidance from you will be appreciated.
    – karan shah
    Nov 20 at 13:29










  • anyone who can answer this question?
    – karan shah
    Nov 21 at 11:24










  • I can't help you much more then that. The things I mentioned are wrong. Trying fixing those and then you can continue by your problem smaller stackoverflow.com/help/mcve
    – mpkorstanje
    Nov 21 at 16:56
















You're using JUnits @Before rather then Cucumbers. This means your setup isn't done. Also @RunWith(MockitoJUnitRunner.class) and @PrepareForTest(InfluxDBHelper.class) won't do anything. Cucumber doesn't process JUnit annotations on anything but the runner class and only a limited set at that.
– mpkorstanje
Nov 20 at 12:23






You're using JUnits @Before rather then Cucumbers. This means your setup isn't done. Also @RunWith(MockitoJUnitRunner.class) and @PrepareForTest(InfluxDBHelper.class) won't do anything. Cucumber doesn't process JUnit annotations on anything but the runner class and only a limited set at that.
– mpkorstanje
Nov 20 at 12:23














@mpkorstanje so any suggestion for this. any guidance from you will be appreciated.
– karan shah
Nov 20 at 13:29




@mpkorstanje so any suggestion for this. any guidance from you will be appreciated.
– karan shah
Nov 20 at 13:29












anyone who can answer this question?
– karan shah
Nov 21 at 11:24




anyone who can answer this question?
– karan shah
Nov 21 at 11:24












I can't help you much more then that. The things I mentioned are wrong. Trying fixing those and then you can continue by your problem smaller stackoverflow.com/help/mcve
– mpkorstanje
Nov 21 at 16:56




I can't help you much more then that. The things I mentioned are wrong. Trying fixing those and then you can continue by your problem smaller stackoverflow.com/help/mcve
– mpkorstanje
Nov 21 at 16:56

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386651%2fgetting-nullpointer-exception-while-implementing-test-cases-using-cucumber-mock%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386651%2fgetting-nullpointer-exception-while-implementing-test-cases-using-cucumber-mock%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen