我阅读了《使用 Node.js 和 Express 进行 Web 开发》这本书。并且有使用的功能suite()。
var assert = require('chai').assert;
suite('tests', function () {
// set of tests
});
Run Code Online (Sandbox Code Playgroud)
我不明白它来自哪里。我找不到有关此功能的任何文档。似乎它的外观和describe()功能与 Mocha 中的功能相同。
我正在编写集成测试,它应该只适用于特定的配置文件。问题是我应该从 application.properties 文件中的 spring.profiles.active 值中获取配置文件名称。但由于 application.properties 的实际价值,我总是得到 null。
我为 main 方法创建了明确地将值放入 System.properties 的方法:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Autowired
public MyApplication(Environment environment) {
String property = "spring.profiles.active";
System.getProperties().setProperty(property, environment.getProperty(property));
}
Run Code Online (Sandbox Code Playgroud)
但该值仍然为空。在测试类中,我使用以下注释:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MyApplication.class)
@SpringBootTest(classes=MyApplication.class)
Run Code Online (Sandbox Code Playgroud)
对于从主类加载上下文,我手动设置了必要的值。也尝试使用@TestPropertySource("classpath:application.properties")
但没有任何帮助。而且我无法对该值进行硬编码。它应该只能从 application.properties 获取
更新 这是我的错误。我已经从静态方法中尝试过 getValue :/ 也许有人知道如何通过使用这个值来禁用类测试?@IfProfileValue 仍然返回 null,因此不适合。
更新 我意识到禁用是无用的,最好使用@SpringBootTest(classes=AppropriateConfigClass.class) 使用适当的配置
我会选择gargkshitiz作为已解决的答案,因为他是唯一一个试图提供帮助的人。
例如我有
public interface CrudUserRepository extends JpaRepository<User, Integer> {
@Modifying
@Query(nativeQuery = true, value = "UPDATE users SET active=?2 WHERE id=?1")
void setActive(long id, boolean active);
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我调用这个方法,然后通过 id 查找。
@Autowired
CrudUserRepository crudUser;
@Transactional
@Test
public void setActiveTest(){
User user = getUser();
crudUser.save(user);
crudUser.setActive(user.getId(), true);
Optional<User> optUser = crudUser.findById(user.getId());
assert optUser.get().isActive(); //here my test falls
Run Code Online (Sandbox Code Playgroud)
当我执行时,crudUser.findById(user.getId())我的 optUser 包含该用户不活动,但实际上他是活动的(我已经通过设置 @Rollback(false) 并手动检查数据库进行了测试)。
我如何测试这个案例?