Manually creating an SqlRowSet - java

I am using EasyMock and need to set expectations on a method that returns an SqlRowSet. Below is a snippet of code.
// SqlRowSet rowSet = new SqlRowSet(); <-- NOT SURE HOW TO MANUALLY CREATE THIS?
expect(myDao.getCustomerData("All_CUSTOMERS")).andReturn(rowSet);
Anybody know how to manually create an SqlRowSet?

For these types of scenario (i.e. creating a concrete instance of a class is difficult), I typically create a mocked instance. Such as:
SqlRowSet mockedSqlRowSet = createMock(SqlRowSet.class);

Related

How to mock a repository method no matter the parameters with mockito

I'm using spring framework and testing with Junit and mockito.
Right now I have a method in my service which is using a few objects I create in the test, let's call them configObject1 and configObject2, I send them to the method as a parameter, and then the method start making some calls to another repositories along those configuration objects, those repositories are mocked and work well, the method makes a List of "CalculusResult" from those queries/configObjects. After that I use a repository extending CRUDRepository and make a saveAll(). Then it should return an iterable with the entities, but for some reason after the saveAll method it returns an empty list.
Test:
#Test
...
configObject1 conf1 = new configObject1 (...);
configObject2 conf2 = new configObject2 (...);
Calculusresult calcRes = new CalculusResult(null,...,new java.sql.Date(system.currentTimeMilis()),...);
List<CalculusResult> resList= new ArrayList<CalculusResult>();
resList.add(calcRes);
Calculusresult calcRes2 = new CalculusResult(1,...,new java.sql.Date(system.currentTimeMilis()),...);
List<CalculusResult> resList2= new ArrayList<CalculusResult>();
resList2.add(calcRes2);
when(calculusResultRepository.saveAll(resList)).thenReturn(resList2);
...
assertTrue(!response.isEmpty())
Method from the service:
...//The method is building the list of calculusResults
resCalc.setDate(new java.sql.Date(system.currentTimeMilis()))
resList.add(calcres);//CalculusResult object is added to the list, this object is well made
List<CalculusResult> savedResults = (List<CalculusResult>) calculusResultRepository.saveAll(resList); //This returns an empty list (If I don't cast, it also returns nothing)
for(CalculusResult calcres : savedResults){
... //This makes nothing because savedResults is empty, making the response empty and failing the test.
Repository:
#Repository
public interface CalculusResultRepository extends CrudRepository<CalculusResult, Long> {
}
I'm not sure but I think the problem may be that the object I'm creating in the test is different to the one in the service because one of the attributes is an sql Date of the moment it's created, so maybe it's not triggering "when(calculusRepository.saveAll(reslist)..." in the test because the object created in the test and the one created in the service have different Dates in that atribute.
If that's the case, is there a way to fix it? Or is the problem a completely different thing?
You can use Mockito ArgumentMatchers to match any argument.
when(calculusResultRepository.saveAll(Mockito.any(List.class)))
.thenReturn(resList2);

Can we mock a spy object using mockito?

I have a class called RadiationControl and I created a spy for it in the following way.
RadiationControl radCtrl = new RadiationControl();
RadiationControl spyRadCtrl = Mockito.spy(radCtrl);
I have a chained method call inside a different class called StationMonitor which is being called by using the RadiationControl object. When I am trying to use the above created spy and trying to access that which has method parameters and they vary from time to time.
StationMonitorObject stationMonitorObject = radCtrl.getStationMonitorLoader().retrieveCVStationMonitorObject(Long.parseLong(syngId), status);
Thus with the above syntax when I try to stub the spy for that method call it's complaining to stub properly.
StationMonitorLoader stationMonitorLoader = StationMonitorLoader.getLoader(domain);
Mockito.doReturn(stationMonitorLoader).when(spyRadCtrl).getStationMonitorLoader();
Mockito.doReturn(stationMonitorObject).when(stationMonitorLoader).retrieveCVStationMonitorObject(any(Long.class), null);
Is there any better approach to deal such scenario ?
Is there any better approach to deal such scenario ?
Yes.
The problem here is:
radCtrl.getStationMonitorLoader()
.retrieveCVStationMonitorObject(Long.parseLong(syngId), status);
This is a violation of the law of demeter (aka don't talk to strangers!).
The method retrieveCVStationMonitorObject() should be available in class RadiationControl and delegate the call to its dependency (which looks like being a StationMonitorLoader...)

Mock Documentum IDfSession using Mockito

I have the following (simplified) function which I wish to check using JUnit:
protected IDfCollection getCollection(IDfSession session) throws DfException{
IDfQuery dfcQuery = new DfQuery();
dfcQuery.setDQL(MY_DQL);
return dfcQuery.execute(session, IDfQuery.READ_QUERY);
}
I have successfully tested it using real IDfSession, but I would like do it without connecting to the repository. So I tried to mock empty IDfSession using:
IDfSession mockedSession = Mockito.mock(IDfSession.class);
But I was given NullPointerException:
Caused by: java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
at java.util.StringTokenizer.<init>(StringTokenizer.java:204)
at com.documentum.fc.internal.util.SoftwareVersion.<init>(SoftwareVersion.java:53)
at com.documentum.fc.client.DfQuery.runQuery(DfQuery.java:136)
at com.documentum.fc.client.DfQuery.execute(DfQuery.java:208)
Not knowing what actually went wrong (which function of the mocked object returned null which was not expected) I created a simple class implementing IDfSession interface and used code coverage tool to check which function was called. I hoped to mock behavior of the function later using mockito. I seemed to be getServerVersion so I changed returned null to real value "6.5.0.355 SP3P0600 Linux.Oracle". Next called function was getBatchManager so I mocked returned object here as well. But now I get:
Caused by: java.lang.ClassCastException: com.example.model.mock.IDfSessionMocked cannot be cast to com.documentum.fc.client.impl.session.ISession
I tried to implement ISession interface in the IDfSessionMocked class, but it does not compile, for instance because one of the used types (namely com.documentum.fc.client.impl.session.ISessionListener) is not visible.
Here: http://www.informedconsulting.nl/blog/?p=187 I found information how to do it using powerMock. Another difference is that object is taken directly from session not using IDfQuery.
What should I do?
Update after comment
getBatchManager function was mocked and now it returns anonymous inner class object, with all the returned values set to false or 0 depending on the expected returned type. Function isFlushBatchOnQuery has been called according to coverage tool.
I am not an expert of Documentum but I think you need a more complex object, you could have a look at this repo https://github.com/ValentinBragaru/dfc-mock
IDfSessionMock is what you need I think.
I hope it helps.

Create Report Template with JRDataSource using Spring Custom Data Source

I have follow the tutorial from http://krams915.blogspot.com/2010/12/spring-3-mvc-jasper-integration.html to create a jasper report. Now, I need to create a template with Spring JRData Source.
What is the Factory class for Spring custom JRData Source ?
What is the static method to retrieve JRData Source ?
How to do it ?
In this case JRData Source is just a wrapper of a DAO class that is designed later in this tutorial (see SalesDAO). This class returns a list of sales objects (see Sales class for the full definition) which are constitute the Spring JRData Source.
You do not have to create any factory class / static method to access this data.
Use a JRBeanCollectionDataSource, is really easy to use, just create a new instance with a List of objects, the objects of your report.
List<Object> reportItems = getReportItems();
JRDataSource datasource = new JRBeanCollectionDataSource(reportItems);
Please see this source code, is a implementation of a JRDataSource, is a really simple interface. The main method is getFieldValue, this method receive a JRField, (this is another simple class that have the field name) and return a Object, the toString() is the string printed in the report.
Sorry for my bad english
Cheers

JAXBElement<Byte> declaration

I've generated classes from my XML .xsd and am trying to set a field EndpointID within in the class MeterSessionInputRF. The problem I'm having is that the setEndpointID method only accepts JAXBElement<Byte> as it's parameter.
I'm currently query a database to get the input for the setEndpointID method. This input can be a string, char, whatever I want it to be.
How do I create a JAXBElement<Byte>? I've tried using the ObjectFactory class but when I try and use it, I don't have the option of creating such an object.
Here's the code I already have to give some perspective.
if(moduleResults.next()){
MeterSessionInputRF msiRF = new MeterSessionInputRF();
msiRF.setRFFrequency(moduleResults.getFloat("id_amr_module"));
JAXBElement<Byte> endpointType;
byte epT = moduleResults.getByte("cd_module_typ");
endpointType.setValue(epT);
msiRF.setEndpointType(endpointType);
}
I keep getting the error that endpointType may have not been initialized. Is there a correct way to create the JAXBElement<Byte>?
The ObjectFactory class generated by XJC should have a method to do that for you. I know you said it wasn't there, but check again, there should be some method that returns an object of that type.

Categories