I'm new to Spring framework. I need to write Unit Test for JPA repository. I'm trying simple repository saveAndFlush()
method. But nothing saves in my repository. Here is my source code:
TestContext.class
@Configuration
@PropertySource("classpath:log4j.properties")
public class TestContext {
@Bean
public RoleService roleService() {
return Mockito.mock(RoleService.class);
}
@Bean
public RightService RightService() {
return Mockito.mock(RightService.class);
}
@Bean
public RoleRepository RoleRepository() {
return Mockito.mock(RoleRepository.class);
}
}
RoleServiceTest.class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class})
@WebAppConfiguration
public class RoleServiceTest {
@Autowired
private RoleRepository roleRepository;
@Test
public void TestServices() throws Exception {
RoleDetails first = new RoleDetails();
first.setId("1");
first.setDescription("First Description");
first.setName("First");
roleRepository.saveAndFlush(new RoleEntity(first));
roleRepository.save(new RoleEntity(first));
List<RoleEntity> roles = new ArrayList<RoleEntity>();
roles = roleRepository.findAll();
System.out.println(roles);
assertEquals(1, roles.size());
}
}
And error:
java.lang.AssertionError: expected:<1> but was:<0>
I'm almost sure that problem occurs because of testContext.Class. I used this class to test my controller and it worked well, but now i need to test my database and i don't know how to modify contextConfiguration. I hope somone will help me. Thanks in advance!
The problem is from the TestContext indeed. You try to save your object using a mock object, which is not correct.
The solution is to use the real repository. For this, you need to follow the next steps:
I hope my answer helps, if you still need help feel free to ask again!
If using Spring Boot, creating a web app and you're running off of a main() method within an Application.class you could use:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class MyUnitTest {
Some someInstance = new Some();
@Autowired
private SomeRepository someRepository;
}
@Test
public void testSomeClass() throws Exception {
Some savedSome = someRepository.save(someInstance);
assertEquals(1, someRepository.count());
}
Your repository is a mock object. A mock object, by definition, is an object that doesn't do what it should normally do, but does what you tell it to do in the test.
To test the repository, the repository must be a real one. Your context class should thus rather have
@Bean
public RoleRepository RoleRepository() {
return new RoleRepositoryImpl(); // or whatever the class implementing the repository is
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With