Exception “At least one JPA metamodel must be present” thrown in Controller-Test

淺唱寂寞╮ 提交于 2019-12-11 05:33:06

问题


I am trying to test a controller class annotated with @RestController. I am using Spring-Boot 1.5.10.

The application itself starts up properly, but the unit test fails. Please bear in mind, that I am currently just trying to test the controller (and mock away the service - I will be testing the services later).

Here are some of my classes:

Application.java

package com.particles.authservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import({ ApplicationConfiguration.class })
public class Application {
    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

ApplicationConfiguration.java

package com.particles.authservice;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EntityScan(basePackageClasses = { Application.class, Jsr310JpaConverters.class })
@EnableJpaRepositories
public class ApplicationConfiguration {
}

AccountController.java

package com.particles.authservice.accountservice;

import ...

@RestController
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * This method attempts to login a user and issue a token if the login was successful.
     * <p>
     * If login fails due a login attempt with a non-existent username or an invalid password, an exception is thrown.
     * 
     * @param credentials
     *            ({@link Credentials}) credentials in a JSON-form, that can be unserialized into an object of this type
     * @param response
     *            ({@link HttpServletResponse}) response, which will be sent to the client;
     *            if the credentials were valid the response receives a JWT as an additional header
     * @return ({@link PersistableAccount}) JSON (automatically serialized from the given TO);
     *         if the request was successful, an additional header containing the freshly issued JWT is added into the response
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PersistableAccount login(@RequestBody final Credentials credentials, final HttpServletResponse response)
            throws IOException, URISyntaxException {
        final Optional<PersistableAccount> account = accountService.login(credentials);
        if (!account.isPresent()) {
            throw new AccountLoginFailedException(credentials);
        }
        response.setHeader("Token", jwtService.tokenForPersistableAccount(account.get()));
        return account.get();
    }
}

AccountControllerTest.java

package com.particles.authservice;

import static ...
import ...

@RunWith(SpringRunner.class)
@WebAppConfiguration
@WebMvcTest(AccountController.class)
public class AccountControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private AccountService accountServiceMock;

    @Test
    public void test() throws Exception {
        final Credentials credentials = TestHelper.createCredentials();
        final Optional<PersistableAccount> account = Optional.of(TestHelper.createPersistableAccount());

        given(accountServiceMock.login(credentials))
                                                    .willReturn(account);

        mockMvc
               .perform(MockMvcRequestBuilders.post("/login").accept(MediaType.APPLICATION_JSON))
               .andExpect(status().isOk());
    }
}

I have reduced the AccountController to just one endpoint and omitted imports for brevity purposes.

The test compiles just fine, but whenever I run the test, I receive the following (nested) exception (shortened - let me know if you need the full stacktrace):

Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
    at org.springframework.util.Assert.notEmpty(Assert.java:277)
    at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.<init>(JpaMetamodelMappingContext.java:52)
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:71)
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:26)
    at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 40 more

I have checked out a lot of similar questions, but the usual ways to resolve this did not seem to work in my case. In particular I tried the following:

  • Using spring-boot-starter-data-jpa (did not apply to me since I was using that dependency to begin with), managed version
  • Separating application from its (JPA-related) configuration due to problems in regards to @EnableJpaRepositories and possibly @EntityScan - to no avail (following the first reply in Getting "At least one JPA metamodel must be present" with @WebMvcTest, but while my application still starts just fine, the test still fails)
  • I have tried using JacksonTester - in fact I just want to test the controller functionality at the moment - to no avail either (ended up needing the context)
  • As far as I understand I am mocking away the actual service; so in fact I am not using any JPA Metamodels, am I?

Removing the @EnableJpaRepositories annotation solves the issue, but unfortunately it seems to break my application.

What am I doing wrong?


回答1:


Add ContextConfiguration. The test did not see ApplicationConfiguration, hence did not see any entity.

@ContextConfiguration(classes = {ApplicationConfiguration.class})
public class AccountControllerTest { ... }

Update:

Another thing the code is missing is @SpringBootTest. Try annotating the test class with this one.



来源:https://stackoverflow.com/questions/49642210/exception-at-least-one-jpa-metamodel-must-be-present-thrown-in-controller-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!