Saiful Azad
Saiful Azad

Reputation: 1941

Spring Boot Unit Test

I am new to spring boot. Need some suggestions Here my unit test class

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
public class EmployeeRepositoryTest {

@Autowired
protected EmployeeRepository employeeRepository;


@Test
public void insertEmploee(){

    Employee employee = new Employee();

    employee.setEmpName("Azad");
    employee.setEmpDesignation("Engg");
    employee.setEmpSalary(12.5f);

    employeeRepository.save(employee);

}

}

When I run it I get exception as

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotationAttributes(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/String;ZZ)Lorg/springframework/core/annotation/AnnotationAttributes;

at org.springframework.test.util.MetaAnnotationUtils$AnnotationDescriptor.<init>(MetaAnnotationUtils.java:290)
at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:365)
at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:360)
at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:191)
at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:198)
at 

Process finished with exit code -1

Upvotes: 5

Views: 3966

Answers (2)

abhinav kumar
abhinav kumar

Reputation: 1803

Instead of using @Autowired on EmployeeRepository we can use @MockBean cause we are writing unit tests we don't need to deal with the real data we just need to verify that the function is working fine or not. Check the below code

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class)//main springboot class
@WebAppConfiguration
public abstract class AbstractBaseTest {
   protected MockMvc mvc;
   @Autowired
   WebApplicationContext webApplicationContext;

   protected void setUp() {
      mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
   }

}    

public class EmployeeRepositoryTest extends AbstractBaseTest{

@MockBean
protected EmployeeRepository employeeRepository;

 @Override
   @Before
   public void setUp() {
      super.setUp();
   }

@Test
public void insertEmploee(){

    Employee employee = new Employee();

    employee.setEmpName("Azad");
    employee.setEmpDesignation("Engg");
    employee.setEmpSalary(12.5f);

       Mockito.doNothing().when(employeeRepository).save(Mockito.any(Employee.class));
       employeeRepository.save(employee);
       Mockito.verify(employeeRepository, Mockito.times(1)).save(employee);

}
}

Upvotes: 0

Smajl
Smajl

Reputation: 8015

It seems that your problem is solved (mixing the Spring dependency versions) but let me just expand the comment from @g00glen00b on how to write unit tests.

Make sure the following dependency is in your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

As pointed out in the comment, @RunWith(SpringJUnit4ClassRunner.class) causes the unit test to start the whole application and it is used rather for integration testing.

Fortunately, Spring-boot has built in dependency for Mockito which is just what you need for unit tests like this.

Now, your unit test could look something like this:

public class EmployeeRepositoryTest {

@InjectMocks
private EmployeeRepository employeeRepository;

@Mock
private Something something; // some class that is used inside EmployRepository (if any) and needs to be injected

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

@Test
public void insertEmploee(){

    Employee employee = new Employee();

    employee.setEmpName("Azad");
    employee.setEmpDesignation("Engg");
    employee.setEmpSalary(12.5f);

    employeeRepository.save(employee);

    Mockito.verify(...); // verify what needs to be verified
    }
}

Nice post about using Mockito can be found, for example, here.

Upvotes: 2

Related Questions