Questions tagged [mockstatic]

24 questions
22
votes
5 answers

Mock static method in JUnit 5 using Mockito

I am trying to mock static classes(org.apache.commons.beanutils.Beanutils) for Junit 5 test cases. I came across mockito-inline dependency helps with mocking static classes. I tried to use the mockito-inline in the project for some strange reason it…
7
votes
1 answer

Mockito 3.6: Using mockStatic in @Before or @BeforeClass with JUnit4

Mockito 3.6 supports mocking static methods under a try-with-resources block as explained here. Can someone let me know if static methods are mocked using Powermock in @Before or @BeforeClass can Mockito.mockStatic be used to replace them without an…
tuk
  • 5,941
  • 14
  • 79
  • 162
6
votes
1 answer

difference between Powermock and Powermockito

Can anyone could elaborate about PowerMock and PowerMockito. I didn't even get documentation for powermockito. Both used for mocking static and private methods in different way I guess. what are the similarities and usages? which…
anand krish
  • 4,281
  • 4
  • 44
  • 47
4
votes
0 answers

VerifyStatic on System class is failing with PowerMock 2.0.2

Reference to verifystatic on System class is failing after upgrading powermock version to 2X from 1X Below are my current configurations mockito-core - 2.27.0 powermock-api-mockito2 - 2.0.2 powermock-api-support - 2.0.2 powermock-module-junit4 -…
arkay
  • 149
  • 1
  • 12
1
vote
1 answer

Mocking static objects under completable future supply async

Consider below code snippet: class XYZ { public static getFieldABC(){ return new ABC(); } } ... CompletableFuture.supplyAsync(() -> XYZ.getFieldABC()) ... I need to unit test this piece of code. I have a static method getFieldABC which…
Jay Patel
  • 11
  • 1
1
vote
1 answer

How to mock LocalDate time inside an @Async method while writing unit tests?

I need to mock LocalDateTime inside an @Async method. But mocked localdatetime does not work inside Async method.But removing async works as expected.I have attached the code so far. public interface ConfigurationProcessor { void…
NNFdo
  • 29
  • 1
  • 7
1
vote
2 answers

JUnit Mockito: Testing a Static Method and Calling Another Stubbed Static Method Inside Not Working

class A { public static int f1() { return 1; } public static int f2() { return A.f1(); } } class ATest { @Test void testF2() { try (MockedStatic aStatic = Mockito.mockStatic(A.class)) { …
Samir
  • 3,923
  • 9
  • 36
  • 43
1
vote
0 answers

Mockito mockStatic on InjectMocks Object

I'm using Mockito 3.6 to take advantage of their mockStatic method. I am having issue when mocking a class that is also the main class to be used for testing. public class MainClass{ private HttpClient httpClient; public String…
Dariell Bowo
  • 21
  • 1
  • 6
1
vote
1 answer

Mockito cannot resolve a public static method of a class

I have a Component class called AppUtil with a bunch of public static methods: @Component public class AppUtil { // Methods, fields... public static List getActiveProfiles() { return…
Jason
  • 2,495
  • 4
  • 26
  • 37
1
vote
1 answer

Cucumber testng with PowerMockTestCase to mock static classes

I am using cucumber BDD, testng, java to write some BDD test. I would like to mock static classes in order to write my test. However when I write this testrunner, it fails to initialize the BDD scenarios. Complete Example(note the commented line…
abhish_gl
  • 363
  • 1
  • 10
1
vote
2 answers

SQLException: 'No suitable driver found' when PowerMockito mockStatic DriverManager.class

I'm trying to mockStatic DriverManager.class and get mockConnection but a real static void method is called getConnection instead. Tests class: @RunWith(PowerMockRunner.class) @PrepareForTest(DriverManager.class) public class MyClassTest { …
0
votes
0 answers

Mockito.mockStatic always returns null

I am trying to mock static method which always returns null. try (MockedStatic utilityMockedStatic = Mockito.mockStatic(SecretsUtility.class)) { utilityMockedStatic.when(() ->…
0
votes
1 answer

Mock LocalDateTime gives org.mockito.exceptions.misusing.MissingMethodInvocationException - Springboot Unit test writing

I need to mock LocalDateTime without using power mockito. But it gives be below error with a exception. when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles); Also,…
NNFdo
  • 29
  • 1
  • 7
0
votes
1 answer

Vertx JUNIT5 issue during mocking

I am unable to mock static classes for Vertx JUNIT5. Added all dependencies as well in pom.xml for Vertx JUNIT5. Code Snippet: @ExtendWith (VertxExtension.class) VertxTestClass { @BeforeEach public void beforeEach(Vertx…
0
votes
0 answers

mockStatic of Mockito vs mockStatic of PowerMock

In order to mock static methods for unit testing in Java we can either use PowerMock or we Can use mockStatic of Mockito. I already know that need to mock static method means code smell and I should refactor the original code but that is not…
codersaty
  • 23
  • 7
1
2