2017-09-24 153 views
0

在我的Java弹簧MVC应用, 我面对NullPointException在我的JUnit测试,所有的类都位于某个包,我有它们如下面main方法NullpointException在JUnit测试

@SpringBootApplication 
@ComponentScan({ "com.example.model" }) 
public class DemoApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(DemoApplication.class, args); 
    } 
} 

寻址然后在com.example.model我有以下类和接口:

public interface DataService { 

    int[] reteriveAllData(); 

} 

的实现如下:

@Service 
public class DataServiceImpl implements DataService { 

    @Override 
    public int[] reteriveAllData() { 

     return new int[] { 1, 2, 3, 4 }; 
    } 
} 

,然后我使用的服务,如下面另一个类:

public class SomeBussiness { 

    @Autowired 
    DataServiceImpl dataService; 

    public int findTheGreat() { 
     int[] res = dataService.reteriveAllData(); 
     return res[0]; 

    } 
} 

的我写了一个测试,以检查findTheGreattest()SomeBussiness类:

public class SomeBussinessTest { 

    @Test 
    public void findTheGreatTest() { 
     SomeBussiness sbi = new SomeBussiness(); 
     int res = sbi.findTheGreatest(); 
     assertEquals(1, res); 
    } 
} 

但它与NullPointException在抱怨行

int[] res = dataService.reteriveAllData(); 

但是,我有你sed @Service@Autowiered注入dataService

我该如何解决它?


更新:

我已经加入@ServiceSomeBussiness类:

@Service 
public class SomeBussiness { 

    @Autowired 
    DataServiceImpl dataService; 

    public int findTheGreat() { 
     int[] res = dataService.reteriveAllData(); 
     return res[0]; 

    } 
} 

,然后添加更改如下测试:

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes = DemoApplication.class) 
public class SomeBussinessTest { 

    @Autowired 
    SomeBussiness sbi; 

    @Test 
    public void findTheGreatestTest() { 
     assertEquals(1, sbi.findTheGreatest()); 

    } 
} 

,现在它与抱怨:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.SomeBussinessTest': Unsatisfied dependency expressed through field 'sbi'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.model.SomeBussiness' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
+0

由于新的关键字。当你使用new创建一个实例时,Spring的依赖关系不会发生,你必须确保传递依赖关系。 – Barath

+0

我不明白你的意思。你能写一个答案吗? –

回答

0

由于新的关键字。当你使用new创建一个实例时,Spring的依赖注入不会发生,你必须确保通过构造函数或setter来传递依赖。

@Test 
    public void findTheGreatTest() { 
     SomeBussiness sbi = new SomeBussiness(); 
     sbi.setDataService(dbService); 
     int res = sbi.findTheGreatest(); 
     assertEquals(1, res); 
    } 

使用SpringRunner

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = DemoApplication.class) 
public class SomeBussinessTest { 

    @Autowired 
    SomeBussiness sbi; 

    @Test 
    public void findTheGreatestTest() { 
     assertEquals(1, sbi.findTheGreatest()); 

    } 
} 

这篇文章清楚地解释新关键字的作用:

Role of new keyword in Spring Framework

+0

@KamyarParastesh将“@ Service”添加到公共类SomeBussiness {}此类。不在SomeBussinessTest上。 – Barath

+0

@KamyarParastesh仍然错误?将这些类添加到ContextConfiguration以加载上下文。看到更新的答案。如果仍然错误共享包结构 – Barath

+0

创建拉请求看到更改。也更新了它的工作答案。 – Barath

0

从未触发过类中的Autowired。首先你需要一个初始化所有测试类的JUnit运行器。此外,您必须通过此运行程序创建受测试的类,而不是通过调用“新建”来创建它:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {DemoApplication.class}) 
public class SomeBussinessTest { 

    @Autowired 
    SomeBussiness sbi; 

    @Test 
    public void findTheGreatTest() { 
     int res = sbi.findTheGreatest(); 
     assertEquals(1, res); 
    } 
} 
+0

你可以查看更新的问题吗? –