I have a Java service which has few things injected by Guice.
public class RecoveryServiceImpl implements RecoveryService {
#Inject
public RecoveryServiceImpl(SessionInstanceCache sessionInstanceCache, AttendeeCache attendeeCache, MessagingStreamConfiguration messagingConfig) {
this.sessionInstanceCache = sessionInstanceCache;
this.attendeeCache = attendeeCache;
this.messagingConfig = messagingConfig;
}
#Override
public SessionInstanceData recoverSessionInstance(SessionInstanceDto sessionInstance) {
SessionInstanceData sessionInstanceData = SessionInstanceHelper.recoverSessionInstance(sessionInstance);
if (sessionInstanceData.getDeepstreamServerKey() == null) {
String dsKey = SessionInstanceHelper.pickRandomDeepstreamServerKey(
sessionInstanceData, messagingConfig);
And this does:
public static String pickRandomDeepstreamServerKey(
SessionInstanceData sessionInstanceData, MessagingStreamConfiguration dsConfig) {
// NPE occurs here
List<String> dsKeys = new ArrayList(dsConfig.getBaseUrls().keySet());
This is tested with Spock.
I added the messagingConfig and now I am struggling making it mocked in a Spock test:
class RecoveryServiceImplTest extends Specification {
...
MessagingStreamConfiguration msgConfig = Mock(MessagingStreamConfiguration);
RecoveryService recoveryService = new RecoveryServiceImpl(sessionInstanceCache, attendeeCache, msgConfig);
def "Recover session instance"() {
...
def dsMap = new HashMap<String, URL>();
dsMap.put("ds1", new URL("http://ilovemocking.com/"));
when:
msgConfig.getBaseUrls() >> dsMap;
//msgConfig.getBaseUrls().keySet() >> new HashSet(){{add("ds1")}};
recoveryService.recoverSessionInstance(sessionInstanceDto)
In the call to recoverSessionInstance(), I get a NPE because getBaseUrls() returns null.
I have tried other way, instantiating the msgConfig as a normal object (AKA "stubbing"), but same result.
How should I make Spock Mock to return the dsMap instead of null?
In the end, it was some issue with missing method in a dependency .jar (compared to what I saw as code). The NPE was coming from the Mock proxy rather than the expression itself. However it was hard to see in the stacktrace full of reflections.
I will delete the question in a while.
Related
In few classes I have similar else if statements that I wanted to replace using the command pattern. The idea was to externalize the else if bodies and depending on what service calls it use its repositories. Problem is that I can't seem to find a way to get the repositories injected into my class. Ill explain better in the example:
part of code that should use command pattern:
private HashMap<String, BnResponseHandler> commandMap = new HashMap<>();
for (CustomerDelivery customerDelivery : list) {
commandMap.put("true", new SuccessfulResponse(customerDelivery));
commandMap.put("1", new ResponseCodeOne(customerDelivery));
commandMap.put("2", new ResponseCodeTwo(customerDelivery));
commandMap.put("3", new ResponseCodeThree(customerDelivery));
BnResponseHandler bnResponseHandler = new DefaultHandler(customerDelivery);
if(response.isSuccessful()){
commandMap.get(String.valueOf(response.isSuccessful())).handle();
} else if (response.getResponseCode() != null
&& !response.getResponseCode().equals("")){
commandMap.get(response.getResponseCode()).handle();
} else {
bnResponseHandler.handle();
}
}
the interface:
public interface BnResponseHandler {
void handle();
}
and the interface implementation:
#Component
public class ResponseCodeOne implements BnResponseHandler {
#Autowired
private FabricationRepository fabricationRepo;
private Fabrication fabrication;
#Autowired
private MovementsRepository movementsRepository;
private StockMovements stockMovements;
#Autowired
private CustomerDeliveryRepository customerDeliveryRepo;
private CustomerDelivery customerDelivery;
public ResponseCodeOne() {
}
public ResponseCodeOne(Fabrication fabrication) {
this.fabrication = fabrication;
}
public ResponseCodeOne(StockMovements stockMovements) {
this.stockMovements = stockMovements;
}
public ResponseCodeOne(CustomerDelivery customerDelivery) {
this.customerDelivery = customerDelivery;
}
#Override
public void handle() {
if (fabrication != null) {
fabrication.setFG_ETAT("D");
fabrication.setLB_ETAT("");
fabricationRepo.save(fabrication);
} else if (stockMovements != null) {
stockMovements.setFG_ETAT("D");
stockMovements.setLB_ETAT("");
movementsRepository.save(stockMovements);
} else if (customerDelivery != null) {
customerDelivery.setFG_ETAT("D");
customerDelivery.setLB_ETAT("");
customerDeliveryRepo.save(customerDelivery);
}
}
}
So the big picture was to set ResponseCode classes for every possible response code I could get (in this case 1) and inside of those classes do some business logic that depends on the repository beans. It should also be flexible so that I can use the ResponseCodeOne class for multiple different cases(meaning fabrication, stockMovements and customerDelivery). The problem I am having is that my repositories don't get injected this way and I keep on getting an null pointer exception. Is there a way to cleanly inject my repository beans?
The main issue I see is that you are creating your own instances of your response code classes using the new keyword, which means that Spring will not autowire them. To get them successfully autowired you will need to inject instances of them into the class where your for loop is located. However, this introduces a secondary issue where you can no longer provide an object from your for loop to the constructor of your response code instances. To solve this problem you can pass the Fabrication/StockMovements/CustomerDelivery instance as an argument to handle().
I have the following Java method:
public Appointment addAppointment(String client, Appointment appointment) {
String esbUrl = new ESBUrlHelper().getEsbUrl();
AppointmentClient appointmentClient = AppointmentClientFactory.getUnsecuredClient(esbUrl);
if (appointment.getId() == null) {
outputAppointment = appointmentClient.addAppointment(client, appointment);
}
return outputAppointment;
}
The method above makes a call to a third party REST client called appointmentClient.
The issue that I am having is that this is causing my test to fail.
How can I mock the appointmentClientobject within my unit tests?
Currently my test looks as follows:
#Test
public void shouldAddAppointment() {
// act
Appointment appointment = appointmentService.addAppointment(CLIENT_STRING, appointmentMock)
// assert
assertNotNull(appointment);
}
But I get the following error at line appointmentClient.addAppointment(client, appointment);:
org.jboss.resteasy.client.exception.ResteasyIOException: IOException
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
I want to mock something like as follows:
Mockito.when(appointmentClient.addAppointment(client, appointment)).thenReturn(appointmentMock);
You can try using PowerMockito for this.
First, you need to mock the static method call of AppointmentClientFactory class like below:
PowerMockito.mockStatic(AppointmentClientFactory.class);
PowerMockito.when(AppointmentClientFactory,"getUnsecuredClient",esbUrl).thenReturn(appointmentClient);
And also, when you use PowerMockito for mocking static methods, add the #PrepareForTest({AppointmentClientFactory.class}) annotation to the test class.
With your current code, the only way of mocking a call to AppointmentClientFactory#getUnsecuredClient would be using PowerMock, since the factory method is static. This is due to the hard coupling between your calling code addAppointment and the dependency here (i.e. the AppointmentClientFactory).
If I were you, I would avoid that as PowerMock is not the best way to do tests. Instead, what I would do, would be to inject the AppointmentClientFactory as a dependency thus allowing me to mock an instance of it during my tests.
This should be the best approach in twofold manner. Firstly, because you achieve less tightly coupled code and secondly because you do not need to use PowerMock for your unit-tests.
It's not impossible with Mockito. But your original problem is static method of AppointmentClientFactory. You definitely should change this method to the instance method (at least for better architecture) , for example:
public class AppointmentClientFactory {
public AppointmentClient getUnsecuredClient(String url) {
return new AppointmentClient(); //your implementation
}
}
Then your AppointmentService will look like (or close to it):
public class AppointmentService {
private final AppointmentClientFactory factory;
public AppointmentService() {
this(new AppointmentClientFactory());
}
public AppointmentService(AppointmentClientFactory factory) {
this.factory = factory;
}
public Appointment addAppointment(String client, Appointment appointment) {
String esbUrl = "";
Appointment outputAppointment = null;
AppointmentClient appointmentClient = new AppointmentClientFactory().getUnsecuredClient(esbUrl);
if (appointment.getId() == null) {
outputAppointment = appointmentClient.addAppointment(client, appointment);
}
return outputAppointment;
}
}
And then you will could write test like:
public class AppointmentTest {
private final String CLIENT_STRING = "";
#Test
public void shouldAddAppointment() {
AppointmentClientFactory clientFactory = Mockito.mock(AppointmentClientFactory.class);
AppointmentClient mockedClient = Mockito.mock(AppointmentClient.class);
AppointmentService service = new AppointmentService(clientFactory);
Appointment appointmentMock = new Appointment();
when(clientFactory.getUnsecuredClient(any())).thenReturn(mockedClient);
Appointment appointment = service.addAppointment(CLIENT_STRING, appointmentMock);
assertNotNull(appointment);
}
}
For school purposes I am creating an application that's working with a stock API.
I am trying to write a test for a method that gets all the stock data of the last 10 years. Instead of actually getting all that data, I want to throw an exception.
The method I Want to test:
#Override
public List<StockData> getAllTeslaStockData() throws AlphaVantageException {
List<StockData> stockData;
AlphaVantageConnector apiConnector = new AlphaVantageConnector(APIKEY, TIMEOUT);
TimeSeries stockTimeSeries = new TimeSeries(apiConnector);
try {
Daily responseDaily = stockTimeSeries.daily("TSLA", OutputSize.FULL);
stockData = responseDaily.getStockData();
} catch (AlphaVantageException e) {
LOGGER.log(Level.SEVERE, "something went wrong: ", e);
throw e;
}
return stockData;
}
The stockTimeSeries.daily(....) call can throw the AlphaVantageException.
I've mocked the TimeSeries class like this:
TimeSeries stockTimeSeries = mock(TimeSeries.class);
In my test class I want to mock this call, and return an exception instead of actual data.
when(stockTimeSeries.daily("TSLA", OutputSize.FULL)).thenThrow(new AlphaVantageException("No stock data available"));
Regardless of how I am trying to mock this bit of code, it'll never throw the exception. It will always just execute the code, and return valid stock data, instead of throwing the exception like i've tried to do.
How can I mock this bit of code, so that itll throw the exception I am expecting for my tests.
The AlphaVantageConnector, TimeSeries and Daily classes are part of a library used to access the stock API, so I can't change these classes.
I am using JUnit 4.12 and Mockito to try and achieve this.
You can use thenThrow() method. Below is the example
#Test(expected = NullPointerException.class)
public void whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
when(dictMock.getMeaning(anyString()))
.thenThrow(NullPointerException.class);
dictMock.getMeaning("word");
The TimeSeries object is created in the method itself, so you can't mock it - mocking is intended to mock members.
What you can do is to do something like
class YourClass {
private Supplier<TimeSeries> seriesCreator = () -> {
return new TimeSeries(new AlphaVantageConnector(APIKEY, TIMEOUT));
}
which you use to create the series in your method
#Override
public List<StockData> getAllTeslaStockData() throws AlphaVantageException {
TimeSeries stockTimeSeries = seriesCreator.get();
Now you can mock that Supplier.
#Mock Supplier<TimeSeries> seriesCreatorMock;
#InjectMocks MyClass sut;
and in your test
#Test(expected = AlphaVantageException.class)
void testException() {
when(seriesCreatorMock.get()).thenThrow(new AlphaVantageException());
sut.getAllTeslaStockData()
}
EDIT: as suggested by Angkur in the comments, the clean way would be to
class SeriesCreator implements Supplier<TimeSeries> {
public TimeSeries get() {
return new TimeSeries(new AlphaVantageConnector(APIKEY, TIMEOUT));
}
}
class YourClass {
private Supplier<TimeSeries> seriesCreator = new SeriesCreator();
// ...
The code in the main class is creating a new instance of TimeSeries which it will use every time this method is called, so the mocked TimeSeries object is not getting used at all.
TimeSeries stockTimeSeries = new TimeSeries(apiConnector); // --> This is not getting mocked
try {
Daily responseDaily = stockTimeSeries.daily("TSLA", OutputSize.FULL);
stockData = responseDaily.getStockData();
}
You should create another method in your class (or even a separate class if it better satisfies the SOLID principles) which returns you the TimeSeries object. Something like:-
<access modifier> TimeSeries getTimeSeries(...) {
}
and then this method should be mocked in the Junit, and when mocked, it should return the Mocked TimeSeries reference (which is created in TimeSeries stockTimeSeries = mock(TimeSeries.class); ). You would need to use .spy() on the main class (unless you are using a different class to create TimeSeries object) in order to be able to mock the specific method getTimeSeries() but not the others.
MainClass mainObject = Mockito.spy(new MainClass());
Mockito.when(mainObject.getTimeSeries()).thenReturn(stockTimeSeries);
Then, the method call stockTimeSeries.daily() will get actually mocked by your existing code :
when(stockTimeSeries.daily("TSLA", OutputSize.FULL)).thenThrow(new AlphaVantageException("No stock data available"));
NOTE: you should also consider using .anyString() style methods provided by Mockito API while mocking.
I am new to writing tests in java, and seem to be unable to test if a method of a class is called.
I am sending metrics to datadog, and want to test in the code if a function of another class was called.
It says I need to mock first, but I couldn't get it to work.
MetricRecorder.java
import com.timgroup.statsd.StatsDClient;
import com.timgroup.statsd.NonBlockingStatsDClient;
import com.google.common.base.Preconditions;
public class MetricRecorder {
private final String namespace;
private final static StatsDClient metrics = new NonBlockingStatsDClient(
"my.prefix",
"localhost",
8125,
new String[] {"tag:value"}
);
public MetricRecorder(String namespace) {
Preconditions.checkNotNull(namespace);
this.namespace = namespace;
}
public void inc(String metricName) {
this.inc(metricName, 1);
}
public void inc(final String metricName, final long value) {
Preconditions.checkNotNull(metricName);
try {
metrics.recordHistogramValue(MetricRecorder.name(namespace, metricName), value);
} catch (Exception e) {
logger.warn("Unable to record metric {} due to :", metricName, e);
}
}
...
}
MetricRecorderTest.java
public class MetricsRecorderTest {
#Test
public void metricsRecorderTest() {
MetricRecorder recorder = new MetricRecorder("dev");
recorder.inc("foo", 1);
verify(recorder.metrics, times(1)).recordHistogramValue(eq("dev.foo"), 1);
}
}
When I run the test I get this => org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type NonBlockingStatsDClient and is not a mock!
Any idea of how I should be testing if recordHistogramValue was called, and if so with what arguments?
Since it looks like StatsDClient is an interface of some kind, it would make your testing effort easier to simply inject this dependency into your object. Even if you're not using an IoC container like Spring or Guice, you can still somewhat control this simply by passing an instance of it in through the constructor.
public MetricRecorder(String namespace, StatsDClient client) {
Preconditions.checkNotNull(namespace);
Preconditions.checkNotNull(client);
this.namespace = namespace;
this.client = client;
}
This will make your testing simpler since all you realistically need to do is mock the object passed in during test.
Right now, the reason it's failing is because you're newing up the instance, and Mockito (in this current configuration) isn't equipped to mock the newed instance. In all honesty, this set up will make testing simpler to conduct, and you should only need your client configured in one area.
#RunWith(MockitoJUnitRunner.class)
public class MetricsRecorderTest {
#Test
public void metricsRecorderTest() {
StatsDClient dClientMock = Mockito.mock(StatsDClient.class);
MetricRecorder recorder = new MetricRecorder("dev", dClientMock);
recorder.inc("foo", 1);
verify(recorder.metrics).recordHistogramValue(eq("dev.foo"), 1);
}
}
You are getting things wrong here. You don't use a mocking framework to test your "class under test".
You use the mocking framework to create mocked objects; which you then pass to your "class under test" within a test case. Then your "code under test" calls methods on the mocked object; and by controlling returned values (or by verifying what happens to your mock); that is how you write your testcases.
So, your testcase for a MetricRecorder doesn't mock a MetricRecorder; it should mock the StatsDClient class; and as Makoto suggests; use dependency injection to put an object of that class into MetricRecorder.
Besides: basically writing "test-able" code is something that needs to be practiced. I wholeheartedly recommend you to watch these videos if you are serious about getting in this business. All of them; really (worth each second!).
I'm new to groovy and I'm trying to use Spock to do some integration tests.
I have a Java class FunctionalTestCase which I used as a framework before to do my tests.
For Spock, I'm still using this FunctionalTestCase from a groovy class. Most of the things worked without problems, but I have the following scenario:
In FunctionalTestCase I have something similar:
protected static void doSomething(#Nullable Object nullableObject)
{
SomeInterface<InstrumentQuotationDataImage> marketDataSnapshot = new SomeInterface<InstrumentQuotationDataImage>()
{
#Override
public InstrumentQuotationDataImage getData()
{
InstrumentQuotationDataImage instrumentQuotationData = new InstrumentQuotationDataImageImpl();
if (nullableObject != null)
{
instrumentQuotationData.setDataRecord(nullableObject);
}
return instrumentQuotationData;
}
};
marketData.getQuotationService().pushQuotation(marketDataSnapshot);
}
This method is called once at setup with a non null parameter, and pushes an object into a Service (this can be seen at the end of the code).
When I later want to call SomeInterface#getData from the object retrieved from that QuotationService, instead of nullableObject to be null it throws groovy.lang.MissingPropertyException.
Any idea why this happens from groovy? Using it from java everything works ok and the objects are null.