是否可以合并';JGiven Spring';与';JGiven JUnit 5';?
不幸的是,SpringBean并没有与我的方法自动关联。我使用了JGiven的0.17.0版 以下测试因NullPointerException而失败,因为HelloWorldStage类中的SpringBean“messageService”为null 格拉德尔:是否可以合并';JGiven Spring';与';JGiven JUnit 5';?,spring,junit-jupiter,jgiven,Spring,Junit Jupiter,Jgiven,不幸的是,SpringBean并没有与我的方法自动关联。我使用了JGiven的0.17.0版 以下测试因NullPointerException而失败,因为HelloWorldStage类中的SpringBean“messageService”为null 格拉德尔: testCompile group: 'com.tngtech.jgiven', name: 'jgiven-junit5', version: '0.17.0' testCompile group: 'com.tngtech.jg
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-junit5', version: '0.17.0'
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-spring', version: '0.17.0'
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-junit5', version: '0.17.0'
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-spring', version: '0.17.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.3.2'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.1.RELEASE'
测试等级:
import com.tngtech.jgiven.annotation.ScenarioStage;
import com.tngtech.jgiven.integration.spring.EnableJGiven;
import com.tngtech.jgiven.junit5.JGivenExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootTest(classes = {HelloWorldTest.class})
@Configuration
@EnableJGiven
@ComponentScan(basePackages = "sample.jgiven.*")
@ExtendWith(JGivenExtension.class)
class HelloWorldTest {
@ScenarioStage
private HelloWorldStage helloWorldStage;
@Test
void sayHello() {
helloWorldStage
.given().recipient("Bob")
.when().sendMessage()
.then().answer();
}
}
阶段:
import com.tngtech.jgiven.Stage;
import com.tngtech.jgiven.annotation.Quoted;
import com.tngtech.jgiven.annotation.ScenarioState;
import com.tngtech.jgiven.integration.spring.JGivenStage;
import org.springframework.beans.factory.annotation.Autowired;
@JGivenStage
class HelloWorldStage extends Stage<HelloWorldStage> {
@Autowired
private MessageService messageService;
@ScenarioState
private String recipient;
HelloWorldStage recipient(@Quoted String name) {
this.recipient = name;
return self();
}
public HelloWorldStage sendMessage() {
return self();
}
public String answer() {
return messageService.createMessage(recipient);
}
}
是的,这是可能的,但是,它目前需要一个解决方案。你必须做以下两件事:
- 从jgiven spring依赖项中排除jgiven junit
- 创建新的基类SpringJUnit5 ScenarioTest,扩展SpringExtension和JGivenExtension,还实现BeanFactoryAware以设置场景的阶段创建者
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-junit5', version: '0.17.0'
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-spring', version: '0.17.0'
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-junit5', version: '0.17.0'
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-spring', version: '0.17.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.3.2'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.1.RELEASE'
测试类别:
package mypackage;
import org.springframework.stereotype.Service;
@Service
public class MessageService {
public String createMessage(String recipient) {
return "Hello " + recipient;
}
}
弹簧配置:
package mypackage;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.tngtech.jgiven.integration.spring.EnableJGiven;
@EnableJGiven
@Configuration
@ComponentScan(basePackages = {"mypackage"})
public class SpringConfig {
}
舞台类:
package mypackage;
import org.springframework.beans.factory.annotation.Autowired;
import com.tngtech.jgiven.Stage;
import com.tngtech.jgiven.annotation.ScenarioState;
import com.tngtech.jgiven.integration.spring.JGivenStage;
import static org.assertj.core.api.Assertions.assertThat;
@JGivenStage
class HelloWorldStage extends Stage<HelloWorldStage> {
@Autowired
private MessageService messageService;
@ScenarioState
private String recipient;
@ScenarioState
private String answer;
HelloWorldStage recipient(String name) {
this.recipient = name;
return self();
}
public HelloWorldStage sendMessage() {
answer = messageService.createMessage(recipient);
return self();
}
public void answerIs(String expectedAnswer) {
assertThat(answer).isEqualTo(expectedAnswer);
}
}