我用JUnit测试以下DAO:
@Repository
public class MyDao {
@Autowired
private SessionFactory sessionFactory;
// Other stuff here
}
Run Code Online (Sandbox Code Playgroud)
如您所见,sessionFactory使用Spring自动装配.当我运行测试时,sessionFactory保持为null并且我得到一个空指针异常.
这是Spring中的sessionFactory配置:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
怎么了?如何为单元测试启用自动装配?
更新:我不知道它是否是运行JUnit测试的唯一方法,但请注意我在Eclipse中运行它,右键单击测试文件并选择"run as" - >"JUnit test"
我的实体有一个自定义的Hibernate验证器.我的一个验证器使用Autowired Spring @Repository.应用程序正常工作,我的存储库在我的验证器上成功自动装配.
问题是我找不到测试验证器的方法,因为我无法在其中注入我的存储库.
Person.class:
@Entity
@Table(schema = "dbo", name = "Person")
@PersonNameMustBeUnique
public class Person {
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column()
@NotBlank()
private String name;
//getters and setters
//...
}
Run Code Online (Sandbox Code Playgroud)
PersonNameMustBeUnique.class
@Target({ TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = { PersonNameMustBeUniqueValidator.class })
@Documented
public @interface PersonNameMustBeUnique{
String message() default "";
Class<?>[] groups() default {};
Class<? extends javax.validation.Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)
验证者:
public class PersonNameMustBeUniqueValidatorimplements ConstraintValidator<PersonNameMustBeUnique, Person> {
@Autowired
private …Run Code Online (Sandbox Code Playgroud) 我正在做一个Spring Boot项目。我正在基于“ TDD”编写“单元测试”代码,这有点困难。
@SpringBootTest加载了所有的BEAN,这导致更长的测试时间。
因此,我使用了@SpringBootTest的类名称。
我已正常完成测试,但不确定使用@ContextConfiguration与使用@Import之间的区别。
这三个选项均正常运行。我想知道哪种选择是最好的。
@Service
public class CoffeeService {
private final CoffeeRepository coffeeRepository;
public CoffeeService(CoffeeRepository coffeeRepository) {
this.coffeeRepository = coffeeRepository;
}
public String getCoffee(String name){
return coffeeRepository.findByName(name);
}
}
public interface CoffeeRepository {
String findByName(String name);
}
@Repository
public class SimpleCoffeeRepository implements CoffeeRepository {
@Override
public String findByName(String name) {
return "mocha";
}
}
Option 1(SpringBootTest Annotation) - OK
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {CoffeeService.class, SimpleCoffeeRepository.class})
public class CoffeeServiceTest {
@Autowired
private CoffeeService coffeeService;
@Test
public void getCoffeeTest() { …Run Code Online (Sandbox Code Playgroud) 我正在使用自动装配 (@Autowired) 在 JUnit 测试类中注入依赖项,但面临 NullPointerException。我想知道 JUnit 测试类中是否可以进行自动装配。否则应该如何将bean注入到测试类中。我的代码如下 -
主类/客户端- 自动装配按预期工作。
package com.example.demo;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.example.demo.services.IMessage;
import com.example.demo.services.SayWelcomeService;
@SpringBootApplication
@ComponentScan("com.example.demo.services")
public class AutowireWithMultipleImplementationsApplication {
@Autowired
IMessage sayHelloService;
@Autowired
SayWelcomeService sayWelcome;
@Autowired
IMessage masterService;
public static void main(String[] args) {
SpringApplication.run(AutowireWithMultipleImplementationsApplication.class, args);
}
@PostConstruct
public void init() {
String message;
message=masterService.message("George");
System.out.println("message: \n" + message);
message=sayWelcome.message("george");
System.out.println("message: " + message);
}
}
Run Code Online (Sandbox Code Playgroud)
服务接口和实现类
接口IMessage
package com.example.demo.services;
public interface IMessage { …Run Code Online (Sandbox Code Playgroud) junit spring dependency-injection inversion-of-control autowired