简体   繁体   中英

Spring 4.07 Junit Test and Autowired

Hi I'm using Spring and CDI. In my unit test I want to test a Class that uses the @Autowired annotation. Problem is if I create a instance of this class and call a method all annotated objects are null. In basic the annotation works. Just whithin my unit test it doesn't

This is my Unit Test. In here Autowired works. In my test I create an instance of the DemoConsumerBean .class and call the method requestJobsFromPublishedJobsApi in here I have also some Autowired declaration. Problem is all instances are null!

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("development")
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, FirstbirdTestExecutionListener.class, FlywayTestExecutionListener.class })
@ContextConfiguration(locations = { "classpath:hibernate-spring.xml" })
@FlywayTest
public class DemoConsumerBeanTest extends AbstractJUnit4SpringContextTests {

@Autowired
private CustomerManager customerManager;

@Autowired
private DemoDetailsManager demoDetailsManager;

@Before
public void setup() {
    CamelContext context = new DefaultCamelContext();
    exchange = new DefaultExchange(context);
}

@Test
public void requestJobsFromPublishedJobsApiTest() throws NoSuchDataException {
    DemoConsumerBean demoConsumerBean = new DemoConsumerBean();

    customer = new Customer();
    customer.setCustomerId(15);

    customer = customerManager.getCustomerById(customer);

    // This one works
    DemoDetails demoDetails = demoDetailsManager.getDemoDetailsByCustomerId(customer);

    demoConsumerBean.requestJobsFromPublishedJobsApi(exchange, customer);

    PublishedJobs apiJobs = exchange.getIn().getBody(PublishedJobs.class);

    assertNotNull(apiJobs);

}

}

public class DemoConsumerBean {

@Autowired
protected CustomerManager customerManager;
@Autowired
protected DemoDetailsManager demoDetailsManager;
@Autowired
protected MessageLogManager messageLogManager;

public void requestJobsFromPublishedJobsApi(Exchange exchange, Customer customer) throws NoSuchDataException {
    //this one is null!
    DemoDetails demoDetails = demoDetailsManager.getDemoDetailsByCustomerId(customer);
    PublishedJobs jobs = null;

    if (demoDetails == null || StringUtils.isBlank(demoDetails.getDemoApiUrl())) {
        throw new NoSuchDataException("No demo data found for customer " + customer.getCustomerFirstbirdId());
    }
....
}   

}

Using new in new DemoConsumerBean(); bypasses spring, that's your issue.

Either use a DemoConsumerBean instance from spring (ie autowired in the test) or add setters and use them to "manually autowire" in DemoConsumerBean in your test:

@Test
public void requestJobsFromPublishedJobsApiTest() throws NoSuchDataException {
    DemoConsumerBean demoConsumerBean = new DemoConsumerBean();
    demoConsumerBean.setCustomerManager(this.customerManager)
    // etc

Some reading: Spring framework reference - The IoC container

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM