我有以下代码:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TRConfig.class)
public class ARepositoryTest {
@Autowired
ARepository aRepository;
private A a = new A("value");
@Before
public void setup() {
aRepository.save(a);
}
@Test
public void findByValue1() {
assertEquals(a, aRepository.findByValue("value"));
}
@Test
public void findByValue2() {
assertEquals(null, aRepository.findByValue("inv"));
}
}
我在第二次测试时遇到以下错误,我不知道是否第一次通过,因为第二次是第一次运行:
org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find ...model.a.A with id 10; nested exception is javax.persistence.EntityNotFoundException: Unable to find model.a.A with id 10
但是当我这样做的时候,测试通过了:
@Test
public void findByValue() {
assertEquals(a, aRepository.findByValue("value"));
assertEquals(null, aRepository.findByValue("inv"));
}
为什么呢?我需要做些什么才能让它正常运行?
这是我的配置:
@EnableAutoConfiguration
@EnableJpaRepositories(basePackageClasses = ARepository.class)
@EntityScan(basePackageClasses = A.class)
@Import({ DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
public class TRConfig {
}