spring boot集成测试(业务层)

时间:2018-01-18 19:59:34

标签: java spring-boot integration-testing

我正在尝试为Spring启动应用程序的业务层设置集成测试。单元测试工作正常,但集成测试不起作用。这是基本设置:

11111111111111111111111111111111
37777777777
ffffffff
// entities
@Entity
Table(name="TOrder")
public class JPAOrder... {
}

@Entity
Table(name="TCustomer")
public class JPACustomer... {
}
// Repository interfaces
@Repository
public interface OrderRepository extends CrudRepository<JPAOrder, Long> {
...
}

@Repository
public interface CustomerRepository extends CrudRepository<JPACustomer, Long> {
...
}
// Business logic
@Service
@Transactional
public class OrderService ... {
...
@Autowired
private CustomerRepository customerRepository;
...
public Order createOrderForCustomer(final Order order, final Customer customer) {
...
}
}

启动应用程序会给我这个错误

// Test
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
public class OrderIntegrationTest {

@SpyBean
private OrderRepository orderRepository;

@Autowired
private OrderService orderService;

Order order = orderService.createOrderForCustomer(...);
}

如果我没有在集成测试中使用@SpyBean批注,OrderService中的orderRepository就是null。 我必须遗漏一些非常明显的东西,但我无法弄清楚是什么。 有什么建议吗?

1 个答案:

答案 0 :(得分:1)

对我来说,这个例外也发生了。请参阅This Question

尝试更改@TestPropertySource(..)

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
            value={"spring.profiles.active=integrationtest"})

希望它有所帮助!