I am new to spring boot.
I have a Service class as below
@Service
public class AService{
@Value("${sample.time})
private Long time;
...
public int sampleMethod(){
...
return time;
}
}
and my application.yml under src/resource has:
sample:
time:1
and my junit test class is:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = MainApplication.class)
public class AServiceTest{
@Autowired
private WebApplicationContext wac;
private Aservice aservice;
@Before
public void setupMockMvc() {
MockitoAnnotations.initMocks(this);
aservice = new AService();
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
public void sampleMethodTest(){
Assert.assertEquals(1,aservice.sampleMethod());
}
This always gives false, because the method aservice.sampleMethod()
Returns 0
.
It is not able to read the yml file under src when am testing.
I know that since ist injected with spring by standalone like this wont work. But it would be great help if you could guide me with possible Solutions or any pointers are helpful too.
Thanks in advance