Search code examples
spring-bootstaticmockitotelemetry

java.lang.NoSuchMethodError Mockito LocalDate and static method


I'm using the Mockito 4.8.0 and Java 1.8 versions and I have a LocalDate variable which is sent to a static method where I get the error. The static method is to print logs using Telemetry. Thanks in advance.

@RunWith(MockitoJUnitRunner.class) 
public class CategoryServiceImplTest {

    @InjectMocks    
    private ItemServiceImpl itemService;

    @Test public void getItem() { 
        LocalDate startDate = LocalDate.of(2022, 12, 12); 
        itemService.getItem(startDate);    
    } 
}

public class ItemServiceImpl { 
    getItem(LocalDate  startDate){ 
        A.method(startDate); 
    } 
}

public class Utils {
    public static void method(final LocalDate startDate){} 
}

I tried to fix using mvn clean install and persists the problem. Please I appreciate any help or suggestion to solve it. This method works when it calls in other method.


Solution

  • I am able to execute CategoryServiceImplTest.java successfully which is similar in nature to what you have in your question. Looks like you just need to tweak your code a little bit to get passed errors. I have java 8, spring-boot-starter-parent 2.5.5 and junit 4.13.2 declared in my pom.xml. Your classes will most likely have differences than what I have below but this gives you an idea of how you can get passed your errors.

    CategoryService.java:

    package com.example.javalangnosuchmethoderror;
    
    public interface CategoryService {
        String getCategory();
    }
    

    CategoryServiceImpl.java

    package com.example.javalangnosuchmethoderror;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class CategoryServiceImpl implements CategoryService {
        @Override
        public String getCategory() {
            return "test";
        }
    }
    

    ItemService.java

    package com.example.javalangnosuchmethoderror;
    
    import java.time.LocalDate;
    
    public interface ItemService {
        LocalDate getItem(LocalDate startDate);
    }
    

    ItemServiceImpl.java

    package com.example.javalangnosuchmethoderror;
    
    import org.springframework.stereotype.Service;
    
    import java.time.LocalDate;
    
    @Service
    public class ItemServiceImpl implements ItemService {
    
        @Override
        public LocalDate getItem(LocalDate startDate) {
            Utils.method(startDate);
            return startDate;
        }
    }
    

    Utils.java

    package com.example.javalangnosuchmethoderror;
    
    import java.time.LocalDate;
    
    public class Utils {
        public static void method(final LocalDate startDate) {
        }
    }
    

    CategoryServiceImplTest.java:

    package com.example.javalangnosuchmethoderror;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.InjectMocks;
    import org.mockito.junit.MockitoJUnitRunner;
    
    import java.time.LocalDate;
    
    @RunWith(MockitoJUnitRunner.class)
    public class CategoryServiceImplTest {
    
        @InjectMocks
        private ItemServiceImpl itemService;
    
        @Test
        public void getItem() {
            LocalDate startDate = LocalDate.of(2022, 12, 12);
            itemService.getItem(startDate);
        }
    }