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.
java unit-testing automated-tests mockito cucumber
add a comment |
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.
java unit-testing automated-tests mockito cucumber
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
add a comment |
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.
java unit-testing automated-tests mockito cucumber
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
java unit-testing automated-tests mockito cucumber
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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