简体   繁体   English

java.lang.IllegalArgumentException:未指定Junit和Mockito的数据源

[英]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. 有多种方法可以解决您的问题。

  1. Make spring to run application context. 使spring运行应用程序上下文。 You can do that by adding: @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);
}
  1. Another theoretical way is to mock JdbcTemplate and inject it into your mock using @InjectMocks - but I wouldn't recommend to do it - too cumbersome and fragile solution. 另一种理论上的方法是模拟JdbcTemplate并使用@InjectMocks将其注入到模拟中-但我不建议这样做-太麻烦又脆弱的解决方案。
  2. Also, taking into account, that your controller uses only 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命名约定 -使您的代码更具可读性。

More info about testing with spring here 有关在此处进行弹簧测试的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:未指定Bean - java.lang.IllegalArgumentException: No bean specified 春季3.2.9; 瓷砖2.0.7; 调用init方法失败; 嵌套异常是java.lang.IllegalArgumentException:未指定DataSource - Spring 3.2.9; Tiles 2.0.7; Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No DataSource specified java.lang.IllegalArgumentException - java.lang.IllegalArgumentException java.lang.IllegalArgumentException - java.lang.IllegalArgumentException java.lang.IllegalArgumentException:启动tomcat时需要&#39;dataSource&#39;或&#39;jdbcTemplate&#39; - java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required when starting tomcat java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required - java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required Junit 与 MockMVC - 错误 - java.lang.IllegalArgumentException:实体不能为空 - Junit with MockMVC - Error - java.lang.IllegalArgumentException: Entity must not be null 骆驼:java.lang.IllegalArgumentException:必须指定defaultEndpoint - Camel: java.lang.IllegalArgumentException: defaultEndpoint must be specified java.lang.IllegalArgumentException:只能指定目标位置 - java.lang.IllegalArgumentException: Only the target location may be specified java.lang.IllegalArgumentException:没有实现指定的模式语言的SchemaFactory - java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM