复制 import org . junit . Test ;
import org . junit . runner . RunWith ;
import org . springframework . boot . test . autoconfigure . orm . jpa . DataJpaTest ;
import org . springframework . test . context . junit4 . SpringRunner ;
import org . springframework . transaction . annotation . Propagation ;
import org . springframework . transaction . annotation . Transactional ;
@ RunWith ( SpringRunner . class )
@ DataJpaTest
@ Transactional (propagation = Propagation . NOT_SUPPORTED )
public class ExampleNonTransactionalTests {
}
复制 import org . junit . * ;
import org . junit . runner . * ;
import org . springframework . boot . test . autoconfigure . orm . jpa . * ;
import static org . assertj . core . api . Assertions . * ;
@ RunWith ( SpringRunner . class )
@ DataJpaTest
public class ExampleRepositoryTests {
@ Autowired
private TestEntityManager entityManager;
@ Autowired
private UserRepository repository;
@ Test
public void testExample () throws Exception {
this . entityManager . persist ( new User( "sboot" , "1234" ) );
User user = this . repository . findByUsername ( "sboot" );
assertThat( user . getUsername()) . isEqualTo ( "sboot" );
assertThat( user . getVin()) . isEqualTo ( "1234" );
}
}
复制 @ RunWith ( SpringRunner . class )
@ DataJpaTest
@ AutoConfigureTestDatabase (replace = Replace . NONE )
public class ExampleRepositoryTests {
// ...
}