[英]java.lang.IllegalArgumentException: No DataSource specified Junit and Mockito
Good Morning people! 大家早上好! I am doing unit test in my spring boot application but it launches the next exception: java.lang.IllegalArgumentException: No DataSource specified;
我正在我的Spring Boot应用程序中进行单元测试,但它会启动下一个异常: java.lang.IllegalArgumentException:未指定DataSource;请参见图9。
This is my method: 这是我的方法:
@RestController
public class controlador {
@Autowired(required = true)
JdbcTemplate conn;
@CrossOrigin
@RequestMapping(value = "/getlistadopantallatab", method = RequestMethod.POST, consumes="application/json",produces = "application/json")
@ResponseBody
public Map<String, Object> getListadoPantallaTab(@RequestBody Map<String,Object> dto) {
Map<String, Object> simpleJdbcCallResult = null;
try {
SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(conn)
.withCatalogName("CCR_PACKAGE")
.withProcedureName("getListadoPorPantallaTab");
SqlParameterSource in = new MapSqlParameterSource(dto);
simpleJdbcCallResult = simpleJdbcCall.execute(in);
} catch (Exception e) {
System.out.println("Se ha lanzado la siguiente excepcion: " + e);
}
return simpleJdbcCallResult;
}
and it is my test: 这是我的测试:
public class controladorTest {
static controlador mockInstanced;
@BeforeClass
public static void setup() throws Exception {
mockInstanced= new controlador();
mockInstanced.conn = mock(JdbcTemplate.class);
}
/**
* Test of getListadoPantallaTab method, of class controlador.
*/
@Test
public void testGetListadoPantallaTab() {
System.out.println("Test unitario getListadoPantallaTab: ");
@SuppressWarnings("serial")
Map<String, Object> dto = new HashMap<String, Object>() {{
put("Inicio", 1);
put("fin", 15);
}};
mockInstanced.getListadoPantallaTab(dto);
}
Somebody knows what i am doing wrong? 有人知道我在做什么错吗?
PD: Sorry for my english, i am spanish! PD:对不起,我的英语,我是西班牙语!
You are trying to test a class, which uses beans without starting the Spring's application context. 您正在尝试测试一个类,该类使用Bean而不启动Spring的应用程序上下文。
There are multiple ways to solve your problem. 有多种方法可以解决您的问题。
@SpringBootTest
and @RunWith(SpringRunner.class)
annotations to your test class. @SpringBootTest
和@RunWith(SpringRunner.class)
批注来实现。 Something like this: @SpringBootTest
@RunWith(SpringRunner.class)
public class controladorTest {
...
}
This way the Spring application context will be created - but you don't have to mock your controller instance - as it's already created - so you can just autowire it: 这样,将创建Spring应用程序上下文-但您不必模拟控制器实例-因为它已经创建-因此您可以自动装配它:
@SpringBootTest
@RunWith(SpringRunner.class)
public class controladorTest {
@Autowired
private controlador controlador;
...
}
and remove next lines : 并删除下一行:
static controlador mockInstanced;
@BeforeClass
public static void setup() throws Exception {
mockInstanced= new controlador();
mockInstanced.conn = mock(JdbcTemplate.class);
}
JdbcTemplate
and inject it into your mock using @InjectMocks
- but I wouldn't recommend to do it - too cumbersome and fragile solution. JdbcTemplate
并使用@InjectMocks
将其注入到模拟中-但我不建议这样做-太麻烦又脆弱的解决方案。 JdbcTemplate
as a dependency, you can use an embedded DB for Junit and create the JdbcTemplate
manually and inject it to your controller - in this case, there is no need to create the application context and you can just manually create a controller instance and pass JdbcTemplate to it. JdbcTemplate
作为依赖项,您可以使用嵌入式DB for Junit并手动创建JdbcTemplate
并将其注入到控制器中-在这种情况下,无需创建应用程序上下文和您只需手动创建一个控制器实例并将JdbcTemplate传递给它即可。 See this for more info So after step 1, your code should look like the following: 因此,在执行步骤1之后,您的代码应如下所示:
@SpringBootTest
@RunWith(SpringRunner.class)
public class controladorTest {
@Autowired
private controlador controlador;
/**
* Test of getListadoPantallaTab method, of class controlador.
*/
@Test
public void testGetListadoPantallaTab() {
System.out.println("Test unitario getListadoPantallaTab: ");
@SuppressWarnings("serial")
Map<String, Object> dto = new HashMap<String, Object>() {{
put("Inicio", 1);
put("fin", 15);
}};
controlador.getListadoPantallaTab(dto);
}
}
By the way, please look at Java naming conventions - to make your code more readable. 顺便说一下,请查看Java命名约定 -使您的代码更具可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.