不幸的是,我的方法无法自动安装春豆。我使用的是JGiven的0.17.0版本。
以下测试失败,并出现NullPointerException,因为类HelloWorldStage中的spring bean'messageService'为null。
等级:
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-junit5', version: '0.17.0'
testCompile group: 'com.tngtech.jgiven', name: 'jgiven-spring', version: '0.17.0'
测试类:
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);
}
}
春豆:
import org.springframework.stereotype.Service;
@Service
public class MessageService {
public String createMessage(String recipient) {
return "Hello " + recipient;
}
}
答案 0 :(得分:0)
是的,但是目前需要一种解决方法。您必须做以下两件事:
有关详细信息,另请参见https://github.com/TNG/JGiven/issues/369。
JGiven的下一个主要版本将立即提供Spring 5支持。
答案 1 :(得分: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);
}
}
测试:
package mypackage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringConfig.class })
public class HelloWorldTest {
@Autowired
private HelloWorldStage helloWorldStage;
@Test
void someTest() {
helloWorldStage
.given().recipient("World")
.when().sendMessage()
.then().answerIs("Hello World");
}
}