Spring Unit-test-JPA-repository

Ich bin neu in Spring-framework. Ich brauche zum schreiben von Unit-Test für JPA-repository. Ich versuche einfach repository saveAndFlush() Methode. Aber nichts spart in meinem repository. Hier ist mein 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>

Ich bin mir fast sicher, dass problem tritt auf, weil der testContext.Class. Ich verwendet diese Klasse zum testen meiner controller und es funktionierte gut, aber jetzt brauche ich zum testen meiner Datenbank und ich weiß nicht, wie zu ändern contextConfiguration. Ich hoffe, dass somone mir helfen wird. Vielen Dank im Voraus!

InformationsquelleAutor Egizeris | 2014-06-23
Schreibe einen Kommentar