小编enh*_*ack的帖子

为什么我不能用条件运算符(?:)替换if语句?

我一直在努力转向

private PlaneClass preferredClass;

if (preferredClass == PlaneClass.FIRST_CLASS)
    preferredClass = PlaneClass.ECONOMY_CLASS;
else
    preferredClass = PlaneClass.FIRST_CLASS;
Run Code Online (Sandbox Code Playgroud)

成

preferredClass == PlaneClass.FIRST_CLASS ? 
                preferredClass = PlaneClass.ECONOMY_CLASS 
                               : preferredClass = PlaneClass.FIRST_CLASS;
Run Code Online (Sandbox Code Playgroud)

if语句编译.条件运算符没有.(错误消息:1.类型不匹配:无法从PlaneClass转换为布尔2.令牌上的语法错误"=".还有另外两个错误......).我哪里做错了?

java conditional if-statement ternary-operator

2
推荐指数
1
解决办法
862
查看次数

使用 @Sql & H2 的 Spring Boot 测试给出了 JdbcSQLIntegrityConstraintViolationException: 唯一索引或主键冲突

我有一个简单的 Spring Boot (2.6.1) 测试,它抛出:

 JdbcSQLIntegrityConstraintViolationException: 
 Unique index or primary key violation
Run Code Online (Sandbox Code Playgroud)

该测试从 加载数据test/resources/data.sql,其中只有一条语句:

INSERT INTO country (name) VALUES ('Italy');
Run Code Online (Sandbox Code Playgroud)

测试类是:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class FailingTest {

   @Test
   @Sql("/data.sql")
   void test(){
       assertTrue(true);
   }
}
Run Code Online (Sandbox Code Playgroud)

域类是:

@Entity 
public class Country {
   @Id
   @GeneratedValue(strategy = IDENTITY)
   private Integer id;

   @Column(nullable = false, unique = true)
   private String name;
}
Run Code Online (Sandbox Code Playgroud)

该application.properties文件包含:

spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.jpa.defer-datasource-initialization=true
Run Code Online (Sandbox Code Playgroud)

异常消息是:

org.springframework.jdbc.datasource.init.ScriptStatementFailedException:
Failed to execute SQL script statement #1 of class path resource [data.sql]: 
INSERT …
Run Code Online (Sandbox Code Playgroud)

testing hibernate h2 unique-constraint spring-boot-test

2
推荐指数
1
解决办法
4098
查看次数