私は、を使用SpringBoot
しSpring Test DBUnit
ます。私はテストを書きました:
ShortenerAppTest
:
@TestPropertySource(locations = "classpath:application-test.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ShortenerApp.class)
@WebAppConfiguration
public class ShortenerAppTest {
@Test
public void contextLoads() {
}
}
repositories/AbstractRepositoryTest
:
@TestPropertySource(locations = "classpath:application-test.properties")
@TestExecutionListeners(DbUnitTestExecutionListener.class)
@SpringBootTest(classes = ShortenerApp.class)
@DirtiesContext
abstract class AbstractRepositoryTest extends AbstractTransactionalJUnit4SpringContextTests {
}
repositories/LinkRepositoryTest
:
@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));
}
}
そしてdataset
:
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<links id="100500" url="http://www.ya.ru"/>
<links id="100501" url="http://www.mail.ru"/>
<links id="100502" url="http://www.google.com"/>
</dataset>
application-test.properties
:
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);
}
}
そして、それはうまくいきましたが、それでもエラーが発生しますLinkRepositoryTest