Quarkus Unit Test Mock The Serive classs - junit

I am Using Junit5 with Quarkus F/w.
when I try to Mock Service class by Using #InjectMock(io.quarkus.test.junit.mockito.InjectMock) it gives an error
Like org.junit.jupiter.api.extension.TestInstantiationException: Failed to create test instance
and another side when I am using #InjectMocks which belongs to (org.mockito.InjectMocks) package
Junit Running fine but, the Service class Did not Mock it is hit a real DAO class and inserted data
inside database
total conclusion in #InjectMocks is not working.
Please give me any suggestion but this mocking
Thank you.

Related

Mock a particular method in DAO layer in junit

I have an application with rest api endpoints. I want to write test cases for that. It follows MVC architecture. For one of the end points I want to mock a method in my DAO class.
Sample code for my test class is:
RequestBuilder requestGetBuilder = MockMvcRequestBuilders
.get("/processcal/getdata/srn/{srn}",1000)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
This controller will call the DAO layer having that method to be mocked.
I tried using the mockito as below in my Test config class:
#Bean
#Primary
BookMarkDao bookMarkDao() {
final BookMarkDao bookMarkDao = Mockito.mock(BookMarkDao.class);
Mockito.when(bookMarkDao.fetchMrPostProcessCalc(Mockito.anyString()))
.thenReturn(TestUtils.getMockResponse());
return bookMarkDao;
}
The problem with this is it's mocking the entire DAO bean so for rest of the endpoints its not calling the DAO class methods and my test coverage reduces. Is there a way around solving this?
You can use a specific profile for mocked beans and activate this profile in necessary test cases. By the way, if your application based on the spring-boot then you can use #MockBean instead of manual making a mock of your DAO in test configurations.

Powermock: Notifications are not supported when all test-instances are created first

When I try to execute the below simplified JUnit test, it succeeds but i am getting an error message: Notifications are not supported when all test-instances are created first!
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(BlockJUnit4ClassRunner.class)
#PrepareForTest({ A.class })
public class TestA extends TestB {
#Test
public void test() throws Exception {
assertEquals(true, true);
}
}
public class TestB {}
public class A {}
When I remove the #PowerMockRunnerDelegate(BlockJUnit4ClassRunner.class), or the extends TestB or the #PrepareForTest({ A.class }), the message disappears. Even an emtpy #PrepareForTest({ }) makes the error message appear.
I found this post, but it is not the same problem, because I am not using an extended BlockJUnit4ClassRunner.
I am trying to understand why this error message appears.
I am using the latest version of Powermock (1.6.6) and JUnit 4.12 and running the test using java8.
PowerMock extends jUnit events message and fire some additional event during test life circle. PowerMockTestListener could be used to listen all jUnit and additional PowerMock events.
One of this event required that an instance per test is created. A jUnit runner usually creates an new instance of test class per each test method in class, but some of them could create only one instance of class for all tests. When #PowerMockRunnerDelegate is used then delegated runner responsible for creating test instance.
If event "test start" is fired without “test created” then PowerMock detects that all test instances are created before tests are started and cannot fire its internal events and this message is printed to system.err. So, you have the same issue as it has been described in post.
PowerMock has only one implementation PowerMockTestListener at current moment - AnnotationEnabler. This implementation is used for integration with EasyMock and Mockito and supporting annotation like #TestSubject, #Mock and so on. As result, then you see this message in console it means that these features could not work properly. But for Mockito case it could be easier fixed with using MockitoAnnotations.initMocks(this).
Very interesting that if you remove ‘extends TestB’ the message is disappeared. I’ll check this case and investigate why it happen.

How to unmock using powermock

I am need to unmock a static method & call the real method in the class constructor as this will give connection to the DB. Now using powermock when I say #RunWith(PowerMockRunner.class) it doesnt allow me to call the real method. This was possible in mockito but I need to use powermock as I need to mock other static methods too.
public TestESMock() throws ConfigurationException{
DatabaseImpl dbImpl=DatabaseImpl.newDatabaseImpl(null);
}
Can someone tell me how to I do this.
There was no problem with powermockito. I was getting exception as powermockito 1.5.6 was not able to connect with SQL Server JDBC4

Deep / Nested Dependency Injection in testing

I am using junit, mockito and mockMVC to test the working of a webapp. I am struggling with a dependency whose injecttion I cannot figure out. My webapp structure is as follows.
Class Controller{
#Autowired Service searchService;
#RequestMapping("Search")
public returnType search(#RequestParam("parameter")String parameter){
searchService.doSearch(parameter);
}
}
and the service class
Class Service{
#Autowired Service2 service2;
public returnType doSearch(String parameter){
//some code
service2.call(parameter);
}
}
I need to test the search method for this controller, however service2 is not currently live and hence calls to it have to be mocked. I can mock service2, but cannot figure out how to inject a mock of service2 in my mockMVC instance of controller. As far as I know #InjectMocks in mockito only injects mocks one level deep and not two.
EDIT:
I am using the following to get MockMVC instance of controller
MockMvc controller;
controller = MockMvcBuilders.standaloneSetup(Controller);
What you are essentially want to do is mock a bean.
In your case, you have to mock bean for service2 using #MockBean annotations.
Please refer this article for details.
You don't need that.
Mocking search service will be sufficient as you get the handle of what needs to be done.
Example:
doReturn(...).when(searchService).doSearch(any());
While performing Unit Testing, the developer need to identify the System Under Test and mock/stub all the collaborators.
So, in this case you would write a separate unit test for Controller and Search Service.
Also, read this brilliant article by Martin Fowler - Mocks Aren't Stubs.

JUnit test suite #BeforeClass not running from individual tests

I have a test suite and a number of test in there own class files. These are selenium webdriver tests. Each test needs to start the webdriver before they start. How should this be done?
I can have the suite start the webdriver fine from its #BeforeClass. But when i try to run a single test from eclipse the webdriver doesnt start. The tests dont know that they are part of the suite and should run the suites #BeforeClass.
The single Tests would only run the #BeforeClass of the suite if their class extends the suite.
Due to the fact that that's a senseless relationship I think the solution for your Problem is either to define a BeforeClass in something like a TestFunctions.java file as Superclass for all Testclasses or create BeforeClasses for every single Testclass.
Keep in mind that the #BeforeClass and #Before Annotations of the superclass are executed before the #Before(Class) of the subclass but can be overridden.