0

Test Class:

@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
public class MyControllerTest {

 @Autowired
 private MockMvc mvc;

 @MockBean
 private MyService myService;

 @Test
 public void testCreateEntit() throws Exception {
  ...
   given(myService.createEntity(eq(entity)))
            .willReturn(entity);
  ...
 }
}

Controller class:

@RestController
@RequestMapping("/")
public class MyController {
private MyService myService;
public MyController(final MyService myService) {
    this.myService = myService;
}
...
}

Service class:

public class DefaultMyService implements MyService {

 private MyRepository repository;

 public DefaultMyService(final MyRepository repository) {
    this.repository = repository;
 }
}

Getting the following error while running the test:

Parameter 0 of constructor in com.company.api.repositories.MyRepositoryImpl required a bean of type 'org.springframework.data.mongodb.core.MongoTemplate' that could not be found.

My question is why the @MockBean annotated class searches the actual dependency?

What I am doing wrong here, how to test mvc with MockMvc?

EDIT:

Versions

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.4.2.RELEASE</version>
</parent>

  <properties>
    <!-- use UTF-8 for everything -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <java.version>1.8</java.version>

    <spring.cloud.netflix.version>1.2.2.RELEASE</spring.cloud.netflix.version>
    <spring.social.google.version>1.0.0.RELEASE</spring.social.google.version>
    <spring.cloud.aws.version>1.1.3.RELEASE</spring.cloud.aws.version>

    <jwt.version>0.6.0</jwt.version>

    <apache.commons-lang3.version>3.4</apache.commons-lang3.version>
    <apache.commons.jexl.version>3.0</apache.commons.jexl.version>
  </properties>
Shafiul
  • 1,452
  • 14
  • 21
  • What version of Spring Boot are you using? Sounds like a bug we fixed. Try with 1.4.2 – Stephane Nicoll Nov 24 '16 at 15:56
  • Thank you for your reply. I am using version 1.4.1, I shall try with 1.4.2 and let you know. – Shafiul Nov 24 '16 at 16:08
  • That looked like https://github.com/spring-projects/spring-boot/issues/6663 but it's fixed in 1.4.1 already. Can you share a sample that breaks with 1.4.2 please? – Stephane Nicoll Nov 24 '16 at 16:14
  • Thanks for mentioning the known issue. The first test class I gave in my post is failing. I have tested even without any assertion and with simple test method. It fails with the error I mentioned. – Shafiul Nov 28 '16 at 10:42
  • When I try to upgrade spring boot version to 1.4.2, I get the an exception: java.lang.NoClassDefFoundError: org/springframework/test/web/servlet/DispatcherServletCustomizer – Shafiul Nov 28 '16 at 11:39
  • You have updated Spring Boot and still forcing an incompatible Spring Framework version. Please don't do that and let boot decide which version of Spring Framework to use. – Stephane Nicoll Nov 28 '16 at 12:38
  • spring boot is changing so make sure you have proper configuration – bananas Nov 28 '16 at 13:02
  • I have updated the post, with versions. @Stephane Can you please point out the incompatibility of versions.. – Shafiul Nov 28 '16 at 13:03
  • Even after updating Spring Boot to 1.4.2, I am getting the same error. – Shafiul Dec 01 '16 at 05:57

0 Answers0