Java SpringTestDBUnit不创建数据集
我使用Java SpringTestDBUnit不创建数据集,java,spring-boot,dbunit,spring-boot-test,spring-test-dbunit,Java,Spring Boot,Dbunit,Spring Boot Test,Spring Test Dbunit,我使用SpringBoot,springtestdbunit。 我写了一个测试: ShortenerAppTest: @TestPropertySource(locations = "classpath:application-test.properties") @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = ShortenerApp.class) @WebAppConfiguration public clas
SpringBoot
,springtestdbunit
。
我写了一个测试:
ShortenerAppTest
:
@TestPropertySource(locations = "classpath:application-test.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ShortenerApp.class)
@WebAppConfiguration
public class ShortenerAppTest {
@Test
public void contextLoads() {
}
}
@TestPropertySource(locations = "classpath:application-test.properties")
@TestExecutionListeners(DbUnitTestExecutionListener.class)
@SpringBootTest(classes = ShortenerApp.class)
@DirtiesContext
abstract class AbstractRepositoryTest extends AbstractTransactionalJUnit4SpringContextTests {
}
@DatabaseSetup(LinkRepositoryTest.DATASET)
@DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = LinkRepositoryTest.DATASET)
public class LinkRepositoryTest {
final static String DATASET = "classpath:datasets/link-table.xml";
private final static Long LINK_1_ID = 100500L;
private final static String LINK_1_TEXT = "http://www.www.ya.ru";
@Autowired
LinkRepository repository;
@Test
public void findOneExisting() {
System.out.println(DATASET);
Optional<Link> got = repository.findOne(LINK_1_ID);
assertThat(got.isPresent(), equalTo(true));
Link linkFromDb = got.get();
Link link = new Link();
link.setId(LINK_1_ID);
link.setUrl(LINK_1_TEXT);
assertThat(linkFromDb, equalTo(link));
}
}
存储库/AbstractRepositoryTest
:
@TestPropertySource(locations = "classpath:application-test.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ShortenerApp.class)
@WebAppConfiguration
public class ShortenerAppTest {
@Test
public void contextLoads() {
}
}
@TestPropertySource(locations = "classpath:application-test.properties")
@TestExecutionListeners(DbUnitTestExecutionListener.class)
@SpringBootTest(classes = ShortenerApp.class)
@DirtiesContext
abstract class AbstractRepositoryTest extends AbstractTransactionalJUnit4SpringContextTests {
}
@DatabaseSetup(LinkRepositoryTest.DATASET)
@DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = LinkRepositoryTest.DATASET)
public class LinkRepositoryTest {
final static String DATASET = "classpath:datasets/link-table.xml";
private final static Long LINK_1_ID = 100500L;
private final static String LINK_1_TEXT = "http://www.www.ya.ru";
@Autowired
LinkRepository repository;
@Test
public void findOneExisting() {
System.out.println(DATASET);
Optional<Link> got = repository.findOne(LINK_1_ID);
assertThat(got.isPresent(), equalTo(true));
Link linkFromDb = got.get();
Link link = new Link();
link.setId(LINK_1_ID);
link.setUrl(LINK_1_TEXT);
assertThat(linkFromDb, equalTo(link));
}
}
存储库/LinkRepositoryTest
:
@TestPropertySource(locations = "classpath:application-test.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ShortenerApp.class)
@WebAppConfiguration
public class ShortenerAppTest {
@Test
public void contextLoads() {
}
}
@TestPropertySource(locations = "classpath:application-test.properties")
@TestExecutionListeners(DbUnitTestExecutionListener.class)
@SpringBootTest(classes = ShortenerApp.class)
@DirtiesContext
abstract class AbstractRepositoryTest extends AbstractTransactionalJUnit4SpringContextTests {
}
@DatabaseSetup(LinkRepositoryTest.DATASET)
@DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = LinkRepositoryTest.DATASET)
public class LinkRepositoryTest {
final static String DATASET = "classpath:datasets/link-table.xml";
private final static Long LINK_1_ID = 100500L;
private final static String LINK_1_TEXT = "http://www.www.ya.ru";
@Autowired
LinkRepository repository;
@Test
public void findOneExisting() {
System.out.println(DATASET);
Optional<Link> got = repository.findOne(LINK_1_ID);
assertThat(got.isPresent(), equalTo(true));
Link linkFromDb = got.get();
Link link = new Link();
link.setId(LINK_1_ID);
link.setUrl(LINK_1_TEXT);
assertThat(linkFromDb, equalTo(link));
}
}
应用程序测试.属性
:
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
但是,当我尝试运行我的测试时,我有java.lang.NullPointerException
。
我想这是因为数据库没有必要的记录
从日志中可以看出,他试图使用的是真实的数据库,而不是测试库。出于某种原因,它不会读取我的测试配置。
我做错了什么
更新:
当我这样写ShortenerAppTest
时:
@DatabaseSetup("/datasets/link-table.xml")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class})
public class ShortenerAppTest {
@Autowired
LinkRepository repository;
@Test
public void contextLoads() {
Optional<Link> got = repository.findOne(100500L);
System.out.println(got);
}
}
@DatabaseSetup(“/datasets/link table.xml”)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class})
公共类简短测试{
@自动连线
链接存储库;
@试验
public void contextLoads(){
可选got=repository.findOne(100500L);
System.out.println(got);
}
}
这是可行的,但我的
LinkRepositoryTest
中仍然有一个错误,为什么需要这个语句?spring.jpa.hibernate.ddl auto=create-drop@TalAvissar,已删除。但是没有以任何方式解决我的问题您显示的错误是丢弃序列链接不工作。然后它创造了它。那么问题是什么呢?这个错误不是问题。@Talavisar,links\u seq
我在真实环境中使用的是PostgeSQL
,不在测试中!在测试中,我使用h2db。问题是表中的3条记录应该在数据集上创建,但它们没有创建,因此测试不起作用。为什么需要此语句?spring.jpa.hibernate.ddl auto=create-drop@TalAvissar,已删除。但是没有以任何方式解决我的问题您显示的错误是丢弃序列链接不工作。然后它创造了它。那么问题是什么呢?这个错误不是问题。@Talavisar,links\u seq
我在真实环境中使用的是PostgeSQL
,不在测试中!在测试中,我使用h2db。问题是表中的3条记录应该在数据集上创建,但它们没有创建,因此测试不起作用。