mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-01-14 05:41:12 +00:00
Move util class to test code
This commit is contained in:
parent
e38a9feebe
commit
095b1a35d9
2 changed files with 2 additions and 2 deletions
|
|
@ -23,7 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
* TestContext Framework: </p> <ul> <li><strong>Spring IoC container caching</strong> which spares us unnecessary set up
|
||||
* time between test execution.</li> <li><strong>Dependency Injection</strong> of test fixture instances, meaning that
|
||||
* we don't need to perform application context lookups. See the use of {@link Autowired @Autowired} on the <code>{@link
|
||||
* ClinicServiceSpringDataJpaTests#clinicService clinicService}</code> instance variable, which uses autowiring <em>by
|
||||
* ClinicServiceTests#clinicService clinicService}</code> instance variable, which uses autowiring <em>by
|
||||
* type</em>. <li><strong>Transaction management</strong>, meaning each test method is executed in its own transaction,
|
||||
* which is automatically rolled back by default. Thus, even if tests insert or otherwise change database state, there
|
||||
* is no need for a teardown or cleanup script. <li> An {@link org.springframework.context.ApplicationContext
|
||||
|
|
@ -38,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
|
||||
public class ClinicServiceSpringDataJpaTests {
|
||||
public class ClinicServiceTests {
|
||||
|
||||
@Autowired
|
||||
protected ClinicService clinicService;
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.samples.petclinic.util;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.orm.ObjectRetrievalFailureException;
|
||||
import org.springframework.samples.petclinic.model.BaseEntity;
|
||||
|
||||
/**
|
||||
* Utility methods for handling entities. Separate from the BaseEntity class mainly because of dependency on the
|
||||
* ORM-associated ObjectRetrievalFailureException.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @see org.springframework.samples.petclinic.model.BaseEntity
|
||||
* @since 29.10.2003
|
||||
*/
|
||||
public abstract class EntityUtils {
|
||||
|
||||
/**
|
||||
* Look up the entity of the given class with the given id in the given collection.
|
||||
*
|
||||
* @param entities the collection to search
|
||||
* @param entityClass the entity class to look up
|
||||
* @param entityId the entity id to look up
|
||||
* @return the found entity
|
||||
* @throws ObjectRetrievalFailureException if the entity was not found
|
||||
*/
|
||||
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
|
||||
throws ObjectRetrievalFailureException {
|
||||
for (T entity : entities) {
|
||||
if (entity.getId() == entityId && entityClass.isInstance(entity)) {
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
throw new ObjectRetrievalFailureException(entityClass, entityId);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue