Mocking SqlRowSet - junit

Hey guys I have a question about SqlRowSet in junit testing. Does anyone have examples or ways to mock an SqlRowSet. To add fake values to it and using it to test your method? I know I am being a little vague here but I have never worked with an SqlRowSet till now so it is confusing to me. Thank you!

There are many frameworks for mocking the real objects for example Mockito, PowerMockito. Below snippet is written in Mockito. Update the test case as per your requirements.
#Test
public void testSqlRowSet() {
SqlRowSet sqlRowSet = Mockito.mock(SqlRowSet.class);
JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class);
Mockito.when(jdbcTemplate.queryForRowSet(Mockito.anyString(), Mockito.eq(Integer.class))).thenReturn(sqlRowSet);//Change the method parameters accordingly
Mockito.when(sqlRowSet.next()).thenReturn(true);
}

Related

Is there a way to share #Before code between tests suite classes?

I am testing a Cordova plugin in Java/Android and I need to initialize my Plugin class and set some state before I run my Tests.
#Before
public void beforeEach() throws Exception {
System.out.println("Creating new Instance ");
PowerMockito.mockStatic(Helpers.class);
PowerMockito.when(Helpers.canUseStorage(any(), any())).thenReturn(true);
MyLogger myLoggerMock = PowerMockito.mock(MyLogger.class);
PowerMockito.doNothing().when(myLoggerMock, "log", anyString());
PowerMockito.whenNew(MyLogger.class).withAnyArguments().thenReturn(myLoggerMock);
this.sut = spy(new FilePicker());
PowerMockito.doNothing().when(this.sut).pick(any(), any());
}
I want to create a Test Suite / Java Class per public function, but I do not want to repeat that code every time.
Is there a way to share that before each between test suites? I have found ClassRule but I think I do not do what I need (or I am understanding it wrong... I am really new in Java)
In Typescript we can share beforeEachfunctions with several suites, and each suite can have their own beforeEach
One possible ways is using inheritance:
Make all test classes extend from one "parent test" class and define a #Before in a parent class.
So it will be called automatically for all the subclasses:
public class ParentTest {
#Before
public void doInitialization() {
....
}
}
public class Test1Class extends ParentClass {
#Test
public void fooTest() {
// doInitialization will be executed before this method
}
#Test
public void barTest() {
// doInitialization will be executed before this method as well
}
}
Two notes:
Note 1
In the code you use sut (subject under test) - this obviously should not be in the parent's doInitialization method, so its possible that Test1Class will also have methods annotated with #Before (read here for information about ordering and so forth)
Then the `sut gets initialized with Spy which is frankly weird IMHO, the Subject Under Test should be a real class that you wrote, but that's beyond the scope of the question, just mentioning it because it can point on mistake.
Note 2
I'm writing it in an an attempt to help because you've said that you're new in Java, this is not strictly related to your question...
While this approach works in general you should be really cautious with PowerMockito. I'm not a PowerMockito expert and try to avoid this type of mocks in my code but in a nutshell the way it manipulates the byte code can clash with other tools. From your code: you can refactor the HelperUtils to be non-static and thus avoid PowerMocking in favor of regular mocking which is faster and much more safe.
As for the Logging - usually you can compromise on it in unit test, if you're using slf4j library you can config it to use "no-op" log for tests, like sending all the logging messages into "nothing", and not-seeing them in the console.

How to mock applog for Junits

Actually I am writing junits for a class using #Autowired AppLog .
And using this.applog.logInfo("field",+object.getFieldName()), they are trying to print values.
I am not getting how to mock this thing.
I am already using #RunWith(SpringRunner.class).
Can powerMock help in this case? I Will be thankful if someone can help.
In Spring we have a class know as [ReflectionTestUtils][1] which can be utilized if you are testing classes with dependencies. In this case as we have applog autowired.
So, we under #Before annotation method can define:
ReflectionTestUtils.setField(instanceOfClassWeAreTesting,"appLog",mockedApplogInstance).
Hope it help if someone face any issue in future.

How to test individual testng methods when it depends on other methods

Consider the following example code
public class PropertyServiceTest extends AbstractTestNGSpringContextTests {
#Test(groups={"property_service"})
public void testCreateProperty() { ... }
#Test(groups={"property_service"}, dependsOnMethods={"testCreateProperty"})
public void testCreateProperty1() {...P
}
When I execute individual test 'testCreateProperty1' using maven,
mvn -Dtest=PropertyServiceTest#testCreateProperty1
am getting the following exception.
Caused by: org.testng.TestNGException:
com.service.PropertyServiceTest.testCreateProperty1() is depending on method public void com.service.PropertyServiceTest.testCreateProperty(), which is not annotated with #Test
at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:95)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:245)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:316)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:51)
Kindly help me to resolve this issue.
This does not appear to be related to the Spring TestContext Framework.
Rather, this appears to be a configuration issue with TestNG.
You therefore might find the "How to solve test method dependencies?" discussion on Google Groups helpful.
Let us know if that helps you solve your issue.

Why should Test methods in Junit be defined public?

I was going through the documentation for junit tests but am unable to understand the need for defining tests as public.Could anyone share some info on this?
I read on https://github.com/junit-team/junit/blob/master/src/main/java/org/junit/Test.java
But am still not clear with the reason.
With is I meant why can't I write something as
#Test
private void testAdd(){ }
The JUnit framework calls your test methods from outside your test class. If your test methods are private, it won't be able to do that.
"Test classes, test methods, and lifecycle methods are not required to be public, but they must not be private."
Ref. the doc:
https://junit.org/junit5/docs/current/user-guide/#writing-tests-classes-and-methods
JUnit accesses your test methods by reflection. A SecurityManager can control access to private methods. Hence JUnit uses only public methods and fields for anything that is accessed by the framework.
In short: JUnit would fail to run private test methods if a SecurityManager is active that does not allow access to private methods.
try
#Test
public void testAdd(){ }
your testAdd method is private, it won't be able to do that.

How to use JExample with #Rule instead of #RunWith

I need to add dependencies between behavioural-test methods where I use #RunWith(SpringJUnit4ClassRunner.class) to run the tests.
I add Mockito to the mix by using:
#Rule
public MockitoRule mockitoRule = new MockitoRule();
Where MockitoRule is a short class implementing MethodRule applying Mokito behaviour, I managed to scrounge up somewhere.
Now the question: Anyone have ideas of how I would archive a somthing similar with JExample, ie: applaying it with a #Rule instead of using the #RunWith(JExample.class)?
Looking at Sourceforge and github, it doesn't look like there has been much development in JExample in the last couple of years, so there probably isn't a #Rule for JExample. I would contact the original author to see how easy it would be to add a TestRule.
At first glance, it seems that it would require a slight change to how JExample works, because the return values of the tests are actually used, whereas the base runners for JUnit assume that the methods are void return values.