1

Here's the problem: I keep running into the MissingMatrixVariableException in my stacktrace when attempting to test my Repository interfaces. I've got 5 and want to increase my overall code coverage.

Be nice - I'm a noob. I've attempted to Mock after doing my own research, and it's been a bit overwhelming.

I've tried Mocking the repository and instantiated my MockMvc mockMvc variables. I've setUp the MockitoAnnotations.initMocks(this);

I run the test and for any testing I've done, I've hit the MissingMatrixVariableException error.

I've found nothing in Stack / Google searches for this Exception when testing repositories.

package example.repository;

import example.model.Metrics;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@EnableJpaRepositories
public interface MetricsRepository extends JpaRepository<Metrics, Long> {
    List<Metrics>findByDate(String date);

    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "delete from backlogreport.metrics where date = ?1")
    void deleteDate(String date);
}

This is my test class:

package example.repository;

import example.controller.RestApiController;
import example.model.Metrics;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder;
import java.util.Collections;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

public class MetricsRepositoryTest {

    @InjectMocks
    private RestApiController restApiController;

    @Mock
    private MetricsRepository MetricsRepository;

    private MockMvc mockMvc;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(restApiController).build();

    }

    @Test
    public void shouldGetByDate() throws Exception {
        Metrics Metrics = new Metrics();
        Metrics.setDate("2019-04-01");
        Metrics.setAgeLength("20 days old");
        when(MetricsRepository.findAll())
                .thenReturn(Collections.singletonList(Metrics));

        mockMvc.perform(get("/report/2019-04-01"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(print());
    }
}

What I'm trying to achieve is when the mockMvc is setup, I put in the date which accepts a String ("2019-04-01") and an age length of ("20 Days").

I then perform my urL "get" and expect the status is ok, then print... except I keep hitting this MissingMatrixVariableException. Any ideas?

goguma
  • 55
  • 1
  • 2
  • 11
  • `NoClassDefFoundError` almost always means you have mixed versions (in this case, Spring) in your project. – chrylis -cautiouslyoptimistic- Apr 17 '19 at 20:38
  • @chrylis do you happen to know which dep I can look into? should I focus on duplicate versions or a specific spring dep? – goguma Apr 17 '19 at 20:43
  • you'd think IntelliJ was intelligent enough to tell the user there are conflicting deps... – goguma Apr 17 '19 at 20:43
  • It's better to read about this type of error reasons here: https://stackoverflow.com/a/25011152/2137378 – Amin Heydari Alashti Apr 17 '19 at 20:46
  • Documentation states "that a matrix variable expected in the method parameters of an @RequestMapping method is not present among the matrix variables extracted from the URL." may be the root cause of this exception is that you are not setting all request parameters in your call to "mockMvc.perform(get("/report/2019-04-01")" – somshivam Apr 18 '19 at 06:02

0 Answers0