1

I am using MockedStatic in static Util.parse(string, class) method but when I am running is not mocking and its printing NULL, basically it should give me a static file as a response that I loaded before, for that I am using MockedStatic 3.4.4, or I am doing something wrong, if someone can help me with that.

public List<ServiceRecord> getListServices(Services.Type type, String env) throws Exception {

        Swagger swagger2 = Util.parse("{ss}", Swagger.class);
        System.out.println(swagger2);

this is my test class:

@RunWith(MockitoJUnitRunner.class)
public class RemoteServicesTest {
...     
@Test
 public void getListServicesLegacyTest() throws Exception {

        Swagger swaggerMock = IoUtil.readFromFile("mock.json", Swagger.class);


        try(MockedStatic<Util> mockUtil2 = Mockito.mockStatic(Util.class)) {
            mockUtil2.when(() -> Util.parse(eq("{ss}"), eq(Swagger.class))).thenReturn(swaggerMock);
            List<ServiceRecord> serviceRecordList2 = remoteCloudLegacyServices.getListServices(Services.Type.LEGACY, environment.getEnv());
        }

my pom.xml

 <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>3.4.4</version>
            <scope>test</scope>
        </dependency>
EliasM
  • 737
  • 1
  • 6
  • 14
  • I would check the following: 1. Is there ONLY mockito-inline on your classpath (mockito-core may interfere if you have it) 2. Imports: particularly eq static 3. readFromFile returns correct value? – Lesiak Nov 30 '22 at 19:56
  • 1. I have only mockito-inline in my pom.xml file. 2. readFromFile is returning a value, I noticed after PowerMockito.mockStatic(Util.class); or MockedStatic mockUtil2 = Mockito.mockStatic(Util.class) my previous values are reseting to null, that's why I am getting null... but I don't know why is setting null – EliasM Nov 30 '22 at 20:08

0 Answers0